### How to reiterate the loop Hello everyone,

Before I posted here I did try to find help on the topic to no avail. I am trying to reiterate the if-else loop where my code asks for students 2, 3 and so forth. As you can see when you run the code it only asks for student 1 and continues on. I need a total of 10 students. Perhaps I don't understand the logic or how to being my problem solving

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164`` ``````#include #include #include #include using namespace std; int main() { // using array to set number of recods, student names and GPA const int ARRAY_SIZE = 10; // number of records generated by Array int record[ARRAY_SIZE]; // number records to be generated string studentTotal [ARRAY_SIZE]; // number of students generated double studentGPA[ARRAY_SIZE]; // number of gpa generated string lastName; string firstName; // Set floating point values to two decimal places cout << setiosflags(ios::fixed); cout << setiosflags(ios::showpoint); cout << setprecision(2); // gather student names and GPAs cout << " welcome to GPA Sheet " << endl; cout << "======================" << endl; for (int count = 0; count < ARRAY_SIZE; count++) { if (count == 0){ cout << "Student " << count + 1 <> studentGPA[count]; } else { cout << "Enter in the students name, starting with the last name : "; cin.ignore(); getline (cin, studentTotal[count]); cout << " student GPA: "; cin >> studentGPA[count]; } } cout << "====================" << endl; //print column names cout << setfill(' ') << setw(25) << left << "Student Name" << setw(5) << right << "GPA" << endl; // set letter case in studentNames array for (int count = 0; count < ARRAY_SIZE; count++) { // separate first and last names string::size_type commaPos = studentTotal[count].find(","); //find "," lastName = studentTotal[count].substr(0, commaPos); // select last name firstName = studentTotal[count].substr(commaPos + 1); // select first name if (firstName == ' ') firstName.erase(0, 1); // remove blank space between "," and first letter of first name // set all letters in first name to lower case, and then set first letter to upper case for (int x = 0; x < firstName.length(); x++) { firstName[x] = tolower(firstName[x]); } firstName = toupper(firstName); // set all letters in last name to lower case, and then set first letter to upper case for (int x = 0; x < lastName.length(); x++) { lastName[x] = tolower(lastName[x]); } lastName = toupper(lastName); // set original studentName variable to proper case formatting studentTotal[count] = lastName + ", " + firstName; // print student record cout << setfill(' ') << setw(25) << left << studentTotal[count] << setw(5) << right << studentGPA[count] << endl; } //system("pause"); return 0; } ``````

Last edited on  First, please use code tags. See http://www.cplusplus.com/articles/jEywvCM9/

Is this the loop that you talk about:
 ``12345678910111213141516`` ``````for ( int count = 0; count < ARRAY_SIZE; count++ ) { if (count == 0) { cout << "Student " << count + 1 <> studentGPA[count]; } else { cout << "Enter in the students name, starting with the last name : "; cin.ignore(); getline (cin, studentTotal[count]); cout << " student GPA: "; cin >> studentGPA[count]; } }``````

You have repetition there. Something that is done on both branches of the if..else. We can take it out:
 ``12345678910111213`` ``````for ( int count = 0; count < ARRAY_SIZE; count++ ) { if (count == 0) { cout << "Student " << count + 1 <> studentGPA[count]; }``````

Why do you have line 4 only for the first student?
Last edited on So this where I was mistaken because if-else is a statement not a loop. What I wanted to do was have the program ask for student 1, 2 and so forth. Judging by what you just asked me I need to have an if-else from all the students all the way up to 10. Could I just turn this is into a do-while loop? Why if..else at all?
 ``123456789`` ``````for ( int count = 0; count < ARRAY_SIZE; count++ ) { cout << "Student " << count + 1 <> studentGPA[count]; }`````` So the problem is, it doesnt ask for student 2. While it works, it doesn't ask for student 2, like it does the first.

this is what I get when its ran

Student 1
Enter in the student's name, starting with the last name :

After inputting the first student's info I want it run again but ask

student 2
Enter in the student's name . starting with the last name :

and continue to ask up to 10 and then stop

so the loop does work, my apologies for not being clear, English is my 3 language.
Last edited on don't

double

or

space

your

code

it

makes it

hard

to Just move

`cout << "Student " << count + 1 <<endl;`

to outside the if statement. That way it will run every time.

 ``123456789101112131415161718`` ``````/ for ( int count = 0; count < ARRAY_SIZE; count++ ) { cout << "Student " << count + 1 <> studentGPA[count]; } else { cout << "Enter in the students name, starting with the last name : "; cin.ignore(); getline (cin, studentTotal[count]); cout << " student GPA: "; cin >> studentGPA[count]; } }`````` thanks @bevilman gave it a try and it works, Interesting.. Thank you so much.
Last edited on
Topic archived. No new replies allowed.