Thread: Help with loop

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    2

    Help with loop

    Can't figure out why my code won't retrieve more than the first input, in a loop.
    This is what I have so far:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    
    const int SENTINEL = -999;
    
    int main()
    {
        int studentNumber[100]; 
        string studentLastName[100]; 
        double studentGPA[100];
        int stuNum;
        string stuLName;
        double stuGPA, sum, average;
        
        cout << "Please enter a student number, -999 to end:";  
        cin >> stuNum;
    
              
               for(int counter = 99; counter >= 0; counter--) 
                   {                                         
                        while (stuNum != -999)  
                        {
                        cout << "Enter last name: ";  
                        cin >> stuLName;
                        cout << "Enter Student GPA: ";
                        cin >> stuGPA;
                        
                        studentNumber[counter] = stuNum;  
                        studentLastName[counter] = stuLName;
                        studentGPA[counter] = stuGPA;
                        
                        counter++;
                        
                        cout << "Enter Student number, -999 to end: " ;
                        cin >> stuNum;
                        }
                        
                        cout << "The reverse is:";
                        cout << studentNumber[0] << " ";
                        cout << studentLastName[0] << " " << studentGPA[0];
                        
                        cin >> stuGPA; 
                   }
                   
                           
                       
                   cout << endl;          
                        
    return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your for loop takes counter down by one; the body of your for loop takes counter up by one. Net result: no change in counter.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    2
    ok so remove the one in the body but I still only get one output
    Last edited by pumpkinkmk; 04-06-2010 at 06:04 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    How do you anticipate getting back inside the while loop, once you get out?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poll event loop
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 04:28 AM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 09:46 PM
  3. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM