Cannot exit 'while' loop

This is a discussion on Cannot exit 'while' loop within the C++ Programming forums, part of the General Programming Boards category; I'm new to C programming and I'm trying to learn on my own. I'm attempting to write a simple (to ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    60

    Cannot exit 'while' loop

    I'm new to C programming and I'm trying to learn on my own. I'm attempting to write a simple (to most people) program that averages five grades. The output should prompt for a name, ask for 5 grades and then output the person's average to the screen. The end user should be able to type "END" to exit the loop. My problem is when I run my program and type "END", the program prompts for another grade when it should exit the loop. I know that my while loop is coded wrong but I don't know how else to code it so it exits the loop when you type "END". I'm also aware I need to do some formatting to my code to make it more readable but I'll work on that so I apologize. Thanks in advance for any advice.
    Attached Files Attached Files

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Such a short example could have just been posted in your message rather than as an attachment:

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string.h>
    using namespace std;
    
    int main()
    {
        string name;
        double average, total;
    
        int grade = 0;
    
        int g1 = 0;
        int g2 = 0;
        int g3 = 0;
        int g4 = 0;
        int g5 = 0;
    
        while (name != "END")
    
        {
    
            cout << "Student first name (END to stop): ";
            cin >> name;
    
            cout << "Grade 1: ";
            cin >> g1;
    
            cout << "Grade 2: ";
            cin >> g2;
    
            cout << "Grade 3: ";
            cin >> g3;
    
            cout << "Grade 4: ";
            cin >> g4;
    
            cout << "Grade 5: ";
            cin >> g5;
    
    
            total = g1 + g2 + g3 + g4 + g5;
    
            average = total / 5;
    
            cout << "Average for"<<name<<"is: " << average << "\n";
        }
    
        return 0;
    }
    1. The string container is defined in the <string> header. <string.h> is the header that deals with all of the NULL terminated character array functions, i.e. strcpy, strcat, etc...

    2. You don't seem to need the <stdlib.h> header at all. Even if you did, you should prefer the <cstdlib> version.

    3. Yes, your loop does get a name and then continues to ask for grades even if you entered "END". This is because of where you have placed your code asking for the name towards the beginning of the loop. You should ask for the name before the while loop so if you enter "END" right from the start, the program won't ask you to enter grades. You then need to also ask for the name within the loop towards the bottom:

    Code:
    // Ask for name before entering loop
    cout << "Student first name (END to stop): ";
    cin >> name;
    
    while (name != "END")
    {
        cout << "Grade 1: ";
        cin >> g1;
    
        cout << "Grade 2: ";
        cin >> g2;
    
        cout << "Grade 3: ";
        cin >> g3;
    
        cout << "Grade 4: ";
        cin >> g4;
    
        cout << "Grade 5: ";
        cin >> g5;
    
        total = g1 + g2 + g3 + g4 + g5;
        average = total / 5;
    
        // Added a couple of spaces so output doesn't appear squished together
        cout << "Average for "<<name<<" is: " << average << "\n";
    
        // Ask for name again within the loop
        cout << "Student first name (END to stop): ";
        cin >> name;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    308

    Wink

    Well... In my opinion, remove the C string (that should be #include <cstring> in C++) and add pure C++ code:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
    	using namespace std;
    	string name;
    	string breakme = "END";
    
    	double average;
    
    	int grade = 0;
    
    	int g1 = 0;
    	int g2 = 0;
    	int g3 = 0;
    	int g4 = 0;
    	int g5 = 0;
    
    	for(;;)
    
    	{
    
    	cout << "Student first name (END to stop): ";
    	cin >> name;
    	if (name == breakme) break;
    
    	cout << "Grade 1: ";
    	cin >> g1;
    
    	cout << "Grade 2: ";
    	cin >> g2;
    
    	cout << "Grade 3: ";
    	cin >> g3;
    
    	cout << "Grade 4: ";
    	cin >> g4;
    
    	cout << "Grade 5: ";
    	cin >> g5;
    
    
    	average = (g1 + g2 + g3 + g4 + g5)/5.00;
    
    	cout << "Average for " << name << " is: " << average << "\n";
    	}
    
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    Sorry I didn't post my code correctly. I tried finding the instructions to post code within your message but I'm new to the board and was kind of lost so I apologize. Thanks for your advice on tweaking my code, it was very helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 03:20 PM
  2. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 03:45 AM
  3. Trying to figure out a simple loop....
    By chadsxe in forum C++ Programming
    Replies: 9
    Last Post: 01-05-2006, 01:31 PM
  4. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM
  5. I can't figure out why this doesn't exit the loop, MAN!
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2002, 09:47 AM

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