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

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




input validation

5 Pages V  1 2 3 > »   
Reply to this topicStart new topic

input validation, grade inputting

C++2b
post 16 Dec, 2005 - 05:37 PM
Post #1


New D.I.C Head

*
Joined: 16 Dec, 2005
Posts: 31


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 ((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 output to the 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 array [6] ----------- is this correct? what has to be stored ( what ype, pass by refrence or value)
double average;

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

while (n <= 0)// is this correct {
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];--put the total 6 in here[FONT=Times][SIZE=7][SIZE=7]
}

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 4.5, its not invalid, whats incorrect? was it the while loop or the incremnt operators?.

Thank you everyone! your total lifesavers

Dan
User is offlineProfile CardPM

Go to the top of the page


Architect
post 16 Dec, 2005 - 05:44 PM
Post #2


New D.I.C Head

*
Joined: 15 Dec, 2005
Posts: 26


My Contributions


Hey Dan,

I have one question before I look further into your code. Are the grades to be entered decimal numbers (3.1, 4.0, etc..) or actual integer values?

If they're decimals, I would start by changing your array to hold float variables instead of integers.

-Architect
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 16 Dec, 2005 - 05:49 PM
Post #3


g++ -o drink whiskey.cpp

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



Thanked 31 times

Dream Kudos: 25
My Contributions


This is the same as the last post...it is not catching that input as invalid becasue you are not using that input for comparing. you are taking the user input and placing it in an array:
CODE

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

but then do the comparison on i...which is only a counter variable. You need to compare the proper variable.
CODE

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

Although in a perfect world, you should really check the value before putting it in the array.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 16 Dec, 2005 - 05:51 PM
Post #4


g++ -o drink whiskey.cpp

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



Thanked 31 times

Dream Kudos: 25
My Contributions


Here is the original post...worded suspiciously similar.
User is offlineProfile CardPM

Go to the top of the page

C++2b
post 16 Dec, 2005 - 07:57 PM
Post #5


New D.I.C Head

*
Joined: 16 Dec, 2005
Posts: 31


My Contributions


now entering grades for example
2.3
2.4
2.9

getting a grade A( should be like C), which shouldn't be the case. is it a loop problem.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 16 Dec, 2005 - 08:49 PM
Post #6


g++ -o drink whiskey.cpp

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



Thanked 31 times

Dream Kudos: 25
My Contributions


No, it's a condition problem
CODE

if (average < 3.2 && average <= 4.0)

2.9 is less than 3.2 and less than 4.0...therefore the condition is met. you need to test if it's in between.

As an fyi, each of your conditions is structured like this.

The top one should be
CODE

if (average > 3.2 && average <= 4.0)

You can modify the rest to macth.
User is offlineProfile CardPM

Go to the top of the page

damoun
post 17 Dec, 2005 - 03:09 AM
Post #7


New D.I.C Head

*
Joined: 6 Aug, 2005
Posts: 36

Amadeus is right, this is exactly like the last post. just read the last post.
here you are making the same mistake again, trying to specify the size of a statically binded array with a varible.

cout << "Enter the number of grades to be averaged: ";
cin >> n;
double arr[n];
------------------------------------------------------------------------
you should do this:
int n;
cout << "Enter the number of grades to be averaged: ";
cin >> n;
double * arr = new double[n];


by the way are you using Big C++(by Cay Horstmann) I think that is where I have seen that problem before. may I sugest,
C++ primer plus( by stephen prata) best beginers book I have ever read!
User is offlineProfile CardPM

Go to the top of the page

C++2b
post 17 Dec, 2005 - 09:42 AM
Post #8


New D.I.C Head

*
Joined: 16 Dec, 2005
Posts: 31


My Contributions


to post array of 6 elements(what ype, pass by refrence or value) will it be int array [6]. What should be stored in the array?

Also, as u see i am new to programming and would appreciate books that help me out in being a better programmer. I thank you once again.
User is offlineProfile CardPM

Go to the top of the page

Mrafcho001
post 17 Dec, 2005 - 10:32 AM
Post #9


D.I.C Addict

Group Icon
Joined: 1 Nov, 2005
Posts: 753



Thanked 5 times

Dream Kudos: 120
My Contributions


Always pass arrays by reference, i believe you cannot pass arrays by value.

You lost me after that.

Also another really good (Probably the best) C++ book is The C++ Programming Language 3rd Edition - Bjarne Stroustrup

He created the language, so i would think he knows what he is doing. Also he gets in depth with some of the topics, which is really nice.
User is offlineProfile CardPM

Go to the top of the page

C++2b
post 17 Dec, 2005 - 11:17 AM
Post #10


New D.I.C Head

*
Joined: 16 Dec, 2005
Posts: 31


My Contributions


it says to read in each grade entered by uer, store them into array of 6 elements .

int [6] = { somevalue/grade & - for example}
User is offlineProfile CardPM

Go to the top of the page

C++2b
post 17 Dec, 2005 - 11:45 AM
Post #11


New D.I.C Head

*
Joined: 16 Dec, 2005
Posts: 31


My Contributions


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

is this line necessary to put in code, i really don't think so
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 17 Dec, 2005 - 02:30 PM
Post #12


g++ -o drink whiskey.cpp

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



Thanked 31 times

Dream Kudos: 25
My Contributions


QUOTE(C @ +2b+17 Dec, 2005 - 03:42 PM)
while (n <= 0) {
cout << "The number of grades must be greater than zero "; / cout << "- please re-enter: ";
cin >> n;
}

is this line necessary to put in code, i really don't think so

Well, it would be necessary if your program assignment was to prompt the user for the number of gradres to be entered, but it is not. You are supposed to prompt the user for 6 grades...therefore you do not really need to check, or even ask for, the number of grades being entered.
User is offlineProfile CardPM

Go to the top of the page

5 Pages V  1 2 3 > » 
Reply to this topicStart new topic
Time is now: 11/20/08 01:05AM

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