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.
这个程序应该读取一个电话号码并打印出来。如果它提供了字母,那么它将把它排序为数字0-9,就像在电话键盘上一样,在它变成大写字母之后。它还将返回由switch语句控制的无效字符等的错误代码。
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?
这段代码有什么问题吗?我怎样才能修好它呢?
4 个解决方案
#1
2
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.
我还没有试着自己运行代码,但是我能看到的唯一让我觉得奇怪的是“Read_Dials”函数……不要把#INCLUDE语句放在函数中。总是将那些语句放在文件的顶部。
Move the #include and let us know what that does. Good luck.
移动#include,让我们知道它的作用。祝你好运。
#2
2
default:
void Acknowledge_Call(digit1, digit2, digit3, digit4, digit5, digit6, digit7, digit8);
Return type is not used while calling a function. So, drop void
. Also default
case should have a break
, else it will fall-through.
调用函数时不使用返回类型。因此,减少无效。另外,默认情况应该有一个中断,否则它将会失效。
int Read_Dials(char &num1, char &num2, char &num3, char &num4, char &num5, char &num6, char &num7, char &num8)
{
#include "To_Digit.h" // The header actually has a definition. Preprocessor
// copies the content of To_Digit.h here. So, you have
// a function definition inside another function while
// compilation phase which is not allowed. So remove
// it and place it at top of the file.
// .....
}
#3
2
Your #includes should be at the top of the file...
你的#include应该在文件的顶部……
if (return_value != -5)
break;
(in main) is not allowed as there is no loop for it to break out of
(在main中)是不允许的,因为它没有可以跳出的循环。
#4
0
Here are some issues (besides the fact you didn't supply header files):
这里有一些问题(除了你没有提供头文件):
-
Function declaration after "default" in
main()
Remove thevoid
in front of the function call.在main()中“default”后的函数声明删除函数调用前的空。
-
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)
类型的参数没有指定的声明为:void dge_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.在主函数的Read_dials前面删除int。
-
In
main()
, change 'Read_dialsto
Read_Dials:
!=
The C++ language is case-sensitive, thus 'dialsDials
!=dIaLs
.在main()中,更改'Read_dialstoRead_Dials: c++语言是大小写敏感的,因此'dials != dials != dials !
-
Remove
break
from afterif
inmain()
function:
You wantreturn 1;
orreturn EXIT_FAILURE;
orexit(1);
在main()函数中删除if后的中断:要返回1;或返回EXIT_FAILURE;或退出(1);
-
Remember to add these lines to
Acknowledge_Calls.cpp
:请记住将这些行添加到claidge_calls.cpp:
#include "acknowledge_call.h"
#include <iostream>
using namespace std;
# include“acknowledge_call。h" #包含
使用名称空间std; -
Remember to add these lines to
Read_Dials.cpp
:请记住将这些行添加到Read_Dials.cpp:
#include "read_dials.h"
#include <iostream>
using namespace std;
# include“read_dials。h" #包含
使用名称空间std; -
In
Read_Dials.cpp
, move the#include "To_Digit.h
to the top of the file.在Read_Dials。cpp,移动#include“To_Digit”。h在文件的顶部。
-
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 ofint To_Digit(char &num1);
当全部执行一个函数时,不要在调用中放入返回类型或参数类型。例如,use: num1 = To_Digit(digit1);而不是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++.
你需要和你的老师就如何调用函数和参数(这一次,仔细听)进行一次长谈。另外,阅读一本关于c++的好书。
#1
2
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.
我还没有试着自己运行代码,但是我能看到的唯一让我觉得奇怪的是“Read_Dials”函数……不要把#INCLUDE语句放在函数中。总是将那些语句放在文件的顶部。
Move the #include and let us know what that does. Good luck.
移动#include,让我们知道它的作用。祝你好运。
#2
2
default:
void Acknowledge_Call(digit1, digit2, digit3, digit4, digit5, digit6, digit7, digit8);
Return type is not used while calling a function. So, drop void
. Also default
case should have a break
, else it will fall-through.
调用函数时不使用返回类型。因此,减少无效。另外,默认情况应该有一个中断,否则它将会失效。
int Read_Dials(char &num1, char &num2, char &num3, char &num4, char &num5, char &num6, char &num7, char &num8)
{
#include "To_Digit.h" // The header actually has a definition. Preprocessor
// copies the content of To_Digit.h here. So, you have
// a function definition inside another function while
// compilation phase which is not allowed. So remove
// it and place it at top of the file.
// .....
}
#3
2
Your #includes should be at the top of the file...
你的#include应该在文件的顶部……
if (return_value != -5)
break;
(in main) is not allowed as there is no loop for it to break out of
(在main中)是不允许的,因为它没有可以跳出的循环。
#4
0
Here are some issues (besides the fact you didn't supply header files):
这里有一些问题(除了你没有提供头文件):
-
Function declaration after "default" in
main()
Remove thevoid
in front of the function call.在main()中“default”后的函数声明删除函数调用前的空。
-
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)
类型的参数没有指定的声明为:void dge_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.在主函数的Read_dials前面删除int。
-
In
main()
, change 'Read_dialsto
Read_Dials:
!=
The C++ language is case-sensitive, thus 'dialsDials
!=dIaLs
.在main()中,更改'Read_dialstoRead_Dials: c++语言是大小写敏感的,因此'dials != dials != dials !
-
Remove
break
from afterif
inmain()
function:
You wantreturn 1;
orreturn EXIT_FAILURE;
orexit(1);
在main()函数中删除if后的中断:要返回1;或返回EXIT_FAILURE;或退出(1);
-
Remember to add these lines to
Acknowledge_Calls.cpp
:请记住将这些行添加到claidge_calls.cpp:
#include "acknowledge_call.h"
#include <iostream>
using namespace std;
# include“acknowledge_call。h" #包含
使用名称空间std; -
Remember to add these lines to
Read_Dials.cpp
:请记住将这些行添加到Read_Dials.cpp:
#include "read_dials.h"
#include <iostream>
using namespace std;
# include“read_dials。h" #包含
使用名称空间std; -
In
Read_Dials.cpp
, move the#include "To_Digit.h
to the top of the file.在Read_Dials。cpp,移动#include“To_Digit”。h在文件的顶部。
-
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 ofint To_Digit(char &num1);
当全部执行一个函数时,不要在调用中放入返回类型或参数类型。例如,use: num1 = To_Digit(digit1);而不是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++.
你需要和你的老师就如何调用函数和参数(这一次,仔细听)进行一次长谈。另外,阅读一本关于c++的好书。