C++ 重复一组输入,存储并跟踪每个输入。C++;
我希望创建一个程序,向用户提出5个不同的问题。每次回答5个问题时,程序将询问用户是否希望输入一组新的答案 我将使用什么函数来重新运行问题,以及使用什么函数来存储和跟踪问题C++ 重复一组输入,存储并跟踪每个输入。C++;,c++,loops,while-loop,store,repeat,C++,Loops,While Loop,Store,Repeat,我希望创建一个程序,向用户提出5个不同的问题。每次回答5个问题时,程序将询问用户是否希望输入一组新的答案 我将使用什么函数来重新运行问题,以及使用什么函数来存储和跟踪问题 string Q1[10]; string Q2[10]; int Q3[10]; int Q4[10]; char newEntry; do{ for(int i=0; i<11; i++){ cout << "Question 1: " << endl;
string Q1[10];
string Q2[10];
int Q3[10];
int Q4[10];
char newEntry;
do{
for(int i=0; i<11; i++){
cout << "Question 1: " << endl;
cin >> Q1[i];
}
for(int i=0; i<11; i++){
cout << endl << endl << "Question 2: " << endl;
cin >> Q2[i];
}
for(int i=0; i<11; i++){
cout << endl << endl << "Question 3: " << endl;
cin >> Q3[i];
}
for(int i=0; i<11; i++){
cout << endl << endl << "Question 4: " << endl;
cin >> Q4[i];
}
cout << "Would you like to repeat? Enter either 'y' or 'n': " << endl;
cin >> newEntry;
}while (newEntry=='y');
system("pause");
return 0;
}
字符串Q1[10];
字符串Q2[10];
int Q3[10];
int Q4[10];
charnewentry;
做{
for(int i=0;i将for
循环从所有问题中移出
for(int i=0; i<10; i++){
cout << "Question 1: " << endl;
cin >> Q1[i];
cout << endl << endl << "Question 2: " << endl;
cin >> Q2[i];
cout << endl << endl << "Question 3: " << endl;
cin >> Q3[i];
cout << endl << endl << "Question 4: " << endl;
cin >> Q4[i];
cout << "Would you like to repeat? Enter either 'y' or 'n': " << endl;
cin >> newEntry;
if (newEntry != 'y')
break;
}
for(int i=0;i我会使用字符串向量来记录答案,而不是使用数组。
例如,您的程序可能如下所示:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(int argc, const char * argv[])
{
vector<string> Q1;
vector<string> Q2;
vector<string> Q3;
vector<string> Q4;
vector<string> Q5;
char newEntry = '\0';
string temp;
do {
cout<<"Question 1: "<<endl;
cin>>temp;
Q1.push_back(temp);
temp.clear();
cout<<"Question 2: "<<endl;
cin>>temp;
Q2.push_back(temp);
temp.clear();
cout<<"Question 3: "<<endl;
cin>>temp;
Q3.push_back(temp);
temp.clear();
cout<<"Question 4: "<<endl;
cin>>temp;
Q4.push_back(temp);
temp.clear();
cout<<"Question 5: "<<endl;
cin>>temp;
Q5.push_back(temp);
temp.clear();
cout << "Would you like to repeat? Enter either 'y' or 'n': " << endl;
cin >> newEntry;
} while (newEntry=='y');
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main(int argc,const char*argv[]
{
向量Q1;
向量Q2;
向量Q3;
向量Q4;
向量Q5;
char newEntry='\0';
字符串温度;
做{
coutYes我知道它只会按规定重复10次。它现在似乎工作正常,但是当我告诉程序停止,在重复问题中输入“n”时,它会继续进行,有什么需要补充的吗?非常感谢!!@noobuser333编辑了代码。删除了do…while
循环。
int index = 0;//i want the answer from the fisrt occurance of the loop.
string answer = Q1[index];//i want the answer to Question 1 from loop occurance 1
index=1;//i want the answer from the second occurance of the loop.
string answer2 = Q1[index];//i want the answer to Question 1 from loop occurance 2
if (answer==answer2) {
cout<<"Answers were the same";
}
else cout<<"Answers were not the same";