Loopy Problem!!!

This is a discussion on Loopy Problem!!! within the C++ Programming forums, part of the General Programming Boards category; I'm doing a program for school and this piece of code won't work... // This function gets the user to ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    1

    Talking Loopy Problem!!!

    I'm doing a program for school and this piece of code won't work...

    // This function gets the user to input the percent grade and returns the
    // rounded version of that value.
    int input_grade (void)
    {

    // This variable is the percent grade and is input by the user. It is set to 150
    // so the 'while' loop will function correctly.
    float per_grd=150;

    // This variable hold the value of the rounded percent grade.
    int round_grd;


    // This loop lets the user re-enter the percentage grade if it is invalid.
    while (per_grd>100 || per_grd <0)
    {

    // Gets the user to input the percentage grade.
    printf ("Enter percentage grade: ");
    cin >> per_grd;

    // The following checks it make sure the input is valid and if it is not, it
    // displays an error message.
    if (per_grd>100 || per_grd <0)
    {

    // Prints an error message to the screen, gets the user to press
    // any key to continue then clears the screen. The loop then restarts.
    printf ("\nTHAT IS AND INVALID ENTRY...PLEASE RE-ENTER!\n\n");
    system ("PAUSE");
    system ("CLS");


    }

    else
    {

    // Calls the round function then assigns the value to the 'round_grd'
    // function.
    round_grd=per_grd_round(per_grd);

    }

    }

    // Returns the value of the rounded grade.
    return round_grd;
    }

    ... When you enter a letter the loop works, but it won't let you re-enter the value and just loops continuously.

    Thanx, Ian

    PS. I have attached the full code... please take a look and reply...
    Attached Files Attached Files

  2. #2
    Registered User JasonLikesJava's Avatar
    Join Date
    Mar 2002
    Posts
    175

    Talking

    I'm not too sure of the problem...

    Maybe you could state your while loop as while(1) and then use a break statement in your if statement to exit the loop.

    Here is an easier way to round a number I think just FYI (you'll have to include math.c)

    int round(double x)
    {
    if (ceil(x)-x <= x-floor(x))
    {
    return (int)ceil(x);
    }
    else
    {
    return (int)floor(x);
    }
    }

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this:

    // This loop lets the user re-enter the percentage grade if it is invalid.
    while (per_grd>100 || per_grd <0)
    {

    // Gets the user to input the percentage grade.
    printf ("Enter percentage grade: ");
    cin >> per_grd;

    if (cin.fail())
    {
    cin.clear();
    cin.seekg(0);
    per_grd = 150;
    }

    // The following checks it make sure the input is valid and if it is not, it
    // displays an error message.
    if (per_grd>100 || per_grd <0)
    {

    // Prints an error message to the screen, gets the user to press
    // any key to continue then clears the screen. The loop then restarts.
    printf ("\nTHAT IS AND INVALID ENTRY...PLEASE RE-ENTER!\n\n");
    system ("PAUSE");
    system ("CLS");


    }

    else
    {

    // Calls the round function then assigns the value to the 'round_grd'
    // function.
    round_grd=per_grd_round(per_grd);

    }

    }

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Originally posted by JasonLikesJava

    Maybe you could state your while loop as while(1) and then use a break statement in your if statement to exit the loop.

    That's called bad programming...It's hard to look through code with gotos and breaks.

  5. #5
    Registered User JasonLikesJava's Avatar
    Join Date
    Mar 2002
    Posts
    175
    What???? I've never heard that a break is a bad programming technique.

    Anyways, in the code it just seemed strange that the while and if say the same thing so it seemed clearer to me to use break.

    I agree with that gotos are bad and have never used them since I left programming on my TI-83 during Algebra I (never really learned to use them since they aren't needed).

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What???? I've never heard that a break is a bad programming technique.
    Not a bad programming technique, but it does indicate bad design. What break/continue/goto do is jump locally, this defeats the purpose of a structured program which should start at the beginning, and move logically to the end. And using infinite loops with break actually makes for longer code.
    Code:
    int more = getFirst();
    while ( more ) {
      // Do stuff
      more = getMore();
    }
    Code:
    if ( getFirst() )
      while ( 1 ) {
        // Do stuff
        if ( !getMore() )
          break;
      }
    Which is easier to follow, and which is shorter? Programmers who use infinite loops and breaks think they are making the program shorter and easier to understand when it actually is the opposite.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User JasonLikesJava's Avatar
    Join Date
    Mar 2002
    Posts
    175
    Right, in the second case it is shorter... in this case, but I don't think that is the case in the originally posted program.

  8. #8
    Registered User JasonLikesJava's Avatar
    Join Date
    Mar 2002
    Posts
    175
    Strike that.... FIRST case is shorter... but you know what I mean.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but I don't think that is the case in the originally posted program
    Probably because it wasn't designed properly. If you just throw code together without thinking about it then an infinite loop will be shortest, but if you redesign the program with structure in mind then without an infinite loop it will be shorter.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21