5 Replies - 1474 Views - Last Post: 23 August 2006 - 03:48 PM Rate Topic: -----

#1 hlabrams  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 17-August 06

problem with a loop

Posted 23 August 2006 - 11:17 AM

Can anyone tell me why after I enter either a 1 or a 0, the screen goes nuts? Here's my code.....
#include<iostream>
#include<iomanip>

using namespace std;

struct STUDENT_STRUCT
{
	char last_name[31];
	char first_name[31];
	char soc_sec_no[10];
	char ph_no[11];
	int quiz_1;
	int quiz_2;
	int quiz_3;
	int quiz_4;
	int quiz_5;
	double quiz_avg;
	int final_exam;
	double final_grade;
};

void Load_Student_Table(STUDENT_STRUCT[], int);
void Display_Student_Table(STUDENT_STRUCT[], int);

   
	const int TABLE_SIZE = 25;
	int response;	//yes=1, no=0
	int num_students;

int main()
{
 
	
	STUDENT_STRUCT student_table[TABLE_SIZE];
	num_students=0;

	cout << setprecision(2)
		 << setiosflags(ios::fixed)
		 << setiosflags(ios::showpoint);

	Load_Student_Table(student_table, TABLE_SIZE);

	// Display the table
	cout << endl << endl;
	cout << "This is the table you entered. ";

	Display_Student_Table(student_table, TABLE_SIZE);

cout << endl << endl;
cout << setw(10) << "Last Name"
	 << setw(10) << "First Name"
	 << setw(10) << "Social Security Number"
	 << setw(10) << "Phone Number"
	 << setw(10) << "Quiz 1"
	 << setw(10) << "Quiz 2"
	 << setw(10) << "Quiz 3"
	 << setw(10) << "Quiz 4"
	 << setw(10) << "Quiz 5"
	 << setw(10) << "Final Exam" << endl;
return 0;
}

void Load_Student_Table(STUDENT_STRUCT student_table[], int size)
{
	int row=0;
	int response='1';
	
	cout << endl;
	cout << "Enter the table values as you are prompted:"
		 << endl << endl;

	while(response=='1')
	{
cout << endl;
cout << "For row #" << row+1 << " enter:" << endl;
cout << "Last Name: ";
cin.getline(student_table[row].last_name, 31);

cout << "First Name: ";
cin.getline(student_table[row].first_name, 31);

cout << "Social Security Number: ";
cin.getline(student_table[row].soc_sec_no, 10);

cout << "Phone Number: ";
cin.getline(student_table[row].ph_no, 11);

cout << "Quiz 1: ";
cin >> student_table[row].quiz_1;

cout << "Quiz 2: ";
cin >> student_table[row].quiz_2;

cout << "Quiz 3: ";
cin >> student_table[row].quiz_3;

cout << "Quiz 4: ";
cin >> student_table[row].quiz_4;

cout << "Quiz 5: ";
cin >> student_table[row].quiz_5;

cout << "Final Exam: ";
cin >> student_table[row].final_exam;
row++;
cout<<"Do you wish to enter another student?(1/0)"<<endl;
cin>>response;
}
	cin.get();

}
void Display_Student_Table(STUDENT_STRUCT student_table[], int size)
{
	int row;

	cout << endl << endl;
	cout << setw(10) << "Last Name"
		 << setw(10) << "First Name"
		 << setw(10) << "Social Security Number"
		 << setw(10) << "Phone Number"
		 << setw(10) << "Quiz 1"
		 << setw(10) << "Quiz 2"
		 << setw(10) << "Quiz 3"
		 << setw(10) << "Quiz 4"
		 << setw(10) << "Quiz 5"
		 << setw(10) << "Final Exam" << endl;
	for(row=0; row<size; ++row)
	{
	cout << endl;
	cout << setw(10) << student_table[row].last_name
		 << setw(10) << student_table[row].first_name
		 << setw(10) << student_table[row].soc_sec_no
		 << setw(10) << student_table[row].ph_no
		 << setw(10) << student_table[row].quiz_1
		 << setw(10) << student_table[row].quiz_2
		 << setw(10) << student_table[row].quiz_3
		 << setw(10) << student_table[row].quiz_4
		 << setw(10) << student_table[row].quiz_5
		 << setw(10) << student_table[row].final_exam;
}

}


Is This A Good Question/Topic? 0
  • +

Replies To: problem with a loop

#2 DeeViLiSh  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 175
  • Joined: 25-July 06

Re: problem with a loop

Posted 23 August 2006 - 11:46 AM

Found it. You say int response = '1';
'1' is the NULL in ASCII table, when user enters 1 as a number and not NULL, the while loop isn't correct since the user entered NULL and not 1, take off the ' ' at int response and while(response == '1') and the loop works
Btw, your loop is fixed but the problem is how you display your table, that's when it screws up, the main problem wasn't the while loop ^_^

int response = 1;

	cout << endl;
	cout << "Enter the table values as you are prompted:"
		 << endl << endl;

	while(response == 1)
	{

This post has been edited by DeeViLiSh: 23 August 2006 - 12:07 PM

Was This Post Helpful? 0
  • +
  • -

#3 hlabrams  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 17-August 06

Re: problem with a loop

Posted 23 August 2006 - 12:38 PM

Thanks, DeeViLiSh....it worked. Now I noticed another problem....when I hit 1 to enter another student, it says Last Name: First Name: instead of asking me for the Last Name and then asking me for the First Name. Any ideas on how to fix that?
Was This Post Helpful? 0
  • +
  • -

#4 hlabrams  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 17-August 06

Re: problem with a loop

Posted 23 August 2006 - 12:56 PM

View Posthlabrams, on 23 Aug, 2006 - 12:38 PM, said:

Thanks, DeeViLiSh....it worked. Now I noticed another problem....when I hit 1 to enter another student, it says Last Name: First Name: instead of asking me for the Last Name and then asking me for the First Name. Any ideas on how to fix that?




Forget it....I fixed the problem stated above and all works fine until I enter a 0 for no. Then, the screen goes nuts again. Any idea why?
Was This Post Helpful? 0
  • +
  • -

#5 DeeViLiSh  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 175
  • Joined: 25-July 06

Re: problem with a loop

Posted 23 August 2006 - 03:29 PM

As I said, it's the Display_Student_Table function that's screwing up, I'll look at it.
Was This Post Helpful? 0
  • +
  • -

#6 hlabrams  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 17-August 06

Re: problem with a loop

Posted 23 August 2006 - 03:48 PM

THX, DeeViLiSh....I appreciate your help. I'm new at this.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1