Thread: Problem with while loop

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    52

    Problem with while loop

    I'm having a problem with my while loop, it won't stop prompting for input. I'm using a while loop and want to bail out when the user just presses 'enter' (\0). Perhaps somebody could give me a hint as to what I'm doing wrong?

    Code:
    #include <iostream>
    #include <iomanip>
    #include <stdlib.h>
    
    using namespace std;
    
    struct Employee
    {
    	char firstName[35];
    	char lastName[35];
    	char hireDate[11];
    	char SSN[12];
    	int Hours;
    	float payRate;
    };
    
    float PayCalc(int, float);
    
    int main(void)
    {
    	const int max_count(50);
    	Employee Emps[max_count];
    	int count(0);
    	
    
    	
    	while(Emps[count].firstName != '\0')
    	{
    		cout << "Employee's first name: ";
    		cin >> Emps[count].firstName;
    		
    		cout << Emps[count].firstName << "'s last name is: ";
    		cin >> Emps[count].lastName;
    
    		cout << Emps[count].firstName << "'s hire date is: ";
    		cin >> Emps[count].hireDate;
    
    		cout << Emps[count].firstName << "'s social security number: ";
    		cin >> Emps[count].SSN;
    
    		cout << "Number of hours that " << Emps[count].firstName << " worked: ";
    		cin >> Emps[count].Hours;
    
    		cout << Emps[count].firstName << "'s pay rate: ";
    		cin >> Emps[count].payRate;
    	
    		count++;
    		cin.ignore(100, '\n');
    	}
    
    
    
    	for(int a(0); a < count; a++)
    	{
    		cout << Emps[a].lastName << ", " << Emps[a].firstName << setw(20) << Emps[a].hireDate << setw(5);
    		cout << Emps[a].SSN << setw(5) << Emps[a].Hours << setw(5) << PayCalc(Emps[a].Hours, Emps[a].payRate) << endl;
    	}
    
    	cout << right << count << endl;
    	
    	return 0;
    
    }
    
    
    float PayCalc(int a, float b)
    {
    	return float(a) * b;
    }
    I hope that this looks right on the screen, it's my first time to post and I am trying to abide by the forum bylaws. Anyhow, I'm not looking for the actual code, I'd like to figure this out...However, if somebody could help me in seeing what I'm doing wrong then I would greatly appreciate it.

  2. #2
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Code:
    #include <iostream>
    #include <iomanip>
    #include <stdlib.h>
    #include <ctype.h>
    
    using namespace std;
    
    struct Employee
    {
    	char firstName[35] = { ' ' };
    	char lastName[35] = { ' ' };
    	char hireDate[11]  = { ' ' };
    	char SSN[12] = { ' ' };
    	int Hours;
    	float payRate;
    };
    
    float PayCalc(int, float);
    
    int main(void)
    {
    	const int max_count(50);
    	Employee Emps[max_count];
    	int count(0);
    	
    
    	
    	while(!isspace(Emps[count].firstName[0]))
    	{
    		cout << "Employee's first name: ";
    		cin >> Emps[count].firstName;
    		
    		cout << Emps[count].firstName << "'s last name is: ";
    		cin >> Emps[count].lastName;
    
    		cout << Emps[count].firstName << "'s hire date is: ";
    		cin >> Emps[count].hireDate;
    
    		cout << Emps[count].firstName << "'s social security number: ";
    		cin >> Emps[count].SSN;
    
    		cout << "Number of hours that " << Emps[count].firstName << " worked: ";
    		cin >> Emps[count].Hours;
    
    		cout << Emps[count].firstName << "'s pay rate: ";
    		cin >> Emps[count].payRate;
    	
    		count++;
    		cin.ignore(100, '\n');
    	}
    
    
    
    	for(int a(0); a < count; a++)
    	{
    		cout << Emps[a].lastName << ", " << Emps[a].firstName << setw(20) << Emps[a].hireDate << setw(5);
    		cout << Emps[a].SSN << setw(5) << Emps[a].Hours << setw(5) << PayCalc(Emps[a].Hours, Emps[a].payRate) << endl;
    	}
    
    	cout << right << count << endl;
    	
    	return 0;
    
    }
    
    
    float PayCalc(int a, float b)
    {
    	return float(a) * b;
    }
    That may work.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    52
    Unfortunately, that didn't work. I've used a character input to determine whether to continue through the loop. What I want to do is have a null character input terminate the loop. Perhaps a 'switch' statement? Thank you for the help though....

  4. #4
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    cin always waits for a character before the program continues so just inputting a null character wont work.
    Just make it like this in the end of the loop:

    Code:
    char yesno[3]; //ill make it three so the prog wont bail out if the user does something wrong
    cout << "\n\nContinue? (y or n)     ";
    cin >> yesno;
    if(strcmp(yesno,"y")==0) {
    exit(0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  4. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 10:24 AM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM