help with while loops

So for my lap the user has to input several grades ( A A B C ect.)in a single line and the program has to calculate their gpa. I am at a loss on how to do this. The current code I have at the moment is just me trying to calculate the number of a inputted but it wont go past 1. we can only use while loops for this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main() {
   char letter;
   int NA = 0; 
   std::cout << "Enter grades (Z terminates the list): ";
   std::cin >> letter;
    while ( letter != 'z' || letter != 'Z') {
        if (letter == 'A' || letter == 'a') 
            NA++;
        else (letter == 'Z' || letter == 'z');
            break;
}
   std::cout << "a is " << NA << '\n';  
}
Hello gatsu,

Welcome to the forum.

I believe this is more of what you want. read the comments n the program.

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
#include <iostream>
#include <cctype>  // <--- Added.

int main()
{
	char letter;
	int NA = 0;
	std::cout << "Enter grades (Z terminates the list): ";
	std::cin >> letter;

	letter = std::toupper(letter);  // <--- Added.

	while (/*letter != 'z' && */letter != 'Z')  // <---Using != the && is the choice that works here.
	{
		if (letter == 'A'/* || letter == 'a'*/)  // <--- Only upper case is needed here as with above.
			NA++;
		// <--- Not really needed as the while condition does this first.
		//else if(letter == 'Z' || letter == 'z')  // <---Remove ; and make else if
		// Not part of the original else. Causes the loop to break on the first pass.
		//	break;

		std::cout << "Enter grades (Z terminates the list): ";  // <--- Added.
		std::cin >> letter;    // <--- Added.
	}

	std::cout << "\na is " << NA << '\n';

	return 0;
}


Hope that helps,

Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cctype>

int main()
{
   char letter;
   int NA = 0; 
   double GPA = 0.0;

   std::cout << "Enter grades (Z terminates the list): ";
   while ( std::cin >> letter && std::toupper( letter ) != 'Z' )
   {
      NA++;         // increment number of grades
//    GPA += ....   // whatever you do to turn grades into a numerical score
   }

// Further processing on GPA and NA (e.g. find average)

   std::cout << "Number of grades read = " << NA << '\n';
   std::cout << "GPA =" << GPA << '\n';                        // calculations needed above
}
Registered users can post here. Sign in or register to post.