Public Group

This topic is 4005 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

here's a source code someone can use for reference to the do..while function and the if/else statement.
#include <iostream>

using namespace std;

int main()
{
x,								// variable = to number of grades entered
average;						// total of the grades divided by x (defined below)

total = 0;							// default total
x = 0;								// default # of grades

do
{
cout << "Enter grade, -1 to end: ";

x = x + 1;
}
while ( grade != -1 );				// continue to loop while -1 is not set as input
cin.ignore();						// ignore input

x = x - 1;							// added code to counteract -1
total = total + 1;					// added code to counteract -1

if ( total > -1 )					// proceed only if total is greater than -1
{
average = total / x;
cout << "You average is " << average << "." << endl;
}
else								// proceed to this only if "if" statement = FALSE
{
cout << "No grades were entered." << endl;
}
cin.get();							// receive input at end to keep window active after executing complete
return 0;
}

// All grades will return integers, no decimals will be displayed, nor can you enter decimals


tho the code does work perfectly fine, i would like the code to be a bit shorter. where it's labeled "added code to counteract -1" i should not have to put those, but i can't get it to work that way again. i know i can delete them by setting total to 1 and x to -1, but i shouldn't have to do that either. if someone can find a way around this, that would be great. if not, oh well, at least the code works correctly.

##### Share on other sites
#include <iostream>using namespace std;int main(){	int total,							// added total of grades		grade,							// grade entered for average		x,								// variable = to number of grades entered		average;						// total of the grades divided by x (defined below)	// Initialize the local variables	total = x = 0;	do	{		cout << "Enter grade, -1 to end: ";		cin >> grade;		// if the user input was -1, do not take it into calculation.		if( grade == -1 )			break;		total += grade;		x++;	}	while ( grade != -1 );				// continue to loop while -1 is not set as input	cin.ignore(1000, '\n');						// ignore input	if ( x > 0 )					// proceed only if user has entered at least 1 input	{		average = total / x;		cout << "Your average is " << average << "." << endl;	}	else								// proceed to this only if "if" statement = FALSE	{		cout << "No grades were entered." << endl;	}	cin.get();							// receive input at end to keep window active after executing complete	return 0;}// All grades will return integers, no decimals will be displayed, nor can you enter decimals

Your source code does not work correctly. What if the user enters 5 zeros? The total is still zero, and your code will see this as no user input.

##### Share on other sites
Averaging grades? This sounds like an uber-fun game; can't wait to play it.

##### Share on other sites
cool, thanks for the info Sangha Im. i'm still just starting out, so i didn't know about the "break" function.

1. 1
Rutin
69
2. 2
3. 3
4. 4
5. 5

• 21
• 10
• 33
• 20
• 9
• ### Forum Statistics

• Total Topics
633431
• Total Posts
3011834
• ### Who's Online (See full list)

There are no registered users currently online

×