预计'}在我的程序主函数的输入结束时
该程序应该教授函数。我已将每个函数分成头文件。我认为某个地方有一个花括号向后或丢失,但我已经盯着这个程序几个小时并尝试重新安排事情,但似乎无法让任何事情进行下去。
该程序应该读取电话号码并将其打印出来。如果提供了字母,那么在将其设为大写字母后,它会将其排序为数字 0-9,就像电话键盘上的一样。它还会返回无效字符等的错误代码,这是由 switch 语句控制的。
主要功能
我遇到的错误之一是在最后一行的右大括号上:
输入末尾应有“}”
#include <iostream>
#include <cctype>
#include "Read_Dials.h"
#include "To_Digit.h"
#include "Acknowledge_Call.h"
using namespace std;
int main()
{
char digit1, digit2, digit3, digit4, digit5, digit6, digit7, digit8;
int return_value = 0;
return_value = int Read_dials(digit1, digit2, digit3, digit4, digit5, digit6, digit7, digit8);
if (return_value != -5)
break;
switch(return_value){
case -1:
cout << "ERROR - An invalid character was entered. Please try again, only numbers or letters this time." << endl;
break;
case -2:
cout << "ERROR - Phone number cant start with 0." << endl;
break;
case -3:
cout << "ERROR - This isn't the movies, Phone numbers dont start with \" 555 \" here buddy :/" << endl;
break;
case -4:
cout << "ERROR - Please make sure the hyphen is in position 4." << endl;
break;
default:
void Acknowledge_Call(digit1, digit2, digit3, digit4, digit5, digit6, digit7, digit8);
}
return 0;
}
Read_Dials 函数
此函数中没有错误
int Read_Dials(char &num1, char &num2, char &num3, char &num4, char &num5, char &num6, char &num7, char &num8)
{
#include "To_Digit.h"
int i = 0;
do{
i++;
cout << "Please enter the character for position #" << i << " in the phone number\n";
cout << "NOTE: Please put the hyphen \" - \" in the fourth position and use \"Q\"to quit." << endl;
char temp;
cin >>temp;
if (i = 1 && temp == 0)
{
return_value = -2;
}
else if (i == 1 && (temp == 'q' || temp == 'Q'))
{
return_value -5;
}
else if (i == 1)
{
temp = &num1;
&inputValue = &num1;
int To_Digit(char &num1);
}
else if (i == 2)
{
temp = &num2;
&inputValue = &num2;
int To_Digit(char &num2);
}
else if (i == 3)
{
temp = &num3;
&inputValue = &num3;
int To_Digit(char &num3);
}
else if (&num1 == '5' && &num2 == '5' && &num3 == '5')
{
return_value -3;
}
else if (i == 4 && temp != '-')
{
return_value -4;
}
else if (i == 5)
{
temp = &num5;
&inputValue = &num5;
int To_Digit(char &num5);
}
else if (i == 6)
{
temp = &num6;
&inputValue = &num6;
int To_Digit(char &num6);
}
else if (i == 7)
{
temp = &num7;
&inputValue = &num7;
int To_Digit(char &num7);
}
else if (i == 8)
{
temp = &num8;
&inputValue = &num8;
int To_Digit(char &num8);
}
}while (i < 8)
return 0;
}
To_Digit 函数
我得到的第二个也是最后一个错误在这里,在第二行(左大括号):
在“{”标记之前不允许有函数定义
int To_Digit(char &inputValue)
{
char &inputValue;
if (isdigit(&inputValue))
break;
&inputValue = toupper(&inputValue);
switch(&inputValue){
case 'A': case 'B': case 'C':
&inputValue = '2';
break;
case 'D': case 'E': case 'F':
&inputValue = '3';
break;
case 'G': case 'H': case 'I':
&inputValue = '4';
break;
case 'J': case 'K': case 'L':
&inputValue = '5';
break;
case 'M': case 'N': case 'O':
&inputValue = '6';
break;
case 'P': case 'Q': case 'R': case 'S':
&inputValue = '7';
break;
case 'T': case 'U': case 'V':
&inputValue = '8';
break;
case 'W': case 'X': case 'Y': case 'Z':
&inputValue = '9';
break;
default:
return -1;
}
}
Acknowledge_Call 函数
此函数没有错误。
void Acknowledge_Call(digit1, digit2, digit3, digit4, digit5, digit6, digit7, digit8)
{
cout << "Phone number entered is: " << digit1 << digit2 << digit3 << digit4 << digit5 << digit6 << digit7 << digit8 << endl;
}
这段代码有什么问题?我该如何修复它?
This program is supposed to teach functions. I have separated the functions each into header files. I think there is a curly brace somewhere that is backwards or missing, but I have stared at this program for hours and tried re-arranging things and can't seem to get anything going.
This program is supposed to read a phone number and print it out. If it is provided letters then it will sort it to a number 0-9 like on a phone keypad, after making it an uppercase letter. It will also return error codes for invalid characters, etc., which is controlled by a switch statement.
Main function
One of the errors I am getting is on the closing brace on the last line:
expected '}' at end of input
#include <iostream>
#include <cctype>
#include "Read_Dials.h"
#include "To_Digit.h"
#include "Acknowledge_Call.h"
using namespace std;
int main()
{
char digit1, digit2, digit3, digit4, digit5, digit6, digit7, digit8;
int return_value = 0;
return_value = int Read_dials(digit1, digit2, digit3, digit4, digit5, digit6, digit7, digit8);
if (return_value != -5)
break;
switch(return_value){
case -1:
cout << "ERROR - An invalid character was entered. Please try again, only numbers or letters this time." << endl;
break;
case -2:
cout << "ERROR - Phone number cant start with 0." << endl;
break;
case -3:
cout << "ERROR - This isn't the movies, Phone numbers dont start with \" 555 \" here buddy :/" << endl;
break;
case -4:
cout << "ERROR - Please make sure the hyphen is in position 4." << endl;
break;
default:
void Acknowledge_Call(digit1, digit2, digit3, digit4, digit5, digit6, digit7, digit8);
}
return 0;
}
Read_Dials Function
No errors in this function
int Read_Dials(char &num1, char &num2, char &num3, char &num4, char &num5, char &num6, char &num7, char &num8)
{
#include "To_Digit.h"
int i = 0;
do{
i++;
cout << "Please enter the character for position #" << i << " in the phone number\n";
cout << "NOTE: Please put the hyphen \" - \" in the fourth position and use \"Q\"to quit." << endl;
char temp;
cin >>temp;
if (i = 1 && temp == 0)
{
return_value = -2;
}
else if (i == 1 && (temp == 'q' || temp == 'Q'))
{
return_value -5;
}
else if (i == 1)
{
temp = &num1;
&inputValue = &num1;
int To_Digit(char &num1);
}
else if (i == 2)
{
temp = &num2;
&inputValue = &num2;
int To_Digit(char &num2);
}
else if (i == 3)
{
temp = &num3;
&inputValue = &num3;
int To_Digit(char &num3);
}
else if (&num1 == '5' && &num2 == '5' && &num3 == '5')
{
return_value -3;
}
else if (i == 4 && temp != '-')
{
return_value -4;
}
else if (i == 5)
{
temp = &num5;
&inputValue = &num5;
int To_Digit(char &num5);
}
else if (i == 6)
{
temp = &num6;
&inputValue = &num6;
int To_Digit(char &num6);
}
else if (i == 7)
{
temp = &num7;
&inputValue = &num7;
int To_Digit(char &num7);
}
else if (i == 8)
{
temp = &num8;
&inputValue = &num8;
int To_Digit(char &num8);
}
}while (i < 8)
return 0;
}
To_Digit Function
The second and final error I'm getting is here, on the second line (the opening brace):
A function-definition is not allowed here before '{' token
int To_Digit(char &inputValue)
{
char &inputValue;
if (isdigit(&inputValue))
break;
&inputValue = toupper(&inputValue);
switch(&inputValue){
case 'A': case 'B': case 'C':
&inputValue = '2';
break;
case 'D': case 'E': case 'F':
&inputValue = '3';
break;
case 'G': case 'H': case 'I':
&inputValue = '4';
break;
case 'J': case 'K': case 'L':
&inputValue = '5';
break;
case 'M': case 'N': case 'O':
&inputValue = '6';
break;
case 'P': case 'Q': case 'R': case 'S':
&inputValue = '7';
break;
case 'T': case 'U': case 'V':
&inputValue = '8';
break;
case 'W': case 'X': case 'Y': case 'Z':
&inputValue = '9';
break;
default:
return -1;
}
}
Acknowledge_Call function
No errors with this function.
void Acknowledge_Call(digit1, digit2, digit3, digit4, digit5, digit6, digit7, digit8)
{
cout << "Phone number entered is: " << digit1 << digit2 << digit3 << digit4 << digit5 << digit6 << digit7 << digit8 << endl;
}
What's wrong with this code? How can I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我还没有尝试自己运行代码,但我能看到的唯一对我来说看起来很做作的东西是在“Read_Dials”函数中......不要在函数中放置 #INCLUDE 语句。始终将这些语句放在文件的顶部。
移动 #include 并让我们知道它的作用。
祝你好运。
I haven't tried to run the code myself just yet, but the only thing I can see that looks hokey to me is in the "Read_Dials" function... don't put a #INCLUDE statement within a function. Always place those statements at the top of the file.
Move the #include and let us know what that does.
Good luck.
调用函数时不使用返回类型。因此,删除
void
。另外,default
case 应该有一个break
,否则它会失败。Return type is not used while calling a function. So, drop
void
. Alsodefault
case should have abreak
, else it will fall-through.你的 #includes 应该位于文件的顶部...
(在 main 中)是不允许的,因为没有循环可以打破它
Your #includes should be at the top of the file...
(in main) is not allowed as there is no loop for it to break out of
以下是一些问题(除了您没有提供头文件的事实之外):
main() 中“default”之后的函数声明
删除函数调用前面的
void
。Acknowledge_Call 声明中未指定的参数类型
更改为:
void Acknowledge_Call(chardigit1, chardigit2, chardigit3, chardigit4, chardigit5, chardigit6, chardigit7, chardigit8)
去掉前面的
int
main
函数中的Read_dials
。在
main()
中,将“Read_Dials”更改为
Read_Dials:
!=C++ 语言区分大小写,因此 'dials
Dials
!=dIaLs
。在
main()
函数中的if
之后删除break
:您希望
return 1;
或return EXIT_FAILURE;
或exit(1);
请记住将这些行添加到
Acknowledge_Calls。 .cpp
:#include“acknowledge_call.h”
#include
using namespace std;
记住将这些行添加到
Read_Dials.cpp
:#include“read_dials.h”
#include
using namespace std;
在
Read_Dials.cpp
中,将#include "To_Digit.h
移动到文件顶部。 p>执行函数 all 时,不要将返回类型或参数类型放入调用中。
例如,使用:
num1 = To_Digit(digit1);
而不是
int To_Digit(char &num1);
你需要和你的导师就如何调用函数和传递参数进行长谈(这次,请仔细听)。另外,阅读一本关于 C++ 的好书。
Here are some issues (besides the fact you didn't supply header files):
Function declaration after "default" in
main()
Remove the
void
in front of the function call.Types for parameters not specified in declaration for
Acknowledge_Call
Change to:
void Acknowledge_Call(char digit1, char digit2, char digit3, char digit4, char digit5, char digit6, char digit7, char digit8)
Remove the
int
in front ofRead_dials
in themain
function.In
main()
, change 'Read_dialsto
Read_Dials:
!=The C++ language is case-sensitive, thus 'dials
Dials
!=dIaLs
.Remove
break
from afterif
inmain()
function:You want
return 1;
orreturn EXIT_FAILURE;
orexit(1);
Remember to add these lines to
Acknowledge_Calls.cpp
:#include "acknowledge_call.h"
#include <iostream>
using namespace std;
Remember to add these lines to
Read_Dials.cpp
:#include "read_dials.h"
#include <iostream>
using namespace std;
In
Read_Dials.cpp
, move the#include "To_Digit.h
to the top of the file.When executing a function all, do not put the return type nor the parameter types in the call.
For example, use:
num1 = To_Digit(digit1);
instead of
int To_Digit(char &num1);
You need to have a long talk with your instructor about how to call functions and pass parameters (this time, listen carefully). Also, read a good book on C++.