2014-01-08 60 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