goto (looping) error

I just want to ask what does this error means "goto bypasses initialization of a local variable".

Here is the part of my code where I used the goto, please tell me what's wrong with it
1
2
3
4
5
6
7
8
9
10
11
12
 ask:
{
gotoxy(10,49);
cout<<"Do you want to restart? Y/N";
choice=getch();
}
}
while(choice=='y'||choice=='Y');
if(choice=='n'||choice=='N')
return 0;
else
goto ask;


and also tell me if you want to see the whole source code.. :))
It means that goto transfers the control inside a code block that uses some local variable (I think in your case such a vatiable is choice) bypassing its initialization.
You should forget that in C++ there is operator goto and never use it.
Last edited on
Actually, I used that before and it works..
Below is the program where goto works just fine.

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
#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
int num1,num2;
unsigned char choice;
do
{
clrscr();
gotoxy(25,15);
cout<<"SUM CALCULATOR";
gotoxy(20,18);
cout<<"Enter first number:     ";
cin>>num1;
gotoxy(20,20);
cout<<"Enter second number:    ";
cin>>num2;
gotoxy(22,22);
cout<<"The Sum is         "<<num1+num2;
ask:
{
gotoxy(18,25);
cout<<"Do you want to solve again? Y/N";
choice=getch();
}
}
while(choice=='y'||choice=='Y');
if(choice=='n'||choice=='N')
return 0;
else
goto ask;

}


and here is the whole source code of the program that I'm working off where it says error about goto 'goto bypasses initialization of a local variable"

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
float att1,att2,r1,r2,q1,q2,prj,mid;
char choice;
do
{
clrscr();
gotoxy(28,5);
cout<<"PROGRAM#3 (GRADES)";
gotoxy(29,8);
cout<<"Enter attendance";
gotoxy(25,11);
cout<<"Att1                 ";
cin>>att1;
gotoxy(25,13);
cout<<"Att2                 ";
cin>>att2;
gotoxy(27,16);
cout<<"Enter the recitation";
gotoxy(25,19);
cout<<"R1                   ";
cin>>r1;
gotoxy(25,21);
cout<<"R2                   ";
cin>>r2;
gotoxy(30,24);
cout<<"Enter Quizzes";
gotoxy(25,27);
cout<<"Q1                   ";
cin>>q1;
gotoxy(25,29);
cout<<"Q2                   ";
cin>>q2;
gotoxy(31,32);
cout<<"The project";
gotoxy(25,35);
cout<<"Project              ";
cin>>prj;
gotoxy(25,39);
cout<<"Midterm exam         ";
cin>>mid;
gotoxy(25,42);
float CS=(((att1+att2)/2)*.1)+(((r1+r2)/2)*.3)+(((q1+q2)/2)*.4)+(prj*.2);
cout<<"CLASS STANDING:      "<<CS;
gotoxy(25,44);
float PG=((CS*2)+mid)/3;
cout<<"PERIODIC GRADE:      "<<PG;

if(PG>=96&&PG<=100)
{gotoxy(25,46);
cout<<"RATING SYSTEM:       1.0";}
else if(PG>=94&&PG<=95)
{gotoxy(25,46);
cout<<"RATING SYSTEM:       1.25";}
else if(PG>=92&&PG<=93)
{gotoxy(25,46);
cout<<"RATING SYSTEM:       1.50";}
else if(PG>=89&&PG<=91)
{gotoxy(25,46);
cout<<"RATING SYSTEM:       1.75";}
else if(PG>=87&&PG<=88)
{gotoxy(25,46);
cout<<"RATING SYSTEM:       2.0";}
else if(PG>=84&&PG<=86)
{gotoxy(25,46);
cout<<"RATING SYSTEM:       2.25";}
else if(PG>=80&&PG<=83)
{gotoxy(25,46);
cout<<"RATING SYSTEM:       2.50";}
else if(PG>=78&&PG<=79)
{gotoxy(25,46);
cout<<"RATING SYSTEM:       2.75";}
else if(PG>=75&&PG<=77)
{gotoxy(25,46);
cout<<"RATING SYSTEM:       3.0";}
else if(PG>=70&&PG<=74)
{gotoxy(25,46);
cout<<"RATING SYSTEM:       FAILED";}
else
{gotoxy(25,46);
cout<<"RATING SYSTEM:       INPUT ERROR";}
ask:
{
gotoxy(10,49);
cout<<"Do you want to restart? Y/N";
choice=getch();
}
}
while(choice=='y'||choice=='Y');
if(choice=='n'||choice=='N')
return 0;
else
goto ask;
}


I tried to remove the choice=getch(); and replaec it with the simple cin>>choice; but it still shows error.
And if you know an alternative to be used instead of goto, please tell me..
what I'm trying to do is when the user press any key except for 'y' and 'n' , it will just stay in the question until the user press either 'y' or 'n' key.
Last edited on
In the second program inside the do while statement where you pass the control by using goto there are definitions of local variables

float CS=(((att1+att2)/2)*.1)+(((r1+r2)/2)*.3)+(((q1+q2)/2)*.4)+(prj*.2);
cout<<"CLASS STANDING: "<<CS;
gotoxy(25,44);
float PG=((CS*2)+mid)/3;

It is forbidden to pass the control to a block bypassing local variable definitions that have initializers.

One more never use goto statement. Your code is very bad.
Last edited on
so what should i use instead of goto ?
You should use loops as while, do-while and for.
Last edited on
Thank you very much for your help @vlad from moscow. My problem is now solved. Thank you again, I have learned new things about C++ (about goto is a bad code) coz I'm just new to C++. :))

But please anyone tell/help me how to put while loop or other loops to my code instead of goto .... (to remain in the question "Do you want restart? Y/N" if any key beside 'y' and 'n' was entered)
Last edited on
Please anyone tell/help me how would I put while loop or other loops to my code instead of goto .... (to remain in the question "Do you want restart? Y/N" if any key beside 'y' and 'n' were entered)
Please...I really need it..
Thank you in advance to those who will answe.. :)
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
#include <iostream.h>
#include <conio.h>

main()
{
   clrscr();

   unsigned char choice;

   do
   {
      clrscr();

      gotoxy( 25, 15 );
      cout << "SUM CALCULATOR";

      gotoxy( 20, 18 );
      cout << "Enter first number:     ";

      int num1;
      cin >> num1;

      gotoxy( 20, 20 );
      cout << "Enter second number:    ";

      int num2;
      cin >> num2;

      gotoxy( 22, 22 );
      cout << "The Sum is         " << num1 + num2;

      gotoxy( 18, 25 );
      cout << "Do you want to solve again? Y/N";
      choice=getch();
   } while ( choice == 'y' || choice == 'Y' );

   return 0;
}
Topic archived. No new replies allowed.