2

I'm a foundation student who starting to learn coding C++. I'm doing a survey program for my college assignment and after the testing, i found out that the sum value from the sub function cannot sum up properly to the value in main function. any one HELP!!!

here is the code:

#include <iostream>

using namespace std;

int Q1();
int Q2();
int Q3();
int Q4();
int Q5();

int main()
{
    char select;
    int E, A, C, N, O;
    int extroversion=0, agreeableness=0, conscientiousness=0, neuroticism=0,          opennesstoexperience=0;

    cout << "This is a Self-Esteem Questionnaire." << endl;
    cout << "\nInsert S to start the test (Q to Quit): ";
    cin >> select;
    select=toupper(select);

    if (select=='S')
    {

    cout << "------------------INSTRUCTIONS-----------------" << endl;
    cout << "For each statement 1-50 mark how much you agree" << endl;
    cout << "with on the scale 1-5, where                   " << endl;
    cout << "1=disagree, 2=slightly disagree, 3=neutral,    " << endl;
    cout << "4=slightly agree and 5=agree                   " << endl;
    cout << "-----------------------------------------------" << endl;

    cout << Q1() << endl;
    extroversion+=E;

    cout << Q2() << endl;
    agreeableness+=A;

    cout << Q3() << endl;
    conscientiousness+=C;

    cout << Q4() << endl;
    neuroticism+=N;

    cout << Q5() << endl;
    opennesstoexperience+=O;

    cout << extroversion << endl;
    cout << agreeableness << endl;
    cout << conscientiousness << endl;
    cout << neuroticism << endl;
    cout << opennesstoexperience << endl;

    }

    else
        if(select=='Q')
   {
        cout << "Program quit!" << endl;
    }
    return 0;
}

int Q1()
{
    int E=0;

    cout << "I am the life of the party." << endl;
    cout << "1=disagree, 2=slightly disagree, 3=neutral," << endl;
    cout << "4=slightly agree and 5=agree" << endl;
    cout << "\nRating: ";
    cin >> E;

    return E;

}

int Q2()
{
    int A=0;

    cout << "I feel little concern for others." << endl;
    cout << "1=disagree, 2=slightly disagree, 3=neutral," << endl;
    cout << "4=slightly agree and 5=agree" << endl;
    cout << "\nRating: ";
    cin >> A;

    return A;

}

int Q3()
{
    int C=0;

    cout << "I am always prepared." << endl;
    cout << "1=disagree, 2=slightly disagree, 3=neutral," << endl;
    cout << "4=slightly agree and 5=agree" << endl;
    cout << "\nRating: ";
    cin >> C;

    return C;

}

int Q4()
{
    int N=0;

    cout << "I get stressed out easily." << endl;
    cout << "1=disagree, 2=slightly disagree, 3=neutral," << endl;
    cout << "4=slightly agree and 5=agree" << endl;
    cout << "\nRating: ";
    cin >> N;

    return N;

}

int Q5()
{
    int O=0;

    cout << "I have a rich vocabulary." << endl;
    cout << "1=disagree, 2=slightly disagree, 3=neutral," << endl;
    cout << "4=slightly agree and 5=agree" << endl;
    cout << "\nRating: ";
    cin >> O;

    return O;

 }`
Simon Kraemer
  • 5,700
  • 1
  • 19
  • 49

3 Answers3

5

Let's start by reducing this problem to its essentials.

int main()
{
    int E;
    int extroversion=0;

    cout << Q1() << endl;
    extroversion+=E;

    cout << extroversion << endl;
    return 0;
}

int Q1()
{
    int E=0;
    cout << "Give me a number" << endl;
    cin >> E;
    return E;
}

The problem is that the variable E you have declared in Q1, has nothing to do with the variable E you have declared in main. Therefor when you write:

    extroversion+=E;

you are using an uninitialized variable. The solution is to rewrite main as:

int main()
{
    int extroversion=0;

    int E = Q1();     // Capture the result of Q1 in E
    cout << E << endl;// ... *then* print it.
    extroversion+=E;  // And now you can use the value of E in this function.

    cout << extroversion << endl;
    return 0;
}

Please note: That "reduced" problem is what you should have posted in the first place - we don't need to see the masses of text, and we certainly don't need to see you do the same thing five times. Remove the verbiage (and check that you still have the problem), and then post the reduced problem.

2

You're using a truck load of uninitialised variables in main. You are returning values back from the various Q functions, which are being used by the cout calls, but you are not setting E, A, C, N, O in main. Formally this means that the behaviour of your program is undefined.

Replace

cout << Q1() << endl;

with

cout << (E = Q1()) << endl;

and so on, and all will be well. See for yourself by using your line by line debugger.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

Let's start from the beginning (which I think your might want to do to learn C/C++, perhaps by running through one of the many online C++ tutorials). I think your main focus should be on the following (in no specific order):

Initialization: Primitive data types like int, char, etc. are called Plain Old Datatypes (POD). In all the cases you are using in your code, they are not initialized, following the rule "don't pay for what you don't use." See What are POD types in C++? for more info.

Scope: Variables exist within a region of a program defined by where they are declared. For example, in main() you declare int E; (without initializing it so it simply acquires the value of whatever was in memory at that location). The scope of this E in main() starts where it is declared at ends at the end of main() (its scope is the function main).. Now we go to your function int Q1() in which you declare int E = 0;. At that point in the function this E comes into existence where it is declared and then goes out of existence at the end of Q1 (its scope is the function Q1). When you set E in Q1 that variable (and its value) disappears when you exit Q1, and so back in main when you add E, you are adding the E declared in main which just has a random value. Thus the E you used in main

 extroversion+=E;

is not the same E you set in Q1

 cin >> E;

They are different variables.

See https://www.tutorialspoint.com/cplusplus/cpp_variable_scope.htm for more info.

Functions: You are calling functions which return a value, but you don't use that returned value. I think that your misunderstanding of the scope of variables and the concept of a function is the root of your difficulties. Frex, in Q1() you return the value of E, which is indeed what you want. You just didn't know how to use it. In this instance, I would replace

cout << Q1() << endl;
    extroversion+=E;

with

extroversion = Q1();

See http://www.cplusplus.com/doc/tutorial/functions/ for more info.

There are many issues with your code above that I think could be rectified by visiting some online tutorials like those I have referenced. I could rewrite your code but I think that (1) it should probably work with the suggestions I have given here and (2) you will learn more if you rewrite it yourself! :-) It also appears to me that you may have just kept adding things in order to get your code to compile that are not necessary. I might suggest that you go through each line and ensure that you understand why it is there (and why it is where it is).

Community
  • 1
  • 1
Jon Spencer
  • 569
  • 5
  • 8