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



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <iostream> 
#include <string> 
#include <cmath> 
#include <iomanip>



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 <<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;

}
 

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

Is this the loop that you talk about:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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?
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?
1
2
3
4
5
6
7
8
9
for ( int count = 0; count < ARRAY_SIZE; count++ )
{
  cout << "Student " << count + 1 <<endl;
  cin.ignore();
  cout << "Enter in the students name, starting with the last name : ";
  getline (cin, studentTotal[count]);
  cout << " student GPA: ";
  cin >> 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
Please

don't

double



or



quadruple

space

your

code



it



makes it

hard

to

read.
Just move

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

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];
 }
}
thanks @bevilman gave it a try and it works, Interesting.. Thank you so much.
Last edited on
Topic archived. No new replies allowed.