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
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
usingnamespace std;
int main()
{
// using array to set number of recods, student names and GPA
constint 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 <<endl;
cout << "Enter in the students name, starting with the last name : ";
getline (cin, studentTotal[count]);
cout << "Student GPA: ";
cin >> 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[0] == ' ') 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[0] = toupper(firstName[0]);
// 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[0] = toupper(lastName[0]);
// 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;
}
for ( int count = 0; count < ARRAY_SIZE; count++ )
{
if (count == 0) {
cout << "Student " << count + 1 <<endl;
cout << "Enter in the students name, starting with the last name : ";
getline (cin, studentTotal[count]);
cout << "Student GPA: ";
cin >> 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:
1 2 3 4 5 6 7 8 9 10 11 12 13
for ( int count = 0; count < ARRAY_SIZE; count++ )
{
if (count == 0) {
cout << "Student " << count + 1 <<endl;
} else {
cin.ignore();
}
cout << "Enter in the students name, starting with the last name : ";
getline (cin, studentTotal[count]);
cout << " student GPA: ";
cin >> studentGPA[count];
}
Why do you have line 4 only for the first student?
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?
to outside the if statement. That way it will run every time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/
for ( int count = 0; count < ARRAY_SIZE; count++ ) {
cout << "Student " << count + 1 <<endl;
if (count == 0) {
cout << "Enter in the students name, starting with the last name : ";
getline (cin, studentTotal[count]);
cout << "Student GPA: ";
cin >> 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];
}
}