Thread: this loop kicking my arse

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    27

    this loop kicking my arse

    The program below compiles. The only problem I am having at them moment is the loop in the main function...

    Code:
    
    		for(row=0;row<TABLE_SIZE;++row)
    		{
    			if(response = 1)
    			{
    			
    				user_prompt(main_struct[row]);
    	
    				cout << "Enter another student?\nEnter 1 for yes or 2 for no\n\n";
    				
    				cin >> response;
    			}
    				
    			else
    				break;
    			
    		}
    It doesn't want to stop when I hit a number other than 1. I've tried other types of loops as well. The choice started out as a y or n and i took the choice in as a character string and did a
    Code:
    while(toupper(*response) == "Y")
    but that didn't work either, so i thought that using integers would for sure work, but they don't. the closest type of loop that has almost worked is a do-while loop but when i hit no it still went thru the loop one more time

    again, the entire program is below if anyone wants to compile it.

    Code:
    /*this program computes a final average for marketing students
    using structures, pointers, and functions.*/
    
    #include <iostream.h>
    #include <iomanip.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    #define TABLE_SIZE 25
    
    struct STUDENT_STRUCT
    	{
    		char last_name [21];
    		char first_name [21];
    		char social [20];
    		char phone [13];
    		double quiz [5];
    		double final;
    		double final_grade;
    	};
    
    void			user_prompt		(STUDENT_STRUCT&);
    void			display_table	(STUDENT_STRUCT, double);
    double			compute_grades	(STUDENT_STRUCT&);
    double			compute_quizes	(STUDENT_STRUCT&);
    
    
    int main ()
    {
    	
    	
    	STUDENT_STRUCT	main_struct[TABLE_SIZE] = {0};
    	int row = 0;
    	int response;
    	double quiz_ave[TABLE_SIZE]={0};
    	
    	
    	
    
    
    	cout << "Welcome, Dr. Jacobs, to the marketing\n 101 grade";
    	cout << "computation program.\n";
    	
    	
    		for(row=0;row<TABLE_SIZE;++row)
    		{
    			if(response = 1)
    			{
    			
    				user_prompt(main_struct[row]);
    	
    				cout << "Enter another student?\nEnter 1 for yes or 2 for no\n\n";
    				
    				cin >> response;
    			}
    				
    			else
    				break;
    			
    		}
    
    		
    	for (row = 0; row<TABLE_SIZE; ++row)
    	{
    		main_struct[row].final_grade = compute_grades(main_struct[row]);
    	}
    
    	
    	for (row = 0; row<TABLE_SIZE; ++row)
    	{
    		quiz_ave[row] = (compute_quizes(main_struct[row]))/5;
    	}
    
    
    
    	cout << "\n\n"	<< setw(13) << "Last Name"
    					<< setw(13) << "First Name"
    					<< setw(13) << "Quiz Average"
    					<< setw(13) << "Final Exam"
    					<< setw(13) << "Final Grade";
    							
    		
    	for (row=0;row<TABLE_SIZE;++row)
    	{
    	
    		display_table(main_struct[row], quiz_ave[row]);
    	}
    		
    	
    
    	
    
    	cout << "\n\n\n";
    
    	return 0;
    }
    		
    
    
    
    
    
    
    
    
    void user_prompt (STUDENT_STRUCT& stud_struc)
    {
    	int y;
    
    
    			cout << "\nEnter student's last name: ";
    			cin.getline (stud_struc.last_name, 21);
    			cin.ignore (80, '\n');
    			cout << "\nEnter student's first name: ";
    			cin.getline (stud_struc.first_name, 21);
    			cin.ignore (80, '\n');
    			cout << "\nEnter student's social security number: ";
    			cin.getline (stud_struc.social, 20);
    			cin.ignore (80, '\n');
    			cout << "\nEnter student's phone number: ";
    			cin.getline (stud_struc.phone, 13);
    			cin.ignore (80, '\n');
    				for (y=0;y<5;++y)
    				{
    					cout << "\nEnter student's grade for quiz " << y+1 << ": ";
    					cin >> stud_struc.quiz[y];
    				}
    			cout << "\nEnter the student's final exam grade: ";
    			cin >> stud_struc.final;
    		
    }
    
    
    
    
    
    void display_table (STUDENT_STRUCT stud_struc, double quiz)
    {
    	
    	
    
    
    	
    	cout	<< "\n" << setw(13) << stud_struc.last_name;
    	cout	<< setw(13) << stud_struc.first_name;
    	cout	<< setw(13) << quiz;
    	cout	<< setw(13) << stud_struc.final;
    	cout	<< setw(13) << stud_struc.final_grade;
    
    }
    
    
    
    
    double compute_grades	(STUDENT_STRUCT& stud_table)
    {
    	int row,
    		q;
    	double	quiz_total[TABLE_SIZE]={0},
    		quiz_average[TABLE_SIZE]={0},
    		grade[TABLE_SIZE]={0};
    
    	
    	for (row=0;row<TABLE_SIZE;++row)
    	{
    		for (q=0;q<5;++q)
    		{
    			quiz_total[row] += stud_table.quiz[q];
    		}
    	}
    
    
    	for (row=0;row<TABLE_SIZE;++row)
    	{
    		quiz_average[row] = quiz_total[row]/5;
    		grade[row] = ((.75 * quiz_average[row]) + (.25 * stud_table.final));
    
    	}
    
    
    
    
    	return (grade[TABLE_SIZE]);
    }
    
    
    
    double	compute_quizes	(STUDENT_STRUCT& stud_table)
    {
    
    	int q,
    		row;
    	double quiz_total[TABLE_SIZE]={0};
    	double quiz_average[TABLE_SIZE]={0};
    
    
    	for (row=0;row<TABLE_SIZE;++row)
    	{
    		for (q=0;q<5;++q)
    		{
    			
    			quiz_total[row] += stud_table.quiz[q];
    		}
    	}
    
    
    	for (row=0;row<TABLE_SIZE;++row)
    	{
    
    		quiz_average[row] = quiz_total[row]/5;
    	}
    
    	
    
    	return (quiz_average[TABLE_SIZE]);
    }
    cout << "\aBIOTCH";

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    >>if(response = 1)
    I didnt read all the code but I noticed this in the beggining and it should be if (response == 1)
    Why drink and drive when you can smoke and fly?

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    27
    very wierd, i changed it to

    if(response == 1)

    and then changed the declaration to

    int response = 1;

    it seemed to work better, but then i started losing information from my table!
    cout << "\aBIOTCH";

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    Code:
    			cin.ignore();
    			cout << "\nEnter student's last name: ";
    			cin.getline (stud_struc.last_name, 21);
    			cout << "\nEnter student's first name: ";
    			cin.getline (stud_struc.first_name, 21);
    			cout << "\nEnter student's social security number: ";
    			cin.getline (stud_struc.social, 20);
    			cout << "\nEnter student's phone number: ";
    			cin.getline (stud_struc.phone, 13);
    should work.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    27
    ok, i did that and the program seems to work a lot better. only problem is, only the last record gets entered into the table as the first record, all others are cleared!
    cout << "\aBIOTCH";

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    27
    hmmm, just thought of something, is that 'if' statement making the for loop not increment the row? if the 'if' keeps proving true does it even go back up to the for loop?


    Code:
    for(row=0;row<TABLE_SIZE;++row)
    		{
    			if(response ==1)
    			{
    			
    				user_prompt(main_struct[row]);
    	
    				cout << "Enter another student?\nEnter 1 for yes or 2 for no\n\n";
    				
    				cin >> response;
    			}
    				
    			else
    				break;
    			
    		}
    cout << "\aBIOTCH";

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    27

    woot...

    figured it out!!!

    just wanted to follow up on this so no one wastes time trying to help me out here.

    I even sorted my table alphabetically!! Now all i need to do is figure out how to export the table to a txt file...

    wish me luck and thanks to the previous posters for the good advice

    here's the code in case anyone cares hehe

    Code:
    /*this program computes a final average for marketing students
    using structures, pointers, and functions.*/
    
    #include <iostream.h>
    #include <iomanip.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    #define TABLE_SIZE 25
    
    struct STUDENT_STRUCT
    	{
    		char last_name [21];
    		char first_name [21];
    		char social [20];
    		char phone [13];
    		double quiz [5];
    		double final;
    		double final_grade;
    		double quiz_ave;
    	};
    
    void			user_prompt		(STUDENT_STRUCT&);
    void			display_table	(STUDENT_STRUCT);
    double			compute_grades	(STUDENT_STRUCT&);
    double			compute_quizes	(STUDENT_STRUCT&);
    
    
    int main ()
    {
    	
    	
    	STUDENT_STRUCT	main_struct[TABLE_SIZE] = {0};
    	int row = 0;
    	int response=1;
    	int limit;
    	int pass;
    	STUDENT_STRUCT temp_name;
    	
    	
    	
    
    
    	cout << "Welcome, Dr. Jacobs, to the marketing\n 101 grade";
    	cout << "computation program.\n";
    	
    	
    		for(row=0;row<TABLE_SIZE;++row)
    		{
    			if(response == 1)
    			{
    			
    				user_prompt(main_struct[row]);
    	
    				cout << "Enter another student?\nEnter 1 for yes or 2 for no\n\n";
    				
    				cin >> response;
    			}
    				
    		
    			
    		}
    
    		
    	for (row = 0; row<TABLE_SIZE; ++row)
    	{
    		main_struct[row].final_grade = compute_grades(main_struct[row]);
    	}
    
    	
    	for (row = 0; row<TABLE_SIZE; ++row)
    	{
    		main_struct[row].quiz_ave = (compute_quizes(main_struct[row]))/5;
    	}
    
    
    
    	cout << "\n\n"	<< setw(13) << "Last Name"
    					<< setw(13) << "First Name"
    					<< setw(13) << "Quiz Average"
    					<< setw(13) << "Final Exam"
    					<< setw(13) << "Final Grade";
    							
    	
    	limit = TABLE_SIZE - 2;
    	for (pass=1;pass<=TABLE_SIZE-1;++pass)
    	{
    		for (row=0; row<=limit; ++row)
    			if (strcmp(main_struct[row].last_name, main_struct[row+1].last_name)>0)
    			{
    				temp_name=main_struct[row];
    				main_struct[row]=main_struct[row+1];
    				main_struct[row+1] = temp_name;
    			}
    			--limit;
    	}
    				
    
    	for (row=0;row<TABLE_SIZE;++row)
    	{
    	
    		display_table(main_struct[row]);
    	}
    		
    	
    
    	
    
    	cout << "\n\n\n";
    
    	return 0;
    }
    		
    
    
    
    
    
    
    
    
    void user_prompt (STUDENT_STRUCT& stud_struc)
    {
    	int y;
    
    			cin.ignore();
    			cout << "\nEnter student's last name: ";
    			cin.getline (stud_struc.last_name, 21);
    			cin.ignore(80,'\n');
    			cout << "\nEnter student's first name: ";
    			cin.getline (stud_struc.first_name, 21);
    			cin.ignore(80,'\n');
    			cout << "\nEnter student's social security number: ";
    			cin.getline (stud_struc.social, 20);
    			cin.ignore(80,'\n');
    			cout << "\nEnter student's phone number: ";
    			cin.getline (stud_struc.phone, 13);
    			
    				for (y=0;y<5;++y)
    				{
    					cout << "\nEnter student's grade for quiz " << y+1 << ": ";
    					cin >> stud_struc.quiz[y];
    					
    				}
    			cout << "\nEnter the student's final exam grade: ";
    			cin >> stud_struc.final;
    		
    }
    
    
    
    
    
    void display_table (STUDENT_STRUCT stud_struc)
    {
    	
    	
    	
    
    	
    	cout	<< "\n" << setw(13) << stud_struc.last_name;
    	cout	<< setw(13) << stud_struc.first_name;
    	cout	<< setw(13) << stud_struc.quiz_ave;
    	cout	<< setw(13) << stud_struc.final;
    	cout	<< setw(13) << stud_struc.final_grade;
    
    }
    
    
    
    
    double compute_grades	(STUDENT_STRUCT& stud_table)
    {
    	int row,
    		q;
    	double	quiz_total[TABLE_SIZE]={0},
    		quiz_average[TABLE_SIZE]={0},
    		grade[TABLE_SIZE]={0};
    
    	
    	for (row=0;row<TABLE_SIZE;++row)
    	{
    		for (q=0;q<5;++q)
    		{
    			quiz_total[row] += stud_table.quiz[q];
    		}
    	}
    
    
    	for (row=0;row<TABLE_SIZE;++row)
    	{
    		quiz_average[row] = quiz_total[row]/5;
    		grade[row] = ((.75 * quiz_average[row]) + (.25 * stud_table.final));
    
    	}
    
    
    
    
    	return (grade[TABLE_SIZE]);
    }
    
    
    
    double	compute_quizes	(STUDENT_STRUCT& stud_table)
    {
    
    	int q,
    		row;
    	double quiz_total[TABLE_SIZE]={0};
    	double quiz_average[TABLE_SIZE]={0};
    
    
    	for (row=0;row<TABLE_SIZE;++row)
    	{
    		for (q=0;q<5;++q)
    		{
    			
    			quiz_total[row] += stud_table.quiz[q];
    		}
    	}
    
    
    	for (row=0;row<TABLE_SIZE;++row)
    	{
    
    		quiz_average[row] = quiz_total[row]/5;
    	}
    
    	
    
    	return (quiz_average[TABLE_SIZE]);
    }
    cout << "\aBIOTCH";

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. 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
  3. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM