Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,498 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,799 people online right now. Registration is fast and FREE... Join Now!




Validating User Input - Grades

 
Reply to this topicStart new topic

Validating User Input - Grades

Mo821
post 13 Dec, 2005 - 05:30 PM
Post #1


New D.I.C Head

*
Joined: 10 Dec, 2005
Posts: 8


My Contributions


• Program will prompt user for six grades to be entered to be entered( one at a time)
• Read in each grade entered by user and store them in an array of six elements 9 what type?)
• Use a loop
• Grades should be 0 - 4.0(inclusive) scale, and programs should accept grades withn the range

2.
a. average grade 0 - 4.0scale), by looping through the array again
b. final letter grade according to the following equivaleneces:

3.2 < grade <= 4.0 : A
2.4 < grade <= 3.2 : B
1.6 < grade <= 2.4 : C
0.8 < grade <= 1.6 : D
0 <= grade <= 0.8 : F

3. The program will write the following outputotthe screen:
a. sentence with the average grade, on a 0.0 – 4.0 scale.
b. sentence indicating the students final letter grade

MY CODE
____________

#include <iostream>
using namespace std;
int main () {

int n;
double average;

cout << "Enter the number of grades to be averaged: ";
cin >> n;
double arr[n];

while (n <= 0) {
cout << "The number of grades must be greater than zero ";
cout << "- please re-enter: ";
cin >> n;
}
double total = 0;

for(int i = 1; i <= n; i++){
cout << "Please enter grade #" << i << ": ";
cin >> arr[i];
while (i < 0.0 || i > 4.0) {
cout << "Invalid grade - please reenter a grade ";
cout << "between 0 and 4.0 inclusive: ";
cin >> arr[i];
break;
}
total = total + arr[i];
}

average = total / n;
cout << n << " grades were entered and the average is " << average;
cout << endl;

if (average > 3.2 && average <= 4.0)
cout << "The final letter grade is A.";
else if (average > 2.4 && average <= 3.2)
cout << "The final letter grade is B.";
else if (average > 1.6 && average <= 2.4)
cout << "The final letter grade is C.";
else if (average > 0.8 && average <= 1.6)
cout << "The final letter grade is D.";
else if (average >= 0 && average <= 0.8)
cout << "The final letter grade is F.";

cout << endl;

if i enter like 5.1, its not invalid, what did i do wrong?.

Thanks for your help everyone, you are truly amazing!

Mo
User is offlineProfile CardPM

Go to the top of the page


Amadeus
post 13 Dec, 2005 - 05:40 PM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,158



Thanked 31 times

Dream Kudos: 25
My Contributions


In this section, you are taking use input an putting it in an array here:
CODE

cin >> arr[i];
while (i < 0.0 || i > 4.0) {

You place the value in arr at index i, but then use the value of i, which is simply a counter, to trigger the loop. the first time through the for loop, i = 1, which compares fine. you need to use the value of arr[i] as the control variable.

As an fyi, your for loop starts at 1, while an array starts at index zero...you will not populate the first element.
User is offlineProfile CardPM

Go to the top of the page

damoun
post 13 Dec, 2005 - 08:13 PM
Post #3


New D.I.C Head

*
Joined: 6 Aug, 2005
Posts: 36

Please note that: your code should not even compile!!!!

in the line where you prompt the user to enter the number of grades to be averaged, you are trying the set the size of a statically binded array
,double arr[n] , at run time which is a fatal error!

please note that the size of a statically binded arrays must be known at compile time, either implicitly or explicitly.

and note that the size of an statically binded array can not be the value of a varibale even if you think the value of the varibale seems to be known at compile time. therefore:

const int ARRAY_SIZE = 1;
double arr[ARRAY_SIZE]; //valid, size sepcified explicitly
double arr[2]; //valid, size specefied explicitly
double arr[] = {1.0 , 20.5 , 12.3}; //valid, size specefied implicitly
also size can be an enumeration

but
int n = 12;
double arr[n]; //error, don't do this
///////////////////////////////////////////////////////////////////////////////

now if you want to set the size of the array at run time you have to use dynamically binded arrays, that is arrays that are allocated on the free store using the new operator.
here is what you would do:

std::cout << "Enter the number of grades: ";
int arr_size;
std::cin >> arr_size;
double * arr = new double[arr_size];

and you can use the pointer arr , exactly as you would use the array name arr, with the subscript [] , operator.

combine this with Amadeus post and I think your code should work properly. (I hope)
Good Luck
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 12:19AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month