simple ticketing machine program

This is a discussion on simple ticketing machine program within the C Programming forums, part of the General Programming Boards category; I'm trying to a simple ticketing machine program. I prompt for user input in integer form because my assignment forced ...

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

    Post simple ticketing machine program

    I'm trying to a simple ticketing machine program. I prompt for user input in integer form because my assignment forced to be so. So i write as follow:

    Code:
    #include <stdio.h>
    
    int main (void)
    
    int departure, destination;
    
    printf("Please enter your destination [0-10]: ");
    scanf("%d",destination);
    
    while (destination<0 || destination>10)
    {
    printf("Error! Please enter appropriate destination.");
    printf("Please enter your destination [0-10]: ");
    scanf("%d",destination);
    }
    Yet, problem occured when i testing the program.
    If the user enter character as input, then my program will keep on non-stop poping out my "loop part".
    It just would be fine when key in integer but it will error when users entering character. So, how m i going to prevent it non-stop poping out error when users entering character ???
    Last edited by ryanmk91; 03-23-2010 at 12:43 PM. Reason: Asking for help

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to check the return value of scanf to make sure it read a number. If it didn't, you need to clear out the input stream. Read the FAQ, it covers it there.


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

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    printf("Please enter your destination [0-10]: ");
    scanf("%d",destination);
    
    
    //should have an ampersand in the scanf() line:
    
    scanf("%d", &destination);
    
    //now add this line to remove the newline from
    // the keyboard buffer:
    getchar();

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    18
    You just need an ampersand before the variable name in the scanf. When you're scanning integers, or doubles or floats for that matter, you need an & before the variable name in the scanf.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    2
    Quote Originally Posted by Adak View Post
    Code:
    printf("Please enter your destination [0-10]: ");
    scanf("%d",destination);
    
    
    //should have an ampersand in the scanf() line:
    
    scanf("%d", &destination);
    
    //now add this line to remove the newline from
    // the keyboard buffer:
    getchar();



    I'm sorry that the "&" i have forgotten to put it on the post i published yesterday. In fact i do put "&" symbol in scanf and i just missed it when published.

    Code:
    #include <stdio.h>
    
    int main (void)
    
    int departure, destination;
    
    printf("Please enter your destination [0-10]: ");
    scanf("%d",&destination);
    
    while (destination<0 || destination>10)
    {
    printf("Error! Please enter appropriate destination.");
    printf("Please enter your destination [0-10]: ");
    scanf("%d",&destination);
    }

    I hope to find the solution for the problem:

    The scanf i set is to read for integer input. So, when user keys in alphabet like A , N, U.... this action will cause my program occured error when running the program. How can i make my scanf can read both char (alphabet) and int (integer) whilst at the same time i just want to scanf for integer input but appear error message to users when get alphabet input ???


    **I'm a beginner in C language. And too pro C language i still couldnt cope with yet.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I already told you how.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  2. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 03:42 PM
  3. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  4. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  5. I need help on a formula for this simple program.
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 01-28-2002, 10:01 PM

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