CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2012
    Posts
    1

    The output of the code seems inconsistent.

    The code below is for my project in my subject which is c++. The code works fine but it seems like the output is inconsistent and weird. Would you please help me think of a solution to this error? I tried to research and read books about this issue but still its not clear for me. Thank you in advance


    #include "stdafx.h"
    #include <iostream>
    using namespace std;

    int ctr, x, y, z;

    //Questions expressed as functions
    void q1(), q2(), q3(), q4(), q5(), q6(), q7(), q8(), q9(), q10(), q11(), q12(), q13(), q14(), q15(), q16(), q17(), q18(), q19();
    void count(); //Function that records the answers submitted

    int main ()
    {
    x = 0; y = 0; z = 0;
    cout << "**Happiness Test**" << endl << endl
    << "A series of conditions will be posted. State the degree of relevance: \n"
    << "1 - Never \n"
    << "2 - Often \n"
    << "3 - Always \n" << endl << endl;
    q1(); //Starts presenting questions
    q2();
    q3();
    q4();
    q5();
    q6();
    q7();
    q8();
    q9();
    q10();
    q11();
    q12();
    q13();
    q14();
    q15();
    q16();
    q17();
    q18();
    q19();

    if (x > y && y > z)
    cout << "You are sorrowful as of now.";
    else if (x > z && z > y)
    cout << "You are an unhappy person.";
    else if (y > x && x > z)
    cout << "You are pleased as a person.";
    else if (y > z && z > x)
    cout << "You are a cheerful person.";
    else if (z > x && x > y)
    cout << "You are a joyous person.";
    else
    cout << "You are definitely a happy person.";

    cin.ignore();cin.ignore(); //To avoid rapid application termination
    }

    //Function definitions
    void q1()
    {
    cout << "I smile everyday." << endl;
    do { //Re-evaluates if submitted number is out of bounds
    cin >> ctr;
    if (ctr < 1 || ctr > 3)
    cout << "Number out of bounds. Please re-enter your answer." << endl;
    } while (ctr < 1 || ctr > 3);
    cout << endl;
    count (); //Records the answer submitted
    }
    //The remaining q_ functions are similar
    void q2()
    {
    cout << "I don't overthink things." << endl;
    do {
    cin >> ctr;
    if (ctr < 1 || ctr > 3)
    cout << "Number out of bounds. Please re-enter your answer." << endl;
    } while (ctr < 1 || ctr > 3);
    cout << endl;
    count ();
    }

    void q3()
    {
    cout << "I am optimistic." << endl;
    do {
    cin >> ctr;
    if (ctr < 1 || ctr > 3)
    cout << "Number out of bounds. Please re-enter your answer." << endl;
    } while (ctr < 1 || ctr > 3);
    cout << endl;
    count ();
    }

    void q4()
    {
    cout << "I laugh hard." << endl;
    do {
    cin >> ctr;
    if (ctr < 1 || ctr > 3)
    cout << "Number out of bounds. Please re-enter your answer." << endl;
    } while (ctr < 1 || ctr > 3);
    cout << endl;
    count ();
    }

    void q5()
    {
    cout << "I have a lot of friends that make me happy." << endl;
    do {
    cin >> ctr;
    if (ctr < 1 || ctr > 3)
    cout << "Number out of bounds. Please re-enter your answer." << endl;
    } while (ctr < 1 || ctr > 3);
    cout << endl;
    count ();
    }

    void q6()
    {
    cout << "I have a good sense of humor." << endl;
    do {
    cin >> ctr;
    if (ctr < 1 || ctr > 3)
    cout << "Number out of bounds. Please re-enter your answer." << endl;
    } while (ctr < 1 || ctr > 3);
    cout << endl;
    count ();
    }

    void q7()
    {
    cout << "I am a happy-go-lucky person." << endl;
    do {
    cin >> ctr;
    if (ctr < 1 || ctr > 3)
    cout << "Number out of bounds. Please re-enter your answer." << endl;
    } while (ctr < 1 || ctr > 3);
    cout << endl;
    count ();
    }

    void q8()
    {
    cout << "I have a positive outlook in life." << endl;
    do {
    cin >> ctr;
    if (ctr < 1 || ctr > 3)
    cout << "Number out of bounds. Please re-enter your answer." << endl;
    } while (ctr < 1 || ctr > 3);
    cout << endl;
    count ();
    }

    void q9()
    {
    cout << "I have a complete family." << endl;
    do {
    cin >> ctr;
    if (ctr < 1 || ctr > 3)
    cout << "Number out of bounds. Please re-enter your answer." << endl;
    } while (ctr < 1 || ctr > 3);
    cout << endl;
    count ();
    }

    void q10()
    {
    cout << "I feel loved by the people around me." << endl;
    do {
    cin >> ctr;
    if (ctr < 1 || ctr > 3)
    cout << "Number out of bounds. Please re-enter your answer." << endl;
    } while (ctr < 1 || ctr > 3);
    cout << endl;
    count ();
    }

    void q11()
    {
    cout << "I have a good disposition in life." << endl;
    do {
    cin >> ctr;
    if (ctr < 1 || ctr > 3)
    cout << "Number out of bounds. Please re-enter your answer." << endl;
    } while (ctr < 1 || ctr > 3);
    cout << endl;
    count ();
    }

    void q12()
    {
    cout << "I cry easily." << endl;
    do {
    cin >> ctr;
    if (ctr < 1 || ctr > 3)
    cout << "Number out of bounds. Please re-enter your answer." << endl;
    } while (ctr < 1 || ctr > 3);
    cout << endl;
    count ();
    }

    void q13()
    {
    cout << "I can be happy even though I'm surrounded with problems." << endl;
    do {
    cin >> ctr;
    if (ctr < 1 || ctr > 3)
    cout << "Number out of bounds. Please re-enter your answer." << endl;
    } while (ctr < 1 || ctr > 3);
    cout << endl;
    count ();
    }

    void q14()
    {
    cout << "I have nothing else to wish for." << endl;
    do {
    cin >> ctr;
    if (ctr < 1 || ctr > 3)
    cout << "Number out of bounds. Please re-enter your answer." << endl;
    } while (ctr < 1 || ctr > 3);
    cout << endl;
    count ();
    }

    void q15()
    {
    cout << "I am a bubbly person." << endl;
    do {
    cin >> ctr;
    if (ctr < 1 || ctr > 3)
    cout << "Number out of bounds. Please re-enter your answer." << endl;
    } while (ctr < 1 || ctr > 3);
    cout << endl;
    count ();
    }

    void q16()
    {
    cout << "I make others happy." << endl;
    do {
    cin >> ctr;
    if (ctr < 1 || ctr > 3)
    cout << "Number out of bounds. Please re-enter your answer." << endl;
    } while (ctr < 1 || ctr > 3);
    cout << endl;
    count ();
    }

    void q17()
    {
    cout << "I am oozing with self-confidence." << endl;
    do {
    cin >> ctr;
    if (ctr < 1 || ctr > 3)
    cout << "Number out of bounds. Please re-enter your answer." << endl;
    } while (ctr < 1 || ctr > 3);
    cout << endl;
    count ();
    }

    void q18()
    {
    cout << "I have a high self-esteem." << endl;
    do {
    cin >> ctr;
    if (ctr < 1 || ctr > 3)
    cout << "Number out of bounds. Please re-enter your answer." << endl;
    } while (ctr < 1 || ctr > 3);
    cout << endl;
    count ();
    }

    void q19()
    {
    cout << "I consider myself fortunate." << endl;
    do {
    cin >> ctr;
    if (ctr < 1 || ctr > 3)
    cout << "Number out of bounds. Please re-enter your answer." << endl;
    } while (ctr < 1 || ctr > 3);
    cout << endl;
    count ();
    }

    void count() //Records the answers entered where x: never, y: often, z: always
    {
    if (ctr == 1)
    x++;
    else if (ctr == 2)
    y++;
    else if (ctr == 3)
    z++;
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: The output of the code seems inconsistent.

    Quote Originally Posted by kristineinah View Post
    The code below is for my project in my subject which is c++.
    Please use code tags when posting code. The code you posted is practically unreadable without code tags.
    The code works fine but it seems like the output is inconsistent and weird.
    How can the program be "fine", but at the same time the output is "inconsistent and weird"? The program is either working correctly or it isn't working correctly.
    Would you please help me think of a solution to this error?
    You never mentioned what the error is. In addition, you can solve your own problems with the program by debugging the code and seeing where the problems originate. The Visual C++ compiler comes with one of the best debuggers ever made for C++ programmers. Debugging is part and parcel of learning how to write programs.
    I tried to research and read books about this issue
    Why are you doing all of that, when the solution is to debug your program and fix the errors?

    Regards,

    Paul McKenzie

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: The output of the code seems inconsistent.

    I don't see anything that would make it inconsistent. You need to provide more detail on exactly what the problem is.

    It's a pretty inefficient design. You could have written it with a lot less code.

    You don't score properly. If you enter the same number each time or two responses the same number of times, none of your if statements work. You'd be better off just coming up with a total number and rating happiness based on that.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: The output of the code seems inconsistent.

    Quote Originally Posted by kristineinah View Post
    Would you please help me think of a solution to this error? I tried to research and read books about this issue but still its not clear for me.
    I will add to what I've stated previously concerning this.

    Since you seem to be a beginner in C++ programming, the way that a program is developed is as follows:

    1) You create a program using an editor.
    2) You use the compiler to compile the program that you wrote, and create an object file if the syntax is correct. Note the highlighted term syntax.
    3) Once the object file is created, the linker then takes the object file and other external libraries, and creates a final executable.

    From the above, nowhere is it stated that the final program will run correctly. In a nutshell, all you're doing when you compile and link a program successfully is that your program has correct C++ syntax, and the linker has found all functions and libraries that are used.

    Your misunderstanding seems to be that if the program builds correctly, it must produce the right results when run. That's the only reason that I can see for searching books to "fix the problem", as if there is a problem with C++. Last week, there was another poster searching google for the same reason -- a program was successfully built, but didn't give the right results.

    The logic of the program has absolutely nothing to do with whether the program was built correctly. As a simple example, if you were to write a program to add two numbers, and instead you mistakingly subtracted the two numbers, the program would compile and link successfully, however the results would not be correct since a mistake was made (you subtracted instead of added).

    Errors such as the ones above are called logical errors, and the only way to fix logical errors is to debug your program. This means you go through the program line by line with the debugger, checking the values of your variables, and seeing where the values of the variables, path of the program, etc. goes wrong. You don't need to read books, search the Internet, or any of that to fix your errors.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: The output of the code seems inconsistent.

    Quote Originally Posted by kristineinah View Post
    The code below is for my project in my subject which is c++.
    To the point that GCDEF mentioned, concerning the size of your code, please take a look:
    Code:
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    int ctr, x, y, z;
    
    //Questions expressed as functions
    void count(); //Function that records the answers submitted
    
    const char* question[] = {"I smile everyday.",
                              "I don't overthink things.",
                              "I am optimistic.",
                              "I laugh hard.",
                              "I have a lot of friends that make me happy."
                            };
    
    const int numQuestions = sizeof(question) / sizeof(question[0]);
    
    void ShowQuestion(int i )
    {
        cout << question[i] << endl;
        do {	//Re-evaluates if submitted number is out of bounds
            cin >> ctr;
            if (ctr < 1 || ctr > 3)
                cout << "Number out of bounds. Please re-enter your answer." << endl;
        } while (ctr < 1 || ctr > 3);
        cout << endl;
        count ();	//Records the answer submitted
    }
    
    int main ()
    {
        x = 0; y = 0; z = 0;
        cout << "**Happiness Test**" << endl << endl
            << "A series of conditions will be posted. State the degree of relevance: \n"
            << "1 - Never \n"
            << "2 - Often \n"
            << "3 - Always \n" << endl << endl;
    
        // Ask each question
        for (int i = 0; i < numQuestions; ++i )
            ShowQuestion(i);
    
        if (x > y && y > z)
            cout << "You are sorrowful as of now.";
        else if (x > z && z > y)
            cout << "You are an unhappy person.";
        else if (y > x && x > z)
            cout << "You are pleased as a person.";
        else if (y > z && z > x)
            cout << "You are a cheerful person.";
        else if (z > x && x > y)
            cout << "You are a joyous person.";
        else
            cout << "You are definitely a happy person.";
    
        cin.ignore();cin.ignore(); //To avoid rapid application termination
    }
    First, note the code tags (the code is formatted and easy to read).

    Second, there is no difference in all of those q() functions you wrote, except for the text that is presented. What if there were 100 questions? Would you write 100 q() functions, all the same except for the text that is presented?

    Instead, the code above:

    1) Creates an array of strings, corresponding to the presented text.
    2) A loop is written to go through each question.
    3) A function called ShowQuestion takes as the parameter the question that I would like to ask. Note that the cout inside ShowQuestion uses the array.

    Now, the logic error is still there and you need to fix it. But at the very least, the program above is simple and straightforward. To add the other questions just requires adding them to the question array.

    The bottom line is that if you're writing code over and over and over again, with the only difference being a string or a couple of values here and there, then it's time to take a step back and "factor out" the common code and create functions, arrays, etc. for the parts that vary.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured