While loop and sentinel values

My while loop is operating, but sentinel value adds to total

Page 1 of 1

3 Replies - 8945 Views - Last Post: 18 September 2009 - 08:08 PM Rate Topic: -----

#1 rondell  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-September 09

While loop and sentinel values

Posted 18 September 2009 - 07:31 PM

My while loop is working as well as the sentinel value stopping the loop. The problem is that the sentinel value is being added onto the total at the end, interfering with the number of courses as well as the final GPA. Any help to see what i am doing incorrectly would be greatly appreciated. Thanks.

#include <iostream>
using namespace std;

int main ()
{
//declare variables
int Total = 1;
float GPA = 0.0, Grade = 0.0;

//get grade input

   Total = Total + Grade;
   Grade++;
   while (Grade != -1)

//figure out gpa
   if (Grade > 0)
      {
      cout << "Enter grade and end list with -1: \n";
      cin >> Grade;
        if (Grade > 90)
        {
        cout << "GPA is 4.0\n";
        }
        else if (Grade >= 80)
        {
        cout << "GPA is 3.0\n";
        }
        else
           cout << "GPA is 2.0\n";
      }
  
//calculate total gpa
//this is where i am having trouble. Overall GPA is only adding in the sentinel value of -1 to courses and to GPA. It won't include the other grades at all. Very Frustrated!

     GPA = GPA / Grade;
     cout << "Overall GPA for " << Grade << " courses," << "is " << GPA <<"\n";

        return 0;
}



//Here is the results I am getting from this code as it is: Enter grade and end list with -1:
77
GPA is 2.0
Enter grade and end list with -1:
88
GPA is 3.0
Enter grade and end list with -1:
99
GPA is 4.0
Enter grade and end list with -1:
100
GPA is 4.0
Enter grade and end list with -1:
-1
GPA is 2.0
Overall GPA for -1 courses,is -0

*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 18 September 2009 - 07:39 PM


Is This A Good Question/Topic? 0
  • +

Replies To: While loop and sentinel values

#2 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: While loop and sentinel values

Posted 18 September 2009 - 07:37 PM

#include <iostream>
using namespace std;

int main ()
{
//declare variables
int Total = 1;
float GPA = 0.0, Grade = 0.0;

//get grade input

Total = Total + Grade;
Grade++;
while (Grade != -1){
	if (Grade > 0){
		cout << "Enter grade and end list with -1: \n";
		cin >> Grade;
	}else{
		if(Grade==-1){
			break;
		}else{
	//figure out gpa
			if (Grade > 90){
				cout << "GPA is 4.0\n";
				}else {
					if (Grade >= 80){
						cout << "GPA is 3.0\n";
					}else
						cout << "GPA is 2.0\n";
			}
		}
	}
}

//calculate total gpa
//this is where i am having trouble. Overall GPA is only adding in the sentinel value of -1 to courses and to GPA. It won't include the other grades at all. Very Frustrated!

GPA = GPA / Grade;
cout << "Overall GPA for " << Grade << " courses," << "is " << GPA <<"\n";
system("pause");

return 0;
}



Your while was missing the '{''}' so basicly you didnt had a while the code above is fixed i hope thats what you wanted xD

[EDIT]Fixed it was still wrong xD
By the way what is the overall supposed to do?

This post has been edited by poncho4all: 18 September 2009 - 07:43 PM

Was This Post Helpful? 0
  • +
  • -

#3 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: While loop and sentinel values

Posted 18 September 2009 - 07:50 PM

 else{
		if(Grade==-1){
			break;
		}else{
	//figure out gpa
			if (Grade > 90){
				cout << "GPA is 4.0\n";
				}else {
					if (Grade >= 80){
						cout << "GPA is 3.0\n";
					}


Do 'else if'. Instead else { if.
Was This Post Helpful? 0
  • +
  • -

#4 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: While loop and sentinel values

Posted 18 September 2009 - 08:08 PM

It looks like you want to increment 'Grade ' for the while loop. Am I wrong? If not you need to increment 'Grade++' inside of the while loop. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1