C++ Looping problem
Results 1 to 9 of 9

Thread: C++ Looping problem

  1. #1
    Registered User
    Join Date
    Dec 2000
    Location
    Dublin, Ireland
    Posts
    12

    Question C++ Looping problem

    I'm new to this C++ programming language. I'm using Dev C++ 4.

    Anyhow, I'm trying to get the program to loop back to the begining... however, it ain't working. I want to enter a series of results, but after each one, keep a count of the results and print out the frequency. However, the program just jumps to the end. I don't know how to get it back to a particular point.

    Heres the code so far... Any ideas, anybody???



    #include <iostream.h>
    #include <stdlib.h>

    int main()
    {
    int input_marks = 0;
    int f_first = 0;
    int f_twoone = 0;
    int f_twotwo = 0;
    int f_pass = 0;
    int f_fail = 0;
    int tot_marks = 0;
    char again;
    int y;
    int n;

    system ("cls");


    do

    {
    cout << "Type in marks" << endl;
    cout << "=============" << endl;
    cin >> input_marks;


    tot_marks = tot_marks + input_marks;



    if (input_marks <= 39)
    {
    f_fail = f_fail++;
    }
    else
    if (input_marks <= 54)
    {
    f_pass ++;
    }
    else
    if (input_marks <= 64)
    {
    f_twotwo ++;
    }
    else
    if (input_marks <= 73)
    {
    f_twoone ++;
    }
    else
    if (input_marks <= 100)
    {
    f_first ++;
    }
    {
    cout <<"Do you want to enter another mark (Y/N) ?" << endl;
    cin >> again;
    }

    while ( again == 'y' ); //end of loop
    }
    while ( again == 'n' ); //end of loop

    cout << "Total number of marks :" << tot_marks << endl;
    cout << "Number of grades in the First Class Honours Band :" << f_first << endl;
    cout << "Number of grades in the Second Class Honours, Grade 1 Band :" << f_twoone << endl;
    cout << "Number of grades in the Second Class Honours, Grade 2 Band :" << f_twotwo << endl;
    cout << "Number of grades in the Pass Band :" << f_pass << endl;
    cout << "Number of grades in the Failure Band :" << f_fail << endl;

    system("PAUSE");
    return 0;
    }
    _ _______________________________ _
    Instant Irishman...just add alcohol

  2. #2
    Registered User Archangel42069's Avatar
    Join Date
    Sep 2002
    Location
    Malebolge, 8th Level of Hell
    Posts
    607

    Re: C++ Looping problem

    Originally posted by Seoirse
    I'm new to this C++ programming language. I'm using Dev C++ 4.

    Anyhow, I'm trying to get the program to loop back to the begining... however, it ain't working. I want to enter a series of results, but after each one, keep a count of the results and print out the frequency. However, the program just jumps to the end. I don't know how to get it back to a particular point.

    Heres the code so far... Any ideas, anybody???



    #include <iostream.h>
    #include <stdlib.h>

    int main()
    {
    int input_marks = 0;
    int f_first = 0;
    int f_twoone = 0;
    int f_twotwo = 0;
    int f_pass = 0;
    int f_fail = 0;
    int tot_marks = 0;
    char again;
    int y;
    int n;

    system ("cls");


    do

    {
    cout << "Type in marks" << endl;
    cout << "=============" << endl;
    cin >> input_marks;


    tot_marks = tot_marks + input_marks;



    if (input_marks <= 39)
    {
    f_fail = f_fail++;
    }
    else
    if (input_marks <= 54)
    {
    f_pass ++;
    }
    else
    if (input_marks <= 64)
    {
    f_twotwo ++;
    }
    else
    if (input_marks <= 73)
    {
    f_twoone ++;
    }
    else
    if (input_marks <= 100)
    {
    f_first ++;
    }
    {
    cout <<"Do you want to enter another mark (Y/N) ?" << endl;
    cin >> again;
    }

    while ( again == 'y' ); //end of loop
    }
    while ( again == 'n' ); //end of loop

    cout << "Total number of marks :" << tot_marks << endl;
    cout << "Number of grades in the First Class Honours Band :" << f_first << endl;
    cout << "Number of grades in the Second Class Honours, Grade 1 Band :" << f_twoone << endl;
    cout << "Number of grades in the Second Class Honours, Grade 2 Band :" << f_twotwo << endl;
    cout << "Number of grades in the Pass Band :" << f_pass << endl;
    cout << "Number of grades in the Failure Band :" << f_fail << endl;

    system("PAUSE");
    return 0;
    }

    it's been about 4-5 years since I looked at C++, but try this out...


    #include <iostream.h>
    #include <stdlib.h>

    int main()
    {
    int input_marks = 0;
    int f_first = 0;
    int f_twoone = 0;
    int f_twotwo = 0;
    int f_pass = 0;
    int f_fail = 0;
    int tot_marks = 0;
    char again;
    int y;
    int n;

    system ("cls");


    do

    {
    cout << "Type in marks" << endl;
    cout << "=============" << endl;
    cin >> input_marks;


    tot_marks = tot_marks + input_marks;



    if (input_marks <= 39)
    {
    f_fail = f_fail++;
    }
    else
    if (input_marks <= 54)
    {
    f_pass ++;
    }
    else
    if (input_marks <= 64)
    {
    f_twotwo ++;
    }
    else
    if (input_marks <= 73)
    {
    f_twoone ++;
    }
    else
    if (input_marks <= 100)
    {
    f_first ++;
    }
    endif
    {
    cout <<"Do you want to enter another mark (Y/N) ?" << endl;
    cin >> again;
    }

    while ( again == 'y' ); //end of loop
    }
    while ( again == 'n' ); //end of loop
    }
    cout << "Total number of marks :" << tot_marks << endl;
    cout << "Number of grades in the First Class Honours Band :" << f_first << endl;
    cout << "Number of grades in the Second Class Honours, Grade 1 Band :" << f_twoone << endl;
    cout << "Number of grades in the Second Class Honours, Grade 2 Band :" << f_twotwo << endl;
    cout << "Number of grades in the Pass Band :" << f_pass << endl;
    cout << "Number of grades in the Failure Band :" << f_fail << endl;

    system("PAUSE");
    return 0;
    }
    --Those who think they know everything annoy those of us that do.

  3. #3
    Registered User
    Join Date
    Dec 2000
    Location
    Dublin, Ireland
    Posts
    12
    No joy with that... just throws a few errors at me, all centred around the "end if" conditional at the end.
    _ _______________________________ _
    Instant Irishman...just add alcohol

  4. #4
    Geezer confus-ed's Avatar
    Join Date
    Jul 1999
    Location
    In front of my PC....
    Posts
    13,087
    Oooo-errr ! stop it off with those HUGE posts (there's no need to requote when its that long - just put up the corrected one - more 'readable') ...

    I'll put my hands straight up & say I do no commercial programming in c any variety ... but looks to me like the 'ifs' are the problem & the value just falls straight through ... especially since you set them all at zero which is less than any value you are testing ...

    So somewhere after the 'do' you want something which says (in c) 'repeat this bit until I tell you to stop' ... so that wants to be before where you ask for the individual marks, you want a question there that says 'start/continue processing' or whatever ... You need to have a continue condition which surrounds all the ifs ... neither 'offered' solution has that.

    If you'd used either a function or procedure to do the 'ifs bit' the 'failure' cause would have been more apparent ...

    Is there no case construct in c ? (Surely?) In other languages you might do all those 'ifs' on one line ... (if you can give me the complete command set I'll try & re-write it for you)

  5. #5
    Registered User Archangel42069's Avatar
    Join Date
    Sep 2002
    Location
    Malebolge, 8th Level of Hell
    Posts
    607
    Originally posted by Seoirse
    No joy with that... just throws a few errors at me, all centred around the "end if" conditional at the end.
    Let me know what the errors are and I will puzzle it out...I will also reinstall visual studio...may not be the same compiler, but at least I may be able to see what works.....
    --Those who think they know everything annoy those of us that do.

  6. #6
    Registered User Archangel42069's Avatar
    Join Date
    Sep 2002
    Location
    Malebolge, 8th Level of Hell
    Posts
    607
    Here is working code....

    // grades.cpp : Defines the entry point for the console application.
    //

    #include <stdafx.h>
    #include <iostream.h>
    #include <stdlib.h>

    int main()
    {
    int input_marks = 0;
    int f_first = 0;
    int f_twoone = 0;
    int f_twotwo = 0;
    int f_pass = 0;
    int f_fail = 0;
    int tot_marks = 0;
    char again = 'y';

    system ("cls");


    do
    {
    cout << "Type in marks" << endl;
    cout << "=============" << endl;
    cin >> input_marks;

    if (input_marks <= 39)
    {
    f_fail++;
    }
    else if (input_marks >= 40)
    {
    f_pass++;
    };

    if (input_marks <= 64)
    {
    f_twotwo++;
    }
    else if (input_marks <= 73)
    {
    f_twoone++;
    }
    else if (input_marks <= 100)
    {
    f_first++;
    };

    tot_marks++;

    cout <<"Do you want to enter another mark (Y/N) ?" << endl;
    cin >> again;

    } while ( again == 'y');


    cout << "Total number of marks :" << tot_marks << endl;
    cout << "Number of grades in the First Class Honours Band :" << f_first << endl;
    cout << "Number of grades in the Second Class Honours, Grade 1 Band :" << f_twoone << endl;
    cout << "Number of grades in the Second Class Honours, Grade 2 Band :" << f_twotwo << endl;
    cout << "Number of grades in the Pass Band :" << f_pass << endl;
    cout << "Number of grades in the Failure Band :" << f_fail << endl;

    system("PAUSE");
    return 0;
    }




    and the visible result:


    --Those who think they know everything annoy those of us that do.

  7. #7
    Registered User migel628's Avatar
    Join Date
    Jul 2000
    Location
    New York
    Posts
    99
    Good stuff, I was just about to suggest a do while loop, but looks like you beat me to it...

  8. #8
    Registered User Archangel42069's Avatar
    Join Date
    Sep 2002
    Location
    Malebolge, 8th Level of Hell
    Posts
    607
    Originally posted by confus-ed
    Oooo-errr ! stop it off with those HUGE posts (there's no need to requote when its that long - just put up the corrected one - more 'readable') ...

    I'll put my hands straight up & say I do no commercial programming in c any variety ... but looks to me like the 'ifs' are the problem & the value just falls straight through ... especially since you set them all at zero which is less than any value you are testing ...

    So somewhere after the 'do' you want something which says (in c) 'repeat this bit until I tell you to stop' ... so that wants to be before where you ask for the individual marks, you want a question there that says 'start/continue processing' or whatever ... You need to have a continue condition which surrounds all the ifs ... neither 'offered' solution has that.

    If you'd used either a function or procedure to do the 'ifs bit' the 'failure' cause would have been more apparent ...

    Is there no case construct in c ? (Surely?) In other languages you might do all those 'ifs' on one line ... (if you can give me the complete command set I'll try & re-write it for you)
    A complete command set can be found on the Microsoft Knowledge Base library under Visual Tools\Visual C++\Product Documentation\Visual C++ Programmer's Guide\Language Quick Reference. I believe there is a Case...Case Else statement - but I may be getting C++ confused with VB again.

    In any event, the code I posted works, although there is no error trapping - Any character other than "y" causes the final output portion of the code to execute.
    --Those who think they know everything annoy those of us that do.

  9. #9
    Registered User 10of40's Avatar
    Join Date
    Aug 2002
    Location
    Unimatrix W-2
    Posts
    78
    // Here's the code snippet to replace the if...else blocks.
    //

    if (input_marks <= 39)
    f_fail++;
    if ((input_marks >= 39) && (input_marks < 54))
    f_pass++;
    if ((input_marks >= 54) && (input_marks < 64))
    f_twotwo++;
    if ((input_marks >= 64) && (input_marks < 73))
    f_twoone++;
    if (((input_marks >= 73) && (input_marks < 100)) || (input_marks == 100))
    f_first++;
    "I aspire sir, to be better than I am." -- Data, Star Trek: Nemesis

Bookmarks

Posting Permissions

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