#include <iostream>
usingnamespace std;
int main()
{
char usr[20];
int score;
int retry;
cout<<"Please enter your name.\n";
cout<<">>. ";
cin>>usr;
cout<<endl;
selection:
cout<<"Hello "<<usr <<". Please enter your score based on 0 - 100.\n";
cout<<">>. ";
cin>>score;
cout<<endl;
cin.ignore();
if (score <= 59) {
cout<<"Your score is equivelant to F.\n";
cin.get();
cout<<"Select the number of the desired option.\n";
cout<<"1. Try another result\n";
cout<<"2. Exit the program\n";
cout<<">>. ";
cin>>retry;
switch (retry) {
case 1:
goto selection;
case 2:
goto exit;
}
}
elseif (score <= 69) {
cout<<"Your score is equivelant to D.";
cin.get();
cout<<"Select the number of the desired option.\n";
cout<<"1. Try another result\n";
cout<<"2. Exit the program\n";
cout<<">>. ";
cin>>retry;
switch (retry) {
case 1:
goto selection;
case 2:
goto exit;
}
}
elseif (score <= 79) {
cout<<"Your score is equivelant to C.";
cin.get();
cout<<"Select the number of the desired option.\n";
cout<<"1. Try another result\n";
cout<<"2. Exit the program\n";
cout<<">>. ";
cin>>retry;
switch (retry) {
case 1:
goto selection;
case 2:
goto exit;
}
}
elseif (score <= 89) {
cout<<"Your score is equivelant to B.";
cin.get();
cout<<"Select the number of the desired option.\n";
cout<<"1. Try another result\n";
cout<<"2. Exit the program\n";
cout<<">>. ";
cin>>retry;
switch (retry) {
case 1:
goto selection;
case 2:
goto exit;
}
}
elseif (score <= 99) {
cout<<"Your score is equivelant to A.";
cin.get();
cout<<"Select the number of the desired option.\n";
cout<<"1. Try another result\n";
cout<<"2. Exit the program\n";
cout<<">>. ";
cin>>retry;
switch (retry) {
case 1:
goto selection;
case 2:
goto exit;
}
}
elseif (score == 100) {
cout<<"Congratulations, You got a full mark.";
cin.get();
cout<<"Select the number of the desired option.\n";
cout<<"1. Try another result\n";
cout<<"2. Exit the program\n";
cout<<">>. ";
cin>>retry;
switch (retry) {
case 1:
goto selection;
case 2:
goto exit;
}
}
else {
cout<<"The number you enterd was invalid. Do you wish to try again?\n";
cout<<"1. Yes\n";
cout<<"2. No\n";
cout<<">>. ";
cin>>retry;
}
cin.get();
exit:
return 0;
}
Here is my version of your program with some modifications...
Update: 12/15/2010 :: I tested it, made some corrections and added something new, it works if you want to try it and learn something from it.
BUG: If you enter both a first name and last name in the ask name prompt, the program will crash or simply go crazy, I know how to fix this but i'll leave that part for anyone who wants the challenge. :)
#include <iostream>
#include <string>
#define QUIT 3
// namespaces
using std::cout;
using std::cin;
using std::string;
/* the following class represents a student and his/her grade score. */
class Student
{
public:
// constructor, score is 0, we have no name
Student() { iGradeScore = 0; bHaveName = false; }
// member functions
void AskName();
void AskScore();
string ScoreResult();
int Menu();
protected:
string szStudentName; // the student's name
int iGradeScore; // the student's score
bool bHaveName; // do we have the student's name or not?
};
void Student::AskName()
/* ask for the student's name */
{
cout << "\nPlease enter your name: ";
cin >> szStudentName;
bHaveName = true;
}
void Student::AskScore()
/* ask for the student's score */
{
cout << "\n\nPlease enter your grade score (0 to 100): ";
cin >> iGradeScore;
}
int Student::Menu()
/* main menu for program. */
{
if (!bHaveName) return QUIT; // QUIT if no name is present.
cout << "\n-------------------------------------------------\n";
cout << "1. Enter score to evaluate.\n";
cout << "2. Change your name.\n";
cout << "3. Quit program.\n";
cout << "-------------------------------------------------\n";
cout << " Active Student; " << szStudentName << "\n";
cout << "-------------------------------------------------\n";
cout << ":: ";
int Selection;
cin >> Selection;
return Selection; // return the selection chosen by the user
}
string Student::ScoreResult()
/* look at the variable iGradeScore and return a string based on the
evaluation of that grade score. */
{
string Evaluation;
if (iGradeScore > 90) Evaluation = "Your score is equivalent to an A.\n\n";
if (iGradeScore > 80 && iGradeScore < 90) Evaluation = "Your score is equivalent to a B.\n\n";
if (iGradeScore > 70 && iGradeScore < 80) Evaluation = "Your score is equivalent to a C.\n\n";
if (iGradeScore > 60 && iGradeScore < 70) Evaluation = "Your score is equivalent to a D.\n\n";
if (iGradeScore < 60) Evaluation = "Oh man, you failed! Study harder!\n\n";
return Evaluation;
}
int main()
/* main program begins here */
{
Student A_Student; // create a student object
cout << "Welcome to the grading program!!\n\n";
A_Student.AskName(); // ask for the name of the student.
/* Keep the program running until the user exits. */
bool bQuit = false; // used to quit loop
int MenuSelection; // used to hold menu selection chosen by user
while (!bQuit)
{
MenuSelection = A_Student.Menu(); // obtain a selection from student menu
if (MenuSelection == 1)
{ // the student wants to see a score grade
A_Student.AskScore(); // ask for the score
cout << A_Student.ScoreResult(); // show the score grade
}
if (MenuSelection == 2)
{ // student wants to change name.
A_Student.AskName(); // ask for new name
}
if (MenuSelection == QUIT) bQuit = true; // quit the loop
MenuSelection = 0; // reset before returning to top.
}
return 0; // end the program
}
GOTO statements are ugly, bad to use, get into the habit of writing functions or classes when you realize that you are repeating code over and over again.
GOTO statements were put into C\C++ for people who learned to program using them in other languages. The C family has functions and logic loops so GOTO is redundent and makes for ugly code.
Lines 66 - 79 in mrfaosfx code by the way can be rewritten like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
string Student::ScoreResult()
/* look at the variable iGradeScore and return a string based on the
evaluation of that grade score. */
{
string Evaluation;
if (iGradeScore > 90) {Evaluation = "Your score is equivalent to an A.\n\n"; return Evaluation;}
if (iGradeScore > 80) {Evaluation = "Your score is equivalent to a B.\n\n"; return Evaluation;}
if (iGradeScore > 70) {Evaluation = "Your score is equivalent to a C.\n\n"; return Evaluation;}
if (iGradeScore > 60) {Evaluation = "Your score is equivalent to a D.\n\n"; return Evaluation;}
if (iGradeScore < 60) {Evaluation = "Oh man, you failed! Study harder!\n\n"; return Evaluation;}
} //End of string Student::ScoreResult()
I just really wanted to point this out to show how you can use multiple return statements in a function to get rid of all of the "if and if" crap.
Goto is an actual jump, rather than a structure. It has scope issues that don't arise with proper loops, functions and other structures. Goto is "left-over C functionality" that was only kept in C++ to maintain support for older C code.
EDIT:
Just read Computergeek's post, and I felt like responding:
1 2 3 4 5 6 7 8
string Student::ScoreResult()
{
if (iGradeScore > 90) return std::string("Your score is equivalent to an A.\n\n");
if (iGradeScore > 80) return std::string("Your score is equivalent to a B.\n\n");
if (iGradeScore > 70) return std::string("Your score is equivalent to a C.\n\n");
if (iGradeScore >= 60) return std::string("Your score is equivalent to a D.\n\n"); // Changed to >=, since iGradeScore 60 would give no result
if (iGradeScore < 60) return std::string("Oh man, you failed! Study harder!\n\n");
}
yeah I noticed that but I ignored it. It works but then again, it looks confusing to anyong attemping to decipher that mess, pardon my words ... some things you can get away at the risk of confusion.
So whats that thing about the "E"?
and one more last question, if I wanted to go from one part of my code to another, how could I do that without using the GOTO statement??
To put it in simple terms...you SHOULD NOT be going to another part of your program, part of WHY your using GOTO statements is because you are thinking this way. You need to think in terms of program flow and how you can write functions and classes that do the things you want done so that you dont have to use GOTO statements.
I'll give an example.
1 2 3 4 5 6 7 8 9
start:
goto askname:
askname:
cout << "what is your name"char somename;
cin >> somename;
goto start:
that looks very ugly and I could have just written a function that does this...
1 2 3 4
void askname(char somename)
{
cout << "what is your name";
}
and I can call this function any where in the program to ask for a name,
askname(putnamehere); or make the function return a value, etc...whatever.
C++ Primer Plus .... look for it in a book store, its a blue book and trust me, once your done reading it, it will all make sense to you in a way you've never imagined.
True, goto should be ignored, but not because of being ugly. The only issues I can see with them is that they can get very complicated very quickly, and they lack scoping rules. This means that you can get nasty errors if you try to jump from the inside of a loop to the outside, for example. Try using loops, if's, elses, switches, breaks, continues and functions to simulate the same behavior; this is the "way to go".
The only reason goto is still usable in C++, is because it was used in older C code. Since there's still a lot of C programs that used this, the statement was left for backward-compatibility.
Mrfaosfx, my code was not really meant to be a practical code, it was just there to illustrate that reducing code is not always good.