返回到代码块的开头 我对C++是相当新的,但是我在编写一个简单的程序,我如何返回代码的开始,同时仍然记住输入的内容。例如,假设我按下1,然后输入名称,我将如何返回到主要部分,其中它会询问您想要什么。谢谢你抽出时间我很感激 #include <iostream> #include <string> #include <iomanip> using namespace std; int main(int argc, char *argv[]) { char name[25]; char address[25]; char city[25]; char state[25]; char zip[25]; char phone[25]; int reply; cout <<"Press 1 to enter the name"<<endl; cout <<"Press 2 to enter the address"<<endl; cout <<"Press 3 to enter the city"<<endl; cout <<"Press 4 to enter the state"<<endl; cout <<"Press 5 to enter the zip"<<endl; cout <<"Press 6 to enter the phone"<<endl; cin >>reply; if (reply = 'one') { cout << " Enter the name" << endl; cin >> name; cin.ignore(80, '\n');} else if (reply = 'two') {cout << " Enter the address" << endl; cin >> address; cin.ignore(80, '\n');} else if (reply = 'three') {cout << " Enter the city" << endl; cin >> city; cin.ignore(80, '\n');} else if (reply = 'four') {cout << " Enter the state" << endl; cin >> state; cin.ignore(80, '\n');} else if (reply = 'five') { cout << " Enter the zip code " << endl; cin >> zip; cin.ignore(80, '\n');} else if (reply = 'six') { cout << " Enter the phone number " << endl; cin >> phone; cin.ignore(80, '\n');} else {cout << " done";} system ("PAUSE"); return EXIT_SUCCESS; #包括 #包括 #包括 使用名称空间std; int main(int argc,char*argv[]) { 字符名[25]; 字符地址[25]; 查尔城[25]; 半焦态[25]; char-zip[25]; 字符电话[25]; int回复; 请注意此处的条件: int reply; cin >>reply; if (reply = 'one') ...

返回到代码块的开头 我对C++是相当新的,但是我在编写一个简单的程序,我如何返回代码的开始,同时仍然记住输入的内容。例如,假设我按下1,然后输入名称,我将如何返回到主要部分,其中它会询问您想要什么。谢谢你抽出时间我很感激 #include <iostream> #include <string> #include <iomanip> using namespace std; int main(int argc, char *argv[]) { char name[25]; char address[25]; char city[25]; char state[25]; char zip[25]; char phone[25]; int reply; cout <<"Press 1 to enter the name"<<endl; cout <<"Press 2 to enter the address"<<endl; cout <<"Press 3 to enter the city"<<endl; cout <<"Press 4 to enter the state"<<endl; cout <<"Press 5 to enter the zip"<<endl; cout <<"Press 6 to enter the phone"<<endl; cin >>reply; if (reply = 'one') { cout << " Enter the name" << endl; cin >> name; cin.ignore(80, '\n');} else if (reply = 'two') {cout << " Enter the address" << endl; cin >> address; cin.ignore(80, '\n');} else if (reply = 'three') {cout << " Enter the city" << endl; cin >> city; cin.ignore(80, '\n');} else if (reply = 'four') {cout << " Enter the state" << endl; cin >> state; cin.ignore(80, '\n');} else if (reply = 'five') { cout << " Enter the zip code " << endl; cin >> zip; cin.ignore(80, '\n');} else if (reply = 'six') { cout << " Enter the phone number " << endl; cin >> phone; cin.ignore(80, '\n');} else {cout << " done";} system ("PAUSE"); return EXIT_SUCCESS; #包括 #包括 #包括 使用名称空间std; int main(int argc,char*argv[]) { 字符名[25]; 字符地址[25]; 查尔城[25]; 半焦态[25]; char-zip[25]; 字符电话[25]; int回复; 请注意此处的条件: int reply; cin >>reply; if (reply = 'one') ...,c++,loops,input,io,C++,Loops,Input,Io,实际上是为了: if (reply == 1) 放置在''之间的文字是指单个字符,对于您应该使用“”的字符串,对于数字文字,例如int,只需使用数字即可。您还应该添加一个选项来停止程序: cout << "Press 1 to enter the name" << endl; cout << "Press 2 to enter the address" << endl; ... cout << "Press 0 to stop" &

实际上是为了:

if (reply == 1)
放置在
'
'
之间的文字是指单个
字符
,对于您应该使用
的字符串,对于数字文字,例如
int
,只需使用数字即可。您还应该添加一个选项来停止程序:

cout << "Press 1 to enter the name" << endl; 
cout << "Press 2 to enter the address" << endl;
...
cout << "Press 0 to stop" << endl;                // <-- something like this

请注意,此处的条件:

int reply;
cin >>reply;
if (reply = 'one')
    ...
实际上是为了:

if (reply == 1)
放置在
'
'
之间的文字是指单个
字符
,对于您应该使用
的字符串,对于数字文字,例如
int
,只需使用数字即可。您还应该添加一个选项来停止程序:

cout << "Press 1 to enter the name" << endl; 
cout << "Press 2 to enter the address" << endl;
...
cout << "Press 0 to stop" << endl;                // <-- something like this

你想要的是一个循环!不要相信GOTO和Labor的谎言!使用适当的结构化代码,你总是可以避免使用这些维护噩梦。

bool doContinue = true;
int userNumber = 0;

while( doContinue )
{
    int temp = 0;

    cout << "What's your next favorite number?"
    cin >> temp;

    userNumber += temp;

    cout << "The sum of your favorite numbers is " << userNumber << endl;

    cout << "Continue?" << endl;
    cin >> temp;

    doContinue = temp != 0;
}
bool doContinue=true;
int userNumber=0;
while(doContinue)
{
内部温度=0;
温度;
userNumber+=temp;

CUT< P>你想要的是一个循环!不要相信Goto和Lable的谎言!使用适当的结构化代码,你总是可以避免使用这些维护噩梦。
bool doContinue = true;
int userNumber = 0;

while( doContinue )
{
    int temp = 0;

    cout << "What's your next favorite number?"
    cin >> temp;

    userNumber += temp;

    cout << "The sum of your favorite numbers is " << userNumber << endl;

    cout << "Continue?" << endl;
    cin >> temp;

    doContinue = temp != 0;
}
bool doContinue=true;
int userNumber=0;
while(doContinue)
{
内部温度=0;
温度;
userNumber+=temp;

你可能想读一读关于循环的书。=而不是==是有史以来最著名的逻辑错误,首先检查:)我想在你给代码添加循环之前(这就是你问题的答案)你应该解决你已经遇到的问题。在糟糕的代码中添加更多的代码是没有意义的。请查看LihO的答案。你可能想了解循环。=而不是==是有史以来最著名的逻辑错误,请首先检查:)我想在你向代码中添加循环之前(这是你问题的答案)你应该解决你已经遇到的问题。在糟糕的代码中添加更多的代码是没有意义的。看看LihO的答案。谢谢大家,祝你们有一个美好的一天我很感激Hanks,祝你们有一个美好的一天我很感激