Question

I am looking to create a program that asks the user 5 different questions. Each time 5 questions have been answered, program will ask the user if they wish to enter a new set of answers.

What function would I use to re-run the questions, and what function to store and keep track of them?

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;
}
Was it helpful?

Solution

I would reccomend using vectors of strings to store your answers in stead of using arrays. For example your program could look like this:

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

I think this is the kind of thing you are going for. If you are not familiar with vectors, here is a decent tutorial/reference for you to look at. The advantage to this approach is that the vectors will store essentially as many answers as you want and you can access the just as if you were accessing an array.

As far as your issue with the while loop, it should work just fine the way I showed you above. Make sure you initialize your char that you are using to store the answer before you use it and you should be fine.

This gives you a method of storing all of your answers. I'm not sure what you are planning on doing with the answers once they are stored, but if you want to compare an answer to a previous answer all you need to know is which iteration of your while loop the answer that your looking for occurred on and which question that you are looking for and you can find your answer. For example:

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

I hope I could help and good luck!

OTHER TIPS

Move the for loops out of all the questions.

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

Please note that the loop index could be only [0, 10) instead of [0, 11) because Q1, Q2, Q3 and Q4 are of size 10.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top