output error
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: output error

  1. #1
    Join Date
    Jun 2013
    Posts
    2

    output error

    the frequent answer by the user input didnt match with the out
    help please

    #include<iostream.>

    using namespace std;

    int main()

    {

    int num[7];


    cout<<"answer all question.1,2,or 3"<<endl<<endl;


    cout<<"QUESTION 1"<<endl<<endl;

    cout<<"A)your favourate food?"<<endl<<endl;
    cout<<"1)chicken"<<endl;
    cout<<"2)fish"<<endl;
    cout<<"3)meat"<<endl<<endl;

    cin >> num[0];



    cout<<"QUESTION 2"<<endl<<endl;

    cout<<"A)your favourate drinks?<<endl"<<endl;
    cout<<"1)coca-cola"<<endl;
    cout<<"2)sprite"<<endl;
    cout<<"3)pepsi"<<endl<<endl;

    cin >> num[1];



    cout<<"QUESTION 3"<<endl<<endl;

    cout<<"A)your favourate football team?"<<endl<<endl;
    cout<<"1)brazil"<<endl;
    cout<<"2)england"<<endl;
    cout<<"3)argentina"<<endl<<endl;

    cin >> num[2];



    cout<<"QUESTION 4"<<endl<<endl;

    cout<<"A)your favourate ice-cream flavour?"<<endl<<endl;
    cout<<"1)vanila"<<endl;
    cout<<"2)chocolate"<<endl;
    cout<<"3)strawberry"<<endl<<endl;

    cin >> num[3];


    cout<<"QUESTION 5"<<endl<<endl;

    cout<<"A)your favourate book?"<<endl<<endl;
    cout<<"1)novel"<<endl;
    cout<<"2)magazine"<<endl;
    cout<<"3)education"<<endl<<endl;

    cin >> num[4];

    cout<<"QUESTION 6"<<endl<<endl;

    cout<<"A)your favourate transportation?"<<endl<<endl;
    cout<<"1)car"<<endl;
    cout<<"2)train"<<endl;
    cout<<"3)motocycle"<<endl<<endl;

    cin >> num[5];

    cout<<"QUESTION 7"<<endl<<endl;

    cout<<"A)your favourate color?"<<endl<<endl;
    cout<<"1)red"<<endl;
    cout<<"2)white"<<endl;
    cout<<"3)blue"<<endl<<endl;

    cin >> num[6];

    cout<<"your frequent answer "<<endl;


    int sorted[7];
    int N = sizeof(num)/sizeof(num[0]);
    int last_change=0;

    for (int i=1; i<4; ++i){
    int n;
    for (n = last_change; n<N && num[n]==i; ++n) {
    }
    cout << i << " repeted " << (n-last_change) << " times " <<endl;
    last_change = n;

    }
    cout<<"thank you"<<endl<<endl;
    system("pause");
    return 0;
    }

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

    Re: output error

    1) Use code tags when posting code. The code you posted is unformatted and is hard to read.

    2)
    Code:
    #include<iostream.>
    What is this? Don't type your code in. Copy it directly from your editor and paste it in the message window (using code tags, as stated above).

    3) Have you debugged your program? What debugging have you done, or are you waiting for us to debug the program for you (which is really your job). For example, this loop:
    Code:
    int N = sizeof(num)/sizeof(num[0]);
    int last_change=0;
    for (int i=1; i<4; ++i)
    {
        int n;
        for (n = last_change; n<N && num[n]==i; ++n) 
        {
        }
        cout << i << " repeted " << (n-last_change) << " times " <<endl;
        last_change = n;
    }
    Did you step through this code with the debugger? What are the values for each loop iteration?

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jun 2013
    Posts
    2

    Re: output error

    sorry..that my mistake..first time joint this forum..newbie in c++

    the value is 0 for each loop...

    if all answer is the same , the output is correct

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,316

    Re: output error

    Quote Originally Posted by dodys View Post
    sorry..that my mistake..first time joint this forum..newbie in c++

    the value is 0 for each loop...

    if all answer is the same , the output is correct

    So from the information obtained from the debugging you have done, what is the issue with the code then? You should be able now to work out what's wrong and correct it.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

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

    Re: output error

    Quote Originally Posted by dodys View Post
    sorry..that my mistake..first time joint this forum..newbie in c++

    the value is 0 for each loop...
    The point is this -- when you write a program, it is a requirement that you know how to debug the programs you write. The reason for the requirement is that you had a plan in mind, you put that plan on paper, you then took that plan on paper and wrote code. Now that the program runs differently than what you planned, you then debug the code to see where the program deviates from the plan you had.

    That's the way you learn how to write a program. Asking a professional or expert programmer to debug your code is cheating (if it is a homework assignment). What we expect is that you at least tell us what is going on in the loop at each step, and tell us which line of code (after debugging) is giving a different result than what you expected, and not just say "my loop is giving wrong results, tell me what to fix".

    Regards,

    Paul McKenzie

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,067

    Re: output error

    I'll add that you want to write your algorithm out in English first and then pseudo code so that you're sure you know what it's doing. I'd also avoid variable names such as n and N and use something meaningful, so that when you revisit the code next year, or somebody else looks at it, they'll have some clue what those variables are supposed to be used for.

    An outsider looking at
    for (n = last_change; n<N && num[n]==i; ++n)
    really has no idea what you're trying to accomplish there.

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,316

    Re: output error

    The reason for the requirement is that you had a plan in mind, you put that plan on paper, you then took that plan on paper and wrote code.
    you want to write your algorithm out in English first and then pseudo code so that you're sure you know what it's doing.
    Yes, yes and yes! This is called program design. However IMO I fear that too many students of programming are not being taught how to correctly design programs. They start by writing some code that the student vaguely thinks may do something connected with the problem thay have been set, find what they have written doesn't work and then they don't know what to do because as they haven't designed the program they don't know how it should behave so can't debug and fix it. It is not a sign of weakness producing a program design first before coding. It is a sign of a good programmer. Some students seem to think that 'professional/expert' programmers just sit down and rattle off a program of a few thousand lines straight from our head that works straight away. We don't! We design first then code then debug.

    [aside. When I was studying for my Computer Science degree which involved substantial programming in several different languages we had to produce a program design first that was marked. Only when we had an agreed correct design did we then start coding the program from the design. If the submitted program was different from the previously agreed design without good reason we were penalised. IMO this should be the norm for all programming excercises.]
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center