Blur on.. skipping codings/go to.

This is a discussion on Blur on.. skipping codings/go to. within the C Programming forums, part of the General Programming Boards category; Hello to the community, i've just registered and i'm hoping if anyone can help me on this. Well i'm not ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    3

    Question Blur on.. skipping codings/go to.

    Hello to the community, i've just registered and i'm hoping if anyone can help me on this.

    Well i'm not sure how i should explain my problem.

    first off i'll start by posting my code.

    Code:
     
    it's an airline seats management system.
    
    void FirstMenu() //can also be named "name menu"
    { blah codes here.
    }
    void PurchaseCustomTicket(int seatUserWants)
    {
    int x= seatUserWants-1;
    if(seatings[plane][x]==1){  //if seat has been taken
    printf("\nSeat has already been purchased.Please view the Seating display to identify a seat that you wish to buy.");
    goto gotomenu; // i wish to go to the first line of my main function...
    }
    else
    {blah codings}
    }
    
    void AutoPurchaseEconomyTicket()
    { blah codings}
    
    void airlinesMenu()
    {
    blah codings......
    if(plane<=0||plane>=11)  //entering a valid  plane value
    {
    	printf("Invalid number, please enter the number associated to the flight you wish");
    	airlinesMenu(); //wishes to skip to the 2nd line of my main function.
    }
    else
    {blah codings....}}}
    
    
    void menu(){}
    
    void submenu(){zzz}
    
    
    int GetBSeatNumber(){}
    
    int GetESeatNumber(){}
    
    void DIS(int chairnum) // it's short for display info on seatings
    {}
    
    void DisplaySeating()
    {printf("\n\n"); // just add some lines before displaying main menu again.
    goto gotomenu; //wishes to go to the 1st line of main coding...
    }
    
    void CheckSeats()
    {
    if all seats have been taken
    	printf("All seats have been purchased for this flight.\nRedirecting you to first menu and please select another flight time.");
    	goto airlinesmenu;  // going to the 2nd line main fuction
    } 
    printf("Invalid Input, returning to main menu.\n");  //if there's an invalid input...
    goto gotomenu;  // go to the 2nd line of the main fuction
    	}
    void DisplayBoardingTicket()
    {system"cls");}
    
    ///////////////////////////////////////////////////////////////////////////////////////
    
    void main()
    {
    firstmenu:
    	FirstMenu();
    airlinesmenu:
    	airlinesMenu();
    gotomenu:
    	menu();
    	if (choice=='P')
    	{
    		CheckSeats();
    		submenu();
    		if (choice=='B')
    		{
    		PurchaseCustomTicket(GetBSeatNumber());
    		DisplayBoardingTicket();
    		goto firstmenu;
    		}
    		if (choice=='E')
    		{
    		printf("\nDo you wish to manually buy a ticket(M) or auto purchase a ticket(A)?");
    		printf("\nEnter selection (M/A): ");
    		getchar();
    		scanf_s("%c", &choice);
    		if (choice=='M')
    		{
    		PurchaseCustomTicket(GetESeatNumber());
    		}
    		if(choice=='A')
    		{
    		AutoPurchaseEconomyTicket();
    		}
    		if(choice!='M'||choice!='A')
    		{
    		printf("Invalid Input, returning to main menu.\n");
    		goto gotomenu;
    		}
    		}
    		if(choice=='M')
    		printf("Main menu");
    		goto gotomenu;
    	}//end option P of main menu.
    	if(choice=='V')
    	{DisplaySeating();}
    	if(choice=='Q')
    	{printf("Exiting.");
    	getchar();
    	system ("cls");
    	goto firstmenu;
    	}
    }
    i didn't edit main. Anyway the problem is the labels firstmenu, airlinesmenu and gotomenu is inaccessible to the functions that's above main.... and i can't place those fuctions below main or else main won't be able to accesses those functions...

    and if there's a way you can skip all codings and go to a specific line of of a specific function please tell me about it.

    thanks in advance.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Don't use "void main()" - http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    And you do NOT need goto here, 'goto' should be avoided at all costs. You need some structure in your programming, perhaps draw a flow chart. Use a loop instead (don't call main() either).

    You can move the functions below main() if you provide a prototype above main() for those functions. ie,

    Code:
    void foo(void);
    
    int main(void)
    {
       foo();
       return 0;
    }
    
    void foo(void)
    {
       /* do something */
    }
    Last edited by zacs7; 11-13-2008 at 11:40 PM.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    3
    oh O_O

    thanks for "int main"

    i didn't know you could do that. :S
    but that method you suggested results in the program being denied acesses to start.


    and the reason why i'm using goto is so that i can skip all the codings below the goto and startover from square 1. without losing the data in my arrays. know any way of doing that?
    Last edited by cheongzewei; 11-14-2008 at 12:44 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cheongzewei View Post
    and the reason why i'm using goto is so that i can skip all the codings below the goto and startover from square 1. without losing the data in my arrays. know any way of doing that?
    There are MANY different ways to solve that sort of problem - one of which is goto, and the most ugly&bad version. I have 22 years experience of programming for a salary, and another 4 years of programming as a hobby/school work. The number of times I've used goto "at work" is probably around 100 - so about 5 times a year. A guestimate is that I've written about 50000 lines of code a year - perhaps more than that. So another way of putting it would be that I've used goto about one in every 10000 lines of code. Bear in mind that a fair amount of my work is low-level drivers, OS kernels and such things, so goto is slightly more "allowed" in this type of code, so my use of goto should be considered high.

    Your code contains about 7 gotos (I just scanned through it) - so that's about a year and a half's worth of "high" usage. In about 200 lines of code...

    In general, you solve the type of problem you describe by a combination of if-statments, switch statements and dedicated functions. You may want to make each menu a function, so that you could do something like this:
    Code:
    int MainMenu()
    {
       int quit = 0;
       while (!quit)
       {
           switch(choice)
           {
              case 'A': 
                  menuA();
                  break;
              case 'B':
                  menuB();
                  break;
              case 'Q':
                  quit = 1;
                  break;
           }
        }
    }
    MenuA and MenuB follows a similar pattern.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    3
    :S
    i'm new to programming XD

    well thanks for the heads up.

    (now why didn't i think of that! T_T)

    i should be able to code my program now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. Blur Function for Pictures Using Arrays
    By robbins in forum C++ Programming
    Replies: 1
    Last Post: 03-26-2008, 07:27 PM
  3. Motion Blur Questions
    By Sentral in forum Game Programming
    Replies: 5
    Last Post: 02-25-2006, 12:58 PM
  4. Window Drag / Blur
    By ventolin in forum Windows Programming
    Replies: 6
    Last Post: 07-26-2004, 07:27 AM
  5. writing a gaussian blur filter
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 06-04-2002, 08:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21