personality test using functions
<p>Here is my problem: a professor of mine told us to do a personality test using <code>C++</code>
in which the program must have 20 question and each question must have 5 choices.</p>

<p>Those 5 choices are the Big 5 Personalities (openness, conscientiousness, extraversion, agreeableness, neuroticism). The first 2 questions belong to openness.</p>

<p>One question per function, I only put 2 for test run. When user input correct choice, the score should add up, but the program must have a choice for the user if the user wanna go back to the last question....</p>

<p>Now my problem is when the user wants to go back to the last question and input a correct choice, THE SUM WON'T ADD UP anymore. Please help, the code is there below :(</p>

<p>Here is what I did:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;stdlib.h&gt;
#include &lt;cmath&gt;
#include &lt;iomanip&gt;

using namespace std;

    int first_QS(int score){  // 1st function for 1st question

        //declaration of variables
        string choice;
        string ans;
        bool valid = false;
        bool invalid = false;

        do{

        invalid = false;

        do{
            system("cls"); // question
            cout &lt;&lt; "\tQuestion of 1/20\n" &lt;&lt; endl;
            cout &lt;&lt; "\t 1. I have a rich vocabulary..." &lt;&lt; endl;
            cout &lt;&lt; "\t             1 = disagree" &lt;&lt; endl; 
            cout &lt;&lt; "\t             2 = slightly disagree" &lt;&lt; endl;
            cout &lt;&lt; "\t             3 = neutral" &lt;&lt; endl;
            cout &lt;&lt; "\t             4 = slightly agree" &lt;&lt; endl;
            cout &lt;&lt; "\t             5 = agree" &lt;&lt; endl &lt;&lt; endl;

            cout &lt;&lt; "\tYou choosed: ";
            cin &gt;&gt; choice;  

            valid = false;
            // score board for 5 personalitites
            if(choice == "1"){
                score = score +1;
                valid = true;
            }

            else if (choice == "2"){
                score = score +2;
                valid = true;
            }

            else if (choice == "3"){
                score = score +3;
                    valid = true;
            }

            else if (choice == "4"){
                score = score +4;
                valid = true;
            }

            else if (choice == "5"){
                score = score +5;
                valid = true;
            }

            else{
                cout &lt;&lt; "\tInvalid input... Please enter only 1-5...going back\n";  // if user input wrong choice
                score = score +0;
                system("pause");
            }
        }
        while (!valid);

        cout &lt;&lt; "\n\tNext (yes/no): ";  
        cin &gt;&gt; ans; // ask the user to go next question or redo again


        if((ans == "yes") || (ans == "Yes") || (ans == "YES")){ // pass value to main
            invalid = true;
            return score;
        }

        else if((ans == "no") || (ans == "No") || (ans == "NO")){ // reset the value in this function only
            score -= score;
            cin.ignore();
        }

        else{
            cout &lt;&lt; "\tYou input invalid command... going back\n";
            score -= score;
            system("pause");
        }
    }
    while(!invalid);
    return 0;
}
int second_QS(int score){  // 2nd function for 2nd question

    //declaration of variables
    string choice;
    string ans;
    bool valid = false;
    bool invalid = false;

    do{
        invalid = false;

        do{
            system("cls"); // question
            cout &lt;&lt; "\tQuestion of 2/20\n" &lt;&lt; endl;
            cout &lt;&lt; "\t 2. I have a vivid imagination..." &lt;&lt; endl;
            cout &lt;&lt; "\t             1 = disagree" &lt;&lt; endl; 
            cout &lt;&lt; "\t             2 = slightly disagree" &lt;&lt; endl;
            cout &lt;&lt; "\t             3 = neutral" &lt;&lt; endl;
            cout &lt;&lt; "\t             4 = slightly agree" &lt;&lt; endl;
            cout &lt;&lt; "\t             5 = agree" &lt;&lt; endl &lt;&lt; endl;

            cout &lt;&lt; "\tYou choosed: ";
            cin &gt;&gt; choice;  

            valid = false;
            // score board for 5 personalitites
            if(choice == "1"){
                score = score +1;
                valid = true;
            }

            else if (choice == "2"){
                score = score +2;
                valid = true;
            }

            else if (choice == "3"){
                score = score +3;
                    valid = true;
            }

            else if (choice == "4"){
                score = score +4;
                valid = true;
            }

            else if (choice == "5"){
                score = score +5;
                valid = true;
            }

            else{
                cout &lt;&lt; "\tInvalid input... Please enter only 1-5...going back\n";  // if user input wrong choice
                score = score +0;
                system("pause");
            }
        }
        while (!valid);

        cout &lt;&lt; "\n\tBack or Next to last question (back/next): ";  
        cin &gt;&gt; ans; // ask the user to go next question 


        if((ans == "back") || (ans == "Back") || (ans == "BACK")){ // pass value back to 1st_QS
            score -= score;
            first_QS(score);
        }

        else if((ans == "next") || (ans == "Next") || (ans == "NEXT")){ // reset the value in this function only
            invalid = true;
            return score;
        }

        else{
            cout &lt;&lt; "\tYou input invalid command... going back\n";
            score -= score;
            system("pause");
        }
    }
    while(!invalid);
    return 0;
}

void userMenu(){
    system("cls");
    cout &lt;&lt; "\tThis is a free demo of personality test" &lt;&lt; endl &lt;&lt; endl;
    cout &lt;&lt; "\tInfo : This program is about knowing you personality based on" &lt;&lt; endl;
    cout &lt;&lt; "\t       the Big Five factors is called the Five Factor Model" &lt;&lt; endl; 
    cout &lt;&lt; "\t       and are the best accepted and most commonly used" &lt;&lt; endl; 
    cout &lt;&lt; "\t       scientific measure of personality and have been" &lt;&lt; endl; 
    cout &lt;&lt; "\t       extensively researched." &lt;&lt; endl &lt;&lt; endl;

    cout &lt;&lt; "\tHow  : Answer all the questions truthfully for better and" &lt;&lt; endl;
    cout &lt;&lt; "\t       accurate results. You must rate on how much you agree" &lt;&lt; endl;
    cout &lt;&lt; "\t       that it is true about you using a five point scale where:" &lt;&lt; endl;
    cout &lt;&lt; "\t             1 = disagree" &lt;&lt; endl; 
    cout &lt;&lt; "\t             2 = slightly disagree" &lt;&lt; endl;
    cout &lt;&lt; "\t             3 = neutral" &lt;&lt; endl;
    cout &lt;&lt; "\t             4 = slightly agree" &lt;&lt; endl;
    cout &lt;&lt; "\t             5 = agree" &lt;&lt; endl;
    cout &lt;&lt; "\t       The items should be responded to quickly without over" &lt;&lt; endl; 
    cout &lt;&lt; "\t       thinking. It should take most people no more than four" &lt;&lt; endl;  
    cout &lt;&lt; "\t       minutes to complete." &lt;&lt; endl &lt;&lt; endl &lt;&lt; endl; 
    cout &lt;&lt; "Want to start now (yes/no) ? : ";
}
int main()
{
    bool invalid = false;
    bool valid = false;
    string userChoice;
    string ans;
        int score1 = 0;
        int open1, open2;

    int total1;

    do{
        invalid = false;
        userMenu();
        cin &gt;&gt; userChoice;

        if ((userChoice == "yes") || (userChoice == "Yes") || (userChoice == "YES"))
        {   
            do{
                invalid = true;
                system("cls");
                cout &lt;&lt;"\n\n\t\tNOTE: Please enter the appropriate commands like yes"&lt;&lt; endl;
                cout &lt;&lt;"\t\t      or no and back or next. If you start you can't" &lt;&lt; endl;
                cout &lt;&lt;"\t\t      quit until you finish all the twenty (20)" &lt;&lt; endl;
                cout &lt;&lt;"\t\t      questions." &lt;&lt; endl;

                cout &lt;&lt;"\n\t\t      Do you want to quit (yes/no)?: ";
                cin &gt;&gt; ans;
                if((ans == "yes") || (ans == "Yes") || (ans == "YES")){ 
                    valid = true;
                    exit(0);
                }

                else if((ans == "no") || (ans == "No") || (ans == "NO")){
                    valid = true;
                    cout &lt;&lt; "\n\t\t      ...Starting personality test..." &lt;&lt; endl &lt;&lt; endl;
                    system("pause");

                    //function to evaluate the total sum of each personality (Opennesss)
                    open1 = first_QS(score1);
                    open2 = second_QS(open1);

                    total1 = open2;

                    system("cls");
                    cout &lt;&lt;"\n\tYour results are out.\n\n" &lt;&lt; endl;
                    cout &lt;&lt;"\tOpenness: " &lt;&lt; setw(15) &lt;&lt; total1 &lt;&lt; setw(5) &lt;&lt;" points" &lt;&lt; endl;
                    cout &lt;&lt;"\tConscientiousness: " &lt;&lt; setw(6) &lt;&lt; setw(5) &lt;&lt;" points" &lt;&lt; endl;
                    cout &lt;&lt;"\tExtraversion: "   &lt;&lt; setw(5) &lt;&lt;" points" &lt;&lt; endl;
                    cout &lt;&lt;"\tAgreeableness: "  &lt;&lt; setw(5) &lt;&lt;" points" &lt;&lt; endl;
                    cout &lt;&lt;"\tNeuroticism: "   &lt;&lt; setw(5) &lt;&lt;" points" &lt;&lt; endl;

                    cin.ignore();
                }

                else{
                    cout &lt;&lt; "\n\t\t      Invalid input... input only yes or no" &lt;&lt; endl;
                    system("pause");
                }

            }while(!valid);
        }

        else if ((userChoice == "no") || (userChoice == "No") || (userChoice == "NO")){
            invalid = true;
            cout &lt;&lt; "Exiting" &lt;&lt; endl;
            exit(0);
        }

        else{
            cout &lt;&lt; "Invalid input... input only yes or no" &lt;&lt; endl;
            system("pause");
        }   
    }while (!invalid);

cin.ignore();
}
</code></pre>
