Help please.

Hello, this is my first time using this website. I have bugs in my code, issues with the goto statement because it makes my program go into a continuous loop when I put a letter i.e."r".
I will put all of my code up hopefully you guys can help me out. :D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  //Created by Matthew James
//3/7/2015
//School Grades
#include <iostream>
#include <conio.h>

using namespace std;

int main()

{
    //variables
double number=0;
char choice;
tryagain: //goto statement line 59
//welcome user to program
cout<<"\t\tHello, welcome to the School Grading program.\n\n";
//ask to insert number
cout   << "Please insert your grade number into system\nto find";
 cout   <<" out what letter grade you received on exam.\n";
cin>> number;
//make loop if statement number is negative or above 100
while (number<0||number>100)
{
    cout<<"please insert correct number."<<endl;
    cin>> number;
    break;
}

//if/else statements
if (number>=90&&number<=100)
{
    cout<<"Congratulations, you received an A."<<endl;
}

else if (number>=80&&number<=89)
{
    cout<<"Good job, you received a B."<<endl;
}
else if (number>=70&&number<=79)
{
    cout<<"You recieved a C."<<endl;
}
else if (number>=60&&number<=69)
{
    cout<<"You got a D."<<endl;
}
else if(number<=59)
{
    cout<<"You got an F ."<<endl;
}

//ask if user wants to restart program
cout<<"\n\n\t\tWould you like to restart program? Y/N\n\n";
cin>>choice;

//use goto statement to rerun program if user requests to do so
if (choice=='Y'||choice=='y')
{goto tryagain;} //goto statement label line 15

else if(choice!='Y'||choice!='y')
{cout<<"\n\n\n\t\t Thank you for taking the time to use this program.\n";
cout<<" \t\t I hope this will help you in your future endeavors."<<endl;}

getch();
return 0;

}
Why are you using goto at all? It is one of the most well-known bad programming practices - don't use it. It only has one extremely rare use case in C++.
Using goto is just a different style of programming.

But I would only use it for escaping out of nested loops.

Your example can replace the goto with a for loop with a bool.
Using goto is just a different style of programming.

well yes... obviously... but its still not good to use. i wouldnt use it to break out of nested loops. just use a boolean. the only good use case ive seen for it is duff's device
Little Bobby Tables wrote:
i wouldnt use it to break out of nested loops.
It is actually the cleanest solution in that case, and it is the "one extremely rare use case" I was talking about.
Here is a link to the code:

http://cpp.sh/2hz5


Basicly, instead of parsing the input right into the double number variable, which can act weird if the user inputs something that cant be converted into double, read it into a string, and then use std:stod(string) to convert it.

From what I understood, it should throw an exception and end the program if the input is invalid.

I also fixed your code for reading and verifying the input, removed the break statement that would allow invalid input on the second try, and added a line on the yes check, to reset the value of number to -1.

I would advise implementing it without the goto as the others have said. Its not a good idea. On this small program it works, but it can become a mess real quick when the size of the solution increases.

Hope it helps!
thank you all for helping me out, I have to brush up on my c++ and focus on using proper while/for/do loops for the situation. And I will note to use goto statements solely for nested loops.

:D
Topic archived. No new replies allowed.