Thread: help with do-while loop!!

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    7

    Question help with do-while loop!!

    Hi. I have nearly finished a program i am writimg but am having trouble with the last thing i need to put in. All it is, is a do while loop that will loop that part of the program until the correct input has been entered. Which means the user cant enter a letter instead of a number or the wrong number. But for some reason it doesn't want to work. I have various other do while loops in the program and they all work fine. I also use the choice made to return a statement from the switch statement later in the program as you can see.

    The ones that aren't working are the one's for the first two choices that are made. I tried making a do while loop that would repeat until a number from 1 to 10 was entered which worked fine when entering other numbers apart from 1 to 10 but when i enter a letter it just freezes and the program flickers. I also tried changing it so it asked for a letter from a to j instead but when putting a do while loop that repeated until a letter from a to j was input it really didn't like it. Can anyone help? it would be much appreciated. I have put a segment of the prgram in so you can see what i have so far. I have left in my attempt at the do while loop for the first selection bit ( commented as NOT WORKING!!! ).

    Many thanks everyone.

    Code:
      
      
      
      
      do {                       //do loop for whole program if ticket is correct after ticket summary          
              do {             //NOT WORKING!!!
      printf("\n\n ##########  Welcome to the ticket machine  ########## \n\n");             //welcome message         
      printf("\n  Please select a leaving station from the list below\n");  //leaving station choice
      printf("  by entering the corresponding number :\n");
      printf("\n  1 = Ashford \n  2 = Brentworth \n  3 = Canonbury Cross \n  4 = Dowgate \n  5 = Edbury \n  6 = Fenchurch Street \n");
      printf("  7 = Gresham \n  8 = Hampstead \n  9 = Islington \n  10 = Jamaica Road \n");
      scanf("%d", &leaving);     //put leaving station choice in &leaving
      system ("CLS");
              } while (leaving != 1,2,3,4,5,6,7,8,9,10);       //NOT WORKING!!!
             
             
      printf("\n\n ##########  Welcome to the ticket machine  ########## \n\n");      
      printf("\n  Please select an arriving station from the list below\n ");  //arriving station choice
      printf(" by entering the corresponding number :\n");
      printf("\n  1 = Ashford \n  2 = Brentworth \n  3 = Canonbury Cross \n  4 = Dowgate \n  5 = Edbury \n");
      printf("  6 = Fenchurch Street \n  7 = Gresham \n  8 = Hampstead \n  9 = Islington \n  10 = Jamaica Road \n");
      scanf("%d", &arriving);      //put arriving station choice into &arriving
      system ("CLS");
               
     
      
      
      do                      //do while loop for return ticket choice
             {
             system("CLS");
             printf("\n\n ##########  Welcome to the ticket machine  ########## \n\n\n");
             printf("\n Do you require a return ticket?");
             printf("\n\n Press y for a return ticket.\n");
             printf(" Press n if a return ticket is not required:\n");
             scanf("%s", &ret);              //assign return choice to &ret
             }    
             
             
      while (ret != 'y' && ret != 'n');      //keep looping if r or x are not selected
      
      do {                     //start of loop for ticket summary
      system ("CLS");                               //clear screen for ticket summary
      
      //START OF TICKET SUMMARY
      
      printf("\n             #####  TICKET SUMMARY  #####\n\n\n");   //ticket summary message
      
      
       switch (leaving)
              {
                     case 1:
                          printf("\n You have selected Ashford ");
                          break;
                     case 2: 
                          printf("\n You have selected Brentworth ");
                          break;
                     case 3:
                          printf("\n You have selected Canonbury Cross ");
                          break;
                     case 4:
                          printf("\n You have selected Dowgate ");
                          break;
                     case 5:
                          printf("\n You have selected Edbury ");
                          break;
                     case 6:
                          printf("\n You have selected Fenchurch Street ");
                          break;
                     case 7:
                          printf("\n You have selected Gresham ");
                          break;
                     case 8:
                          printf("\n You have selected Hampstead ");
                          break;
                     case 9:
                          printf("\n You have selected Islington ");
                          break;
                     case 10:
                          printf("\n You have selected Jamaica Road ");
                          break;   
              }
              
      printf("as your leaving station.\n\n");

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The "f" in scanf stands for formatted -- if you can't trust the user to type the input in the proper format, then scanf isn't for you.

    You can use fgets to read in the input as a string, and then parse the string to see if it's a number.

    EDIT: I hope you don't really have
    Code:
    (leaving != 1,2,3,4,5,6,7,8,9,10)
    in your program.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
              } while (leaving != 1,2,3,4,5,6,7,8,9,10);     // WRONG
              } while (leaving < 1  || leaving > 10);       //RIGHT
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    scanf("%s", &ret);
    what is the type of ret?
    if this is char - correct format will be %c, and you'll need to take care of \n left by the previous scanf in the input buffer.

    if it is array of chars - & should not be present and following comparison is incorrect
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    Thanks for the replies.

    Could you please explain what is meant by parse. Not sure what that means.

    I've changed the limits in the do-while loop as stated by MK27 (it clicked when i saw it, should have wrote it like that the first time DOH!) but still no joy. When i changed it to 'gets' it went haywire and brought up all kinds of jargon and my program was gone.

    Vart, i know what you mean with the 'ret' needing to be %c but for some reason it didn't work and when i changed it to %s it worked so i left it. Probably not the proper way to do it but its the only way i could get it working.

    Thanks again. Your help is much appreciated. Its just very frustating when you cant get it working, especially when its something quite simple and especially when its something that is working elsewhere in the program under the exact same format

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Parse | Define Parse at Dictionary.com

    It's probably easier to change to chars (since a number is a character, but a character is not necessarily a number) if you're concerned about people typing weird things. But you can't read characters with %s, you need %c.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    I see what you mean now. Maybe chars would be better. If make the selection a to j insted of 1 to 10 i still have trouble getting it to work. Someone told me it could be registering the return key after the entered character aswell meaning it could possibly be a string. Is there a way of not having to press return key afterwards??
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poll event loop
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 04:28 AM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 09:46 PM
  3. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM