C++ C+中重复语句的问题+;
(注:这是问题的更新版本) 所以我正在升级我以前工作过的项目。 现在,它将程序的输出打印到一个文件中,并组织成绩。 到目前为止,除了一个错误外,一切都应该正常工作。 代码如下:C++ C+中重复语句的问题+;,c++,visual-c++,visual-studio-2012,C++,Visual C++,Visual Studio 2012,(注:这是问题的更新版本) 所以我正在升级我以前工作过的项目。 现在,它将程序的输出打印到一个文件中,并组织成绩。 到目前为止,除了一个错误外,一切都应该正常工作。 代码如下: #include <iostream> #include <string> #include <fstream> using namespace std; struct Classes { double acousticGuitarEnsemble; double b
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct Classes
{
double acousticGuitarEnsemble;
double biology;
double english;
double geometry;
double hebrew;
double jewishStudies;
double worldHistory;
};
void grade1(Classes eitan)
{
newGrade1:
cout << "Which grade are you entering? (Use the following format: exampleFormat): ";
string classBeingEntered1;
cin >> classBeingEntered1;
cout << "Enter the new grade: ";
double grade1;
cin >> grade1;
cout << "Grade entered." << endl;
if (classBeingEntered1 == "acousticGuitarEnsemble")
eitan.acousticGuitarEnsemble = grade1;
else if (classBeingEntered1 == "biology")
eitan.biology = grade1;
else if (classBeingEntered1 == "english")
eitan.english = grade1;
else if (classBeingEntered1 == "jewishStudies")
eitan.jewishStudies = grade1;
else if (classBeingEntered1 == "geometry")
eitan.geometry = grade1;
else if (classBeingEntered1 == "hebrew")
eitan.hebrew = grade1;
else if (classBeingEntered1 == "worldHistory")
eitan.worldHistory = grade1;
else
cout << "Invalid class name. Try again." << endl;
goto newGrade1;
}
void grade2(Classes eitan)
{
newGrade2:
cout << "Which grade are you entering? (Use the following format: exampleFormat): ";
string classBeingEntered2;
cin >> classBeingEntered2;
cout << "Enter the new grade: ";
double grade2;
cin >> grade2;
cout << "Grade entered." << endl;
if (classBeingEntered2 == "acousticGuitarEnsemble")
eitan.acousticGuitarEnsemble = grade2;
else if (classBeingEntered2 == "biology")
eitan.biology = grade2;
else if (classBeingEntered2 == "english")
eitan.english = grade2;
else if (classBeingEntered2 == "jewishStudies")
eitan.jewishStudies = grade2;
else if (classBeingEntered2 == "geometry")
eitan.geometry = grade2;
else if (classBeingEntered2 == "hebrew")
eitan.hebrew = grade2;
else if (classBeingEntered2 == "worldHistory")
eitan.worldHistory = grade2;
else
cout << "Invalid class name. Try again." << endl;
goto newGrade2;
}
void grade3(Classes eitan)
{
newGrade3:
cout << "Which grade are you entering? (Use the following format: exampleFormat): ";
string classBeingEntered3;
cin >> classBeingEntered3;
cout << "Enter the new grade: ";
double grade3;
cin >> grade3;
cout << "Grade entered." << endl;
if (classBeingEntered3 == "acousticGuitarEnsemble")
eitan.acousticGuitarEnsemble = grade3;
else if (classBeingEntered3 == "biology")
eitan.biology = grade3;
else if (classBeingEntered3 == "english")
eitan.english = grade3;
else if (classBeingEntered3 == "jewishStudies")
eitan.jewishStudies = grade3;
else if (classBeingEntered3 == "geometry")
eitan.geometry = grade3;
else if (classBeingEntered3 == "hebrew")
eitan.hebrew = grade3;
else if (classBeingEntered3 == "worldHistory")
eitan.worldHistory = grade3;
else
cout << "Invalid class name. Try again." << endl;
goto newGrade3;
}
void grade4(Classes eitan)
{
newGrade4:
cout << "Which grade are you entering? (Use the following format: exampleFormat): ";
string classBeingEntered4;
cin >> classBeingEntered4;
cout << "Enter the new grade: ";
double grade4;
cin >> grade4;
cout << "Grade entered." << endl;
if (classBeingEntered4 == "acousticGuitarEnsemble")
eitan.acousticGuitarEnsemble = grade4;
else if (classBeingEntered4 == "biology")
eitan.biology = grade4;
else if (classBeingEntered4 == "english")
eitan.english = grade4;
else if (classBeingEntered4 == "jewishStudies")
eitan.jewishStudies = grade4;
else if (classBeingEntered4 == "geometry")
eitan.geometry = grade4;
else if (classBeingEntered4 == "hebrew")
eitan.hebrew = grade4;
else if (classBeingEntered4 == "worldHistory")
eitan.worldHistory = grade4;
else
cout << "Invalid class name. Try again." << endl;
goto newGrade4;
}
void grade5(Classes eitan)
{
newGrade5:
cout << "Which grade are you entering? (Use the following format: exampleFormat): ";
string classBeingEntered5;
cin >> classBeingEntered5;
cout << "Enter the new grade: ";
double grade5;
cin >> grade5;
cout << "Grade entered." << endl;
if (classBeingEntered5 == "acousticGuitarEnsemble")
eitan.acousticGuitarEnsemble = grade5;
else if (classBeingEntered5 == "biology")
eitan.biology = grade5;
else if (classBeingEntered5 == "english")
eitan.english = grade5;
else if (classBeingEntered5 == "jewishStudies")
eitan.jewishStudies = grade5;
else if (classBeingEntered5 == "geometry")
eitan.geometry = grade5;
else if (classBeingEntered5 == "hebrew")
eitan.hebrew = grade5;
else if (classBeingEntered5 == "worldHistory")
eitan.worldHistory = grade5;
else
cout << "Invalid class name. Try again." << endl;
goto newGrade5;
}
void grade6(Classes eitan)
{
newGrade6:
cout << "Which grade are you entering? (Use the following format: exampleFormat): ";
string classBeingEntered6;
cin >> classBeingEntered6;
cout << "Enter the new grade: ";
double grade6;
cin >> grade6;
cout << "Grade entered." << endl;
if (classBeingEntered6 == "acousticGuitarEnsemble")
eitan.acousticGuitarEnsemble = grade6;
else if (classBeingEntered6 == "biology")
eitan.biology = grade6;
else if (classBeingEntered6 == "english")
eitan.english = grade6;
else if (classBeingEntered6 == "jewishStudies")
eitan.jewishStudies = grade6;
else if (classBeingEntered6 == "geometry")
eitan.geometry = grade6;
else if (classBeingEntered6 == "hebrew")
eitan.hebrew = grade6;
else if (classBeingEntered6 == "worldHistory")
eitan.worldHistory = grade6;
else
cout << "Invalid class name. Try again." << endl;
goto newGrade6;
}
void grade7(Classes eitan)
{
newGrade7:
cout << "Which grade are you entering? (Use the following format: exampleFormat): ";
string classBeingEntered7;
cin >> classBeingEntered7;
cout << "Enter the new grade: ";
double grade7;
cin >> grade7;
cout << "Grade entered." << endl;
if (classBeingEntered7 == "acousticGuitarEnsemble")
eitan.acousticGuitarEnsemble = grade7;
else if (classBeingEntered7 == "biology")
eitan.biology = grade7;
else if (classBeingEntered7 == "english")
eitan.english = grade7;
else if (classBeingEntered7 == "jewishStudies")
eitan.jewishStudies = grade7;
else if (classBeingEntered7 == "geometry")
eitan.geometry = grade7;
else if (classBeingEntered7 == "hebrew")
eitan.hebrew = grade7;
else if (classBeingEntered7 == "worldHistory")
eitan.worldHistory = grade7;
else
cout << "Invalid class name. Try again." << endl;
goto newGrade7;
}
Classes gradeEditor(Classes eitan)
{
cout << "GradeBook 1.2" << endl;
grade1(eitan);
cout << "Editing next grade..." << endl;
grade2(eitan);
cout << "Editing next grade..." << endl;
grade3(eitan);
cout << "Editing next grade..." << endl;
grade4(eitan);
cout << "Editing next grade..." << endl;
grade5(eitan);
cout << "Editing next grade..." << endl;
grade6(eitan);
cout << "Entering next grade..." << endl;
grade7(eitan);
return eitan;
}
void choice(Classes eitan)
{
choiceBack:
cout << "Do you want to edit another grade? (If yes, you must reset all the grades) Press Y or N: ";
char chChoice;
cin >> chChoice;
switch (chChoice) {
case 'Y':
cout << "Alright then!" << endl;
do {
gradeEditor(eitan);
goto choiceBack; // Refers to beginning of choice() function
} while (chChoice == 'Y');
case 'N':
cout << "Printing grades..." << endl;
break;
case 'y':
cout << "Alright then!" << endl;
do {
gradeEditor(eitan);
goto choiceBack; // Refers to beginning of choice() function
} while (chChoice == 'y');
case 'n':
cout << "Printing grades..." << endl;
break;
}
}
void printGrades(Classes eitan)
{
cout << "Acoustic Guitar Ensemble: " << eitan.acousticGuitarEnsemble << endl;
cout << "Biology: " << eitan.biology << endl;
cout << "English: " << eitan.english << endl;
cout << "Jewish Studies: " << eitan.jewishStudies << endl;
cout << "Geometry: " << eitan.geometry << endl;
cout << "Hebrew: " << eitan.hebrew << endl;
cout << "World History: " << eitan.worldHistory << endl;
system("PAUSE");
}
int main(Classes eitan)
{
ofstream outputFile;
outputFile.open("userGrades.txt");
gradeEditor(eitan);
choice(eitan);
printGrades(eitan);
outputFile.close();
cout << "Grades saved to file!" << endl;
return 0;
}
#包括
#包括
#包括
使用名称空间std;
结构类
{
双声吉他;
双重生物学;
双英语;
双几何;
双希伯来语;
双重犹太研究;
双重世界历史;
};
无效等级1(等级eitan)
{
新一年级:
cout>classBeingEntered1;
cout>1级;
cout错误消息非常清楚。问题不在于return
关键字。问题在于试图使用变量而不初始化它
Classes eitan;
//several lines of code in which you do nothing with eitan, then:
return eitan;
如果您尝试执行return
之外的其他操作,则会遇到相同的错误消息
如果需要使用main
中声明的eitan
,则将其作为变量传递
Classes gradeEditor(Classes& c) {
//stuff
return c;
}
这样称呼它:
gradeEditor(eitan);
并对所有void
函数执行相同的操作
如果不希望传递变量,但希望在多个函数中使用它,则必须将其声明为全局变量。仅创建一个在不同范围内具有相同名称的新变量并不能使两个变量指向相同的信息
您被困在无限循环中,因为:
else
cout << "Invalid class name. Try again." << endl;
goto newGrade2;
else
考虑到这一点,您应该重写您的代码并删除所有转到
糟糕的做法——至少在你正在做的节目中是这样
您可以像这样重写选项
函数,还要注意
类
通过引用传递,允许它进行修改
通过引用,您可以修改它,这是您应该做的
在修改它的所有函数中执行,否则更改将被忽略
由于要向函数传递副本,请保持本地
#include <cctype> // for std::toupper
void choice(Classes& eitan) // pass by reference to modify argument
{
do
{
cout << "Do you want to edit another grade? (If yes, you must reset all the grades) Press Y or N: ";
char chChoice;
cin >> chChoice;
chChoice = std::toupper(chChoice);
switch (chChoice)
{
case 'Y':
cout << "Alright then!" << endl;
gradeEditor(eitan);
break;
case 'N':
cout << "Printing grades..." << endl;
break;
default:
cout << "Please answer Y or N!" << endl;
break;
}
}
while ( chChoice != 'N' );
}
#包含//用于std::toupper
void choice(Classes&eitan)//通过引用传递修改参数
{
做
{
cout>chChoice;
chChoice=std::toupper(chChoice);
开关(chChoice)
{
案例“Y”:
难道那些goto
s应该是循环吗?在返回它之前,你没有接触过eitan
。你一直在声明局部变量并将它们扔掉。你一次又一次地声明局部变量,却从不返回它们。例如,在gradeEditor中,你声明eitan
,永远不要对它做任何事情(在您调用的每个函数中,它们都有自己的副本,它们也会丢弃这些副本),然后尝试返回该副本。首先,不要使用grade 1到grade 7,而是使用grade 1方法:grade(Classes&class)
,而不是声明一个局部变量,使用传入的类
。编辑后仍然中断。您需要传递一个引用,否则您正在编辑传递的变量的副本,并且您的goto
循环似乎没有结束。这是每个人都缺少的。我确实使用类eitan,因为我是editi在每个函数中在结构中定义变量。我只是将其划分为单独的函数,以便更好地组织。什么?谁缺少什么?@DJHead On,您正在编辑不同对象中的变量。如果具有相同名称且在不同函数中局部定义的对象实际上都是相同的对象,则代码将变得非常不匹配ntainable.@chris我一开始确实是这样的,但我已经修复了它(如果我理解您的意思)。@chris的可维护性不亚于7个函数,它们都做相同的事情或goto:
statements…;)std::toupper将做什么来帮助您?它的语法到底是什么?
#include <cctype> // for std::toupper
void choice(Classes& eitan) // pass by reference to modify argument
{
do
{
cout << "Do you want to edit another grade? (If yes, you must reset all the grades) Press Y or N: ";
char chChoice;
cin >> chChoice;
chChoice = std::toupper(chChoice);
switch (chChoice)
{
case 'Y':
cout << "Alright then!" << endl;
gradeEditor(eitan);
break;
case 'N':
cout << "Printing grades..." << endl;
break;
default:
cout << "Please answer Y or N!" << endl;
break;
}
}
while ( chChoice != 'N' );
}