2014-01-08 58 views
-1

我期待创建一个程序,询问用户5个不同的问题。每回答5个问题,程序会询问用户是否希望输入一组新的答案。重复一组输入,存储和保存每个输入。 C++

我将使用什么函数来重新运行问题以及存储和跟踪它们的函数?

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; 
} 

回答

0

我会建议使用矢量的字符串来存储你的答案,而不是使用数组。 例如你的程序看起来是这样的:

#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; 
} 

我觉得这是你要的那种东西。 如果你不熟悉载体,here是一个体面的教程/参考供您查看。 这种方法的好处是,矢量将存储尽可能多的答案,并且您可以像访问数组一样访问这些答案。

就你在while循环中的问题而言,它应该以我向你展示的方式工作得很好。确保在使用之前初始化您用来存储答案的char,并且您应该没问题。

这给你一个存储所有答案的方法。我不确定你计划在存储答案时做什么,但是如果你想比较一个答案和以前的答案,你只需要知道你的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"; 

我希望我能帮上忙,祝你好运!

0

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; 
    } 

请注意,循环索引可能是唯一[0,10),而不是[0,11),因为Q1Q2Q3Q4是尺寸为10

+0

是的我知道它只会按照指定重复10次。现在看起来工作正常,但是当我告诉程序停止输入“n”来继续进行重复问题时,是否需要添加一些内容?非常感谢 !! – noobuser333

+0

@ noobuser333编辑代码。删除了'do ... while'循环。 – timrau