Thread: use of WHILE loop to read the grades

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    use of WHILE loop to read the grades

    Hi

    I was trying to learn the use of WHILE loop. Given below is the simple program I tried to write. The first problem is even when I enter the Roll No. to be "10000" the program doesn't terminate. Secondly, what suggestion(s) or advice(s) do you have for me to improve the program, or learning these WHILE, FOR, DO loops? Please let me know. Thanks a lot.

    Code:
    // use of WHILE loop to read the grades
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
          
    	  int RN;
    	  float M;
          
    	  while(RN != 10000)
          
    	  { 		   
          cout << "Enter the roll no.: ";
          cin >> RN;
          
    	  cout << "Enter the marks: ";
          cin >> M;
          
          if ( M > 80 )
          cout << "A Grade" << endl;
          
    	  else if ( M > 70 )
          cout << "B Grade" << endl;
          
    	  else if ( M > 60 )
          cout << "C Grade" << endl;
          
    	  else if ( M < 60 )
          cout << "F Grade" << endl;
          }
    
          system("pause");
       
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    There is nothing wrong with it....(It works as expected in my comp)...(though I did omit the system("pause") ..not that it'd matter )
    But using a magic number is never a good idea...try to make the program so that it asks the user if he wants to quit....rather than assuming the roll no. 10000 to be a quit signal...

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thanks, manasij.

    But it doesn't terminate even when the entered Roll No. is 10000. Why is so? Please tell me.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First you need to initialize your variables before you use them.
    Code:
    	  int RN;
    	  float M;
          
    	  while(RN != 10000)
    Second this program will exit when you enter 10000 for RN, but not until the loop starts again. If you want to stop execution of the remaining parts of the loop then you need to add some logic to check the value of RN after your input.

    Jim

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    initialize your variables
    Is it a must when not using the initial value anywhere ?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Is it a must when not using the initial value anywhere ?
    But he is using the initial value.
    Code:
    while(RN != 10000)
    RN has not been initialized it could be anything, 10000 even.

    Jim

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    ...oh sorry...missed that...

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Hi Jim

    You are right the program only runs once when I enter the RN = 10000.

    Should I insert an if statement after:
    Code:
    cout << "Enter the roll no.: ";
    cin >> RN;
    to stop the program from proceeding any further? But how do I get out of the loop (if RN == 100)? I think I have read somewhere that there is some "break" word to do it. Don't know how to use it or it is the right keyword to do get out of the loop. Please guide me here.

    And secondly I don't get that initialization part of the variables. Yes, I didn't initialize them but you said (or as far as I can understood it) that I already initialized the RN by saying (while RN != 10000). But this is only a condition statement, what does it have to do with the initialization? Please guide me with it too.

    Thanks a lot.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    To initialize a variable you assign a value to it:
    Code:
    int mvar = 100;
    Your idea of an if statement will work, to exit the loop use the break; statement.

    Jim

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by jimblumberg View Post
    But he is using the initial value.
    Code:
    while(RN != 10000)
    RN has not been initialized it could be anything, 10000 even.
    Jim
    Thanks, Jim.

    Please have a look on the below code. And please let me know if you find something wrong (it works okay on my end), or if you have some advice. I'm new to these loops world so I think so suggestions and advices are important.

    I couldn't understood the purpose of the bold part in the quotes post. I understand you were trying to warn about something but couldn't get it. Please shed some light on it.

    And by the way, is using "return 0;" with int main() compulsory or optional? What is your opinion? It could be a good things to do but is it really compulsory? Please tell me.

    Thank you so much for helping me in so many problems.

    Best wishes
    Jackson

    Code:
    // use of WHILE loop to read the grades
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
          
    	  int RN;
    	  float M;
          
    	  while(RN != 10000)
          
    	  { 		   
          cout << "Enter the roll no.: ";
          cin >> RN;
          
    	  cout << "Enter the marks: ";
          cin >> M;
          
          {
       	  if ( RN >= 10000 )
            break;
          }
          
          if ( M > 80 )
          cout << "A Grade" << endl;
          
    	  else if ( M > 70 )
          cout << "B Grade" << endl;
          
    	  else if ( M > 60 )
          cout << "C Grade" << endl;
          
    	  else if ( M < 60 )
          cout << "F Grade" << endl;
          }
          
          cout << "Job Done" << endl;
    
          system("pause");
       
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  11. #11
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Code:
    int RN(0);
    and what are the { } around the if block for ?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jackson6612
    Please have a look on the below code. And please let me know if you find something wrong (it works okay on my end), or if you have some advice.
    The problem that jimblumberg mentioned is still there: you compare RN to 10000 before RN has been given an initial value. This works most of the time, but will fail in the special case that the garbage value that RN has at that point is 10000. One solution to this is to initialise RN to say, 0. Another solution is to use a do while loop.

    Quote Originally Posted by jackson6612
    And by the way, is using "return 0;" with int main() compulsory or optional?
    Optional.

    Oh, and you need to indent your code better, e.g.,
    Code:
    // use of WHILE loop to read the grades
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
        int RN;
        float M;
    
        while(RN != 10000)
        {
            cout << "Enter the roll no.: ";
            cin >> RN;
    
            cout << "Enter the marks: ";
            cin >> M;
    
            {
                if ( RN >= 10000 )
                    break;
            }
    
            if ( M > 80 )
                cout << "A Grade" << endl;
    
            else if ( M > 70 )
                cout << "B Grade" << endl;
    
            else if ( M > 60 )
                cout << "C Grade" << endl;
    
            else if ( M < 60 )
                cout << "F Grade" << endl;
        }
    
        cout << "Job Done" << endl;
    
        system("pause");
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I couldn't understood the purpose of the bold part in the quotes post. I understand you were trying to warn about something but couldn't get it. Please shed some light on it.
    I was replying to manasij7479 telling him that you had not initialized your variables and you were using an uninitialized variable.
    The following two variables are not initialized.
    Code:
    	  int RN;
    	  float M;
    To initialize these variables assign a value to them.
    Code:
    	  int RN = -1;
    	  float M = 0.0;
    You should always initialize your variables when you define them.

    In the following code:
    Code:
         {
       	  if ( RN >= 10000 )
            break;
          }
    I don't think you understand the brace placement. In this snippet you are creating a block around the if statement which is not required. You probably want the braces to belong to the if statement.
    Code:
       	  if ( RN >= 10000 )
              {
                 break;
             }
    You probably want to move this code to right after the cin >> RN; line.

    Jim

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by manasij7479 View Post
    int RN(0)
    and what are the "{ }" around the if block for ?
    What is that bold thing? I understand it's a function call with argument "0".

    I shouldn't have used the braces with the single statement after the if. But using "{ }" with multi-statements after the if is must. Isn't it?

    Quote Originally Posted by laserlight View Post
    The problem that jimblumberg mentioned is still there: you compare RN to 10000 before RN has been given an initial value. This works most of the time, but will fail in the special case that the garbage value that RN has at that point is 10000. One solution to this is to initialise RN to say, 0. Another solution is to use a do while loop.
    That "garbage" part made everything clear. I didn't know that the computer also has garbage in the empty places! Thanks a lot for telling me this.

    So far I haven't learned the use of the do while loop. But I'm sure will learn tomorrow!

    Quote Originally Posted by laserlight View Post
    Oh, and you need to indent your code better
    I understand the problem. But as I mentioned in some other post that when I paste the code here something goes wrong. I don't know why.

    Quote Originally Posted by jimblumberg View Post
    You should always initialize your variables when you define them.
    I will follow your advice from now on. Thanks for advising me.

    Quote Originally Posted by jimblumberg View Post
    I don't think you understand the brace placement. In this snippet you are creating a block around the if statement which is not required. You probably want the braces to belong to the if statement.
    I get your point but don't you think it's a good practice (just for the sake of consistency) to use braces around the block of the if statement even when the block consists of only one statement? Please advise me.

    Thank you everyone for all the help.

    Best wishes
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I get your point but don't you think it's a good practice (just for the sake of consistency) to use braces around the block of the if statement even when the block consists of only one statement? Please advise me.
    If the braces belong to the if statement yes. The way you had your braces
    Code:
       {
       	  if ( RN >= 10000 )
            break;
          }
    With the braces in this position the braces are not part of the if statement. The if statement is contained in the braces. While not wrong having these extra braces can lead to subtle bugs. These braces denote a block. Anything created inside this block will be destroyed when you exit the block.

    Now I do recommend that new programmers use braces on each and every control statement whether or not they are needed. Now to add braces to your if statement they should look like:
    Code:
    if ( RN >= 10000 )
    {
       break;
    }
    /// OR
    if ( RN >= 10000 ){
       break;
    }
    Do you see the difference?

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while loop within a read function
    By aadil7 in forum C Programming
    Replies: 12
    Last Post: 12-12-2010, 03:33 PM
  2. loop doesn't read files
    By Yumin in forum C++ Programming
    Replies: 5
    Last Post: 05-21-2006, 06:43 PM
  3. Read a file into a loop program
    By TimeClock in forum C Programming
    Replies: 5
    Last Post: 07-17-2003, 06:29 PM
  4. a proper read loop
    By Axolotl in forum C Programming
    Replies: 6
    Last Post: 02-24-2003, 09:46 PM
  5. Replies: 13
    Last Post: 08-15-2002, 09:20 AM