Thread: confusion on goto

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    4

    Post confusion on goto

    i have coded the program in such a way that untill the user types y key the program will not be terminated.
    Code:
    one question is asked that do you like the program(y/n?):
    untill you press y it wont end.
    but the problem is that i have to press y 2 times to terminate the programme or have to press n 2 times to make the statement appear again . what shall i do?
    i have coded the program like this:
    #include<iostream.h>
    #include<conio.h>
    #include<stdio.h>
    void main()
    {
    char y,n,option;
    clrscr();
    label:
    cout<<"do u like the program(y/n?):";
    cin>>option;
    option=getchar();
    if(option=='y')
    {
    cout<<"thnx";
    cin.get();
    getch();
    }
    else
    goto label;
    }
    plzzz help.
    thnx in advance.........

  2. #2
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Firstly, and most importantly, don't use goto's... ever. A while loop would do the same thing and look much neater, too.

    Secondly, you check the same thing over and over. ie you always check option, but you never change what it is. And you probably don't need to use cin.get() since the program pauses for getche() anyway. I think what you want is something like this:

    Code:
    while ( getche() == 'y' )
    {
    	cout<<"thnx";
    }
    Other little things:
    You never use the variable y and n, so there's no need to declare them.
    You don't need the .h and the end of iostream.h.
    Don't do void main()... it should be int main().
    There's no need to clear the screen at the start of the program because nothing has been drawn yet.
    You should format your code properly to make it easier for people to read it (ie have neccassary spacing on lines).
    Last edited by yaya; 04-18-2010 at 02:57 AM.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Actually, he changes option *twice*. Get rid of the option = getchar() line. Also, indent your code. I didn't bother to read your code properly, since it's not indented.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. add,view=ok, search,edit got error... help!
    By private0430 in forum C Programming
    Replies: 4
    Last Post: 09-27-2009, 01:36 PM
  2. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  3. Does goto have a glitch or...?
    By Blackroot in forum C++ Programming
    Replies: 9
    Last Post: 02-18-2006, 10:40 AM
  4. helpppp
    By The Brain in forum C Programming
    Replies: 1
    Last Post: 07-27-2005, 07:05 PM
  5. Need some help with a basic tic tac toe game
    By darkshadow in forum C Programming
    Replies: 1
    Last Post: 05-12-2002, 04:21 PM