PDA

View Full Version : C++ programming question



Calvin74
01-16-2010, 17:17
I know we have some programmers here that can help me out if they read this:)

I am taking a basic C++ class and was working on just a small program for our 1st project.
It is a program that the user inputs their name, the number of test scores they are going to enter. After that it figures out the high score low score and the average. I have the math and everything done but was working on getting something to make sure they are inputting the correct things.

Where it asks for the total number of test scores is there a way to check and make sure they enter a number? I have a while loop to make sure they enter a positive number but i get some funky errors if i enter in a string. Thanks.

Calvin74
01-16-2010, 17:22
also not sure if this should be in the spam forum or gen discussion
if it can go in the general discussion devil please use your awesome mod powers to move it:). thanks

555555
01-16-2010, 22:42
consider checking is it is a integer?

http://www.dreamincode.net/code/snippet591.htm

Calvin74
01-17-2010, 00:22
consider checking is it is a integer?

http://www.dreamincode.net/code/snippet591.htm

I will put all the code I have since I know I am missing something.

It checks it is is an integer but then doesn't stop to let them enter the correct integer. Also if I put in a negative number it just jumps straight to the end.

I have code that works for negative numbers but it is in a comment right now trying to get this to work. Thanks for the suggestion. Also I am sure there is a much easier and neater way for this code to be written but I am an amateur:)


#include <iostream>
using namespace std;
#include <string>
#include <limits>

void main()
{
string nameOfTeacher;
string letterGrade;
int totalTest;
int highScore;
int lowScore;
int totalScore;
double averageScore;
int gradeCounter;
int grade;

cout << "Please enter your name: ";
getline( cin, nameOfTeacher);
cout << endl;

cout << "Please enter the number of test scores to be recorded: ";
cin >> totalTest;
/*

while ( totalTest <= 0 )
{
cout << "Please re-enter the number of test scores to be recorded: ";
cin >> totalTest;
}
*/

cin.ignore(numeric_limits<int>::max(), '\n');

if (!cin || cin.gcount() != 1)
{
cout << "Please enter correct number: ";
cin >> totalTest;
}
else
cout << endl;

totalScore = 0;
gradeCounter = 1;
highScore = 0;
lowScore = 100;

while ( gradeCounter <= totalTest )
{
cout << "Enter grade: ";
cin >> grade;
while ( grade < 0 || grade > 100 )
{
cout << "Please re-enter the grade: ";
cin >> grade;
}
totalScore = totalScore + grade;
gradeCounter = gradeCounter + 1;

if ( grade > highScore)
highScore = grade;
if ( grade < lowScore)
lowScore = grade;
}

averageScore = totalScore / totalTest;

if ( averageScore >= 90 )
letterGrade = " an A.";
else if ( averageScore >= 80 )
letterGrade = " a B.";
else if ( averageScore >= 70 )
letterGrade = " a C.";
else if ( averageScore >= 60 )
letterGrade = " a D.";
else ( averageScore >= 50 ); //Mr. Boone it makes me put the ; here and I am not quite sure why.
letterGrade = " a F.";

cout << "\nFor the " << totalTest << " taken, " << nameOfTeacher << "'s class had a grade average of " << averageScore << " which is " << letterGrade << endl;
cout << "The highest score was a " << highScore << " and the lowest score was a " << lowScore << "." << endl;

Devil
01-17-2010, 23:04
*moved to GD upon request*

Piker
01-18-2010, 01:45
Bah, if it was Java I could help you out here... =\