Thread: another struct project

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221

    another struct project

    what i want to do is a loop that asks for yes or no to continue adding names
    how can i do this i cant seem to get it to work right!
    Code:
    while (( choice = menu()) != 6 ) {
      switch( choice )
      {
       case 1:
            printf("yes\n");
            break;
       case 2:
            break;
       case 3:
            break;
       case 4:
                        // what i would like to do here is a loop that asks
                        // a y to continue adding names or a n to quit..
            printf( "customers first name:\n");
            fgets(seat[i].fname, sizeof seat[i].fname , stdin);
            printf("test = %s\n", seat[i].fname);
            printf( "customers last name:\n");
            fgets( seat[i].lname, sizeof seat[i].lname , stdin );
            printf( " continue ( y n ) ? \n");
            }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    do
        would you like to enter more names yes or not
        choice = get a choice
        switch( choice )
            do stuff here
    while choice not equal quit
    What's wrong with that?

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    i would like to keep the first while loop the same.
    and add a yes or no loop inside the switch case 4
    nested swicth possible?

    Code:
    while (( choice = menu()) != 6 ) {
      switch( choice )
      {
       case 1:
            printf("yes\n");
            break;
       case 2:
            break;
       case 3:
            break;
       case 4:
                        // what i would like to do here is a loop that asks
                        // a y to continue adding names or a n to quit..
            printf( "customers first name:\n");
            fgets(seat[i].fname, sizeof seat[i].fname , stdin);
            printf("test = %s\n", seat[i].fname);
            printf( "customers last name:\n");
            fgets( seat[i].lname, sizeof seat[i].lname , stdin );
            printf( " continue ( y n ) ? \n");
            }
    Code:
    int menu ( void )
    {
     int i;   
     char selection[BUFSIZ];
        
     printf( "%55s","*************************************\n");   
     printf( "%55s","** swiftline airlines  fleet menu. **\n");
     printf( "%55s","*************************************\n");
     printf( "\n" );
     printf( "%50s","to choose , enter the letter\n");
     printf( "\n");
     printf("1) Show number of empty seats\n");
     printf("2) Show list of empty seats\n");
     printf("3) Show alphabetical list of seats\n");
     printf("4) Assign a customer to a seat assignment\n");
     printf("5) Delete a seat assignment\n");
     printf("6) Quit\n");
     printf("\n");
       do {
           printf( "choose your menu\n");
           if( i == 6 )
           break;
           i = atoi( fgets ( selection, sizeof selection , stdin ));
           }while ( i < 1 || i > 5 );
    
     return i;

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Not to be rude, but I think that Quzah asked a very good question. What's wrong with implementing his suggestion? In other words:

    > i would like to keep the first while loop the same.
    Why?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    well whats wrong with that you ask?
    Code:
    int menu ( void )
    {
     int i;   
     char selection[BUFSIZ];
        
     printf( "%55s","*************************************\n");   
     printf( "%55s","** swiftline airlines  fleet menu. **\n");
     printf( "%55s","*************************************\n");
     printf( "\n" );
     printf( "%50s","to choose , enter the letter\n");
     printf( "\n");
     printf("1) Show number of empty seats\n");
     printf("2) Show list of empty seats\n");
     printf("3) Show alphabetical list of seats\n");
     printf("4) Assign a customer to a seat assignment\n");
     printf("5) Delete a seat assignment\n");
     printf("6) Quit\n");
     printf("\n");
       do {
           printf( "choose your menu\n");
           if( i == 6 )
           break;
           i = atoi( fgets ( selection, sizeof selection , stdin ));
           }while ( i < 1 || i > 5 );
    
     return i;
    }
    so inside main i have this:
    Code:
    int main(int argc, char *argv[])
    {
    int choice, i = 0;
    char ch;
      
      while (( choice = menu()) != 6 ) {
      switch( choice )
      {
       case 1:
            printf("yes\n");
            break;
       case 2:
            break;
       case 3:
            break;
       case 4:  
            
            
            printf( "customers first name:\n");
            while ( fgets ( seat[i].fname[0], sizeof seat[i].fname, stdin ) != '\0') {       
            fgets(seat[i].fname, sizeof seat[i].fname , stdin);
            
            printf( "customers last name:\n");
            fgets( seat[i].lname, sizeof seat[i].lname , stdin );
            
            }
    so each case is going to be something different. so i dont need to have a loop like he suggested i dont see how it would work for my situation?
    i wanted a loop inside case 4. is that not a good choice?

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > i wanted a loop inside case 4. is that not a good choice?
    Well not really, but by now I think you've realized that if you don't effectively break up your program into functions, you will be crushed by a wall of code.

    So it is cleaner to make a function like:
    Code:
    funtion getNames 
      do
         would you like to enter more names yes or not?
         if choice == yes
            do stuff
         endif
      while choice != no
      return names(?)
    endfunction
    And then call getNames() or whatchamacallit in your code.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    it would be quite good that if you display the menu using the function and get the choice from the user in the main, which actually dosn't mess up your code. Like say something like as follow

    Code:
    #include<stdio.h>
    
    int DipslayMenu()
    {
        printf("1. Menu 1 \n");
        printf("2. Menu 2 \n");
        printf("3. Menu 3 \n");
        
        printf("\nEnter your choice\n?");
    }
    
    int main()
    {
        int ch;
        
        DisplayMenu();
        scanf("%d",&ch);
        
        while(ch != 4)
        {
            switch(ch)
            {
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
            }
            DisplayMenu();
            scanf("%d",&ch);
        }
    
        getchar();
        return 0;
    }
    ssharish2005

  8. #8
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    thanks citizen i think what im going to do is make a function like you suggested.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  2. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM