Program that requests input of an integer number

This is a discussion on Program that requests input of an integer number within the C Programming forums, part of the General Programming Boards category; Help, I'm a C noob, and need help writing this program: The program will request input of an integer number. ...

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

    Program that requests input of an integer number

    Help, I'm a C noob, and need help writing this program:

    The program will request input of an integer number. if the entered number matches a specific number(an ID number) it will continue. If the entered number does not match the specific number, it has to request a new number. If a "q" is entered it will quit.

    I have the basics of it down, I'm just wondering if and where to put a while loop in there, and I've come up with something kind of like this:

    #include <stdio.h>

    int main()
    {
    int idnumber;
    printf("Enter your student ID number:");
    scanf("&#37;d", &idnumber);
    if(idnumber==30922419)
    {
    printf("Correct: Please continue.")
    }

    }

    I know it's flawed, but I've been working on it for days. The main problem that i am having is getting the program to request the number, and what to do from there.

    I would appreciate any help.
    Last edited by theejuice; 04-29-2008 at 09:56 PM.

  2. #2
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    If the entered number does not match the specific number, it has to request a new number.
    Assuming it is something like "if the entered password is wrong, request password again";

    remove the if statement and
    Code:
    while(idnumber!=30922419)
    {    
        printf("Wrong! wrong! Enter Again:");
        scanf("&#37;d", &idnumber);
    }

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    4
    ok, i got it to work, all i need now is to know how to make it quit when a "q" is entered.


    Code:
    #include <stdio.h>
    
    int main()
    {
    int idnumber;
    printf("Enter your student ID number:");
    scanf("%d", &idnumber);
    while(idnumber!=30922419)
    {printf("Wrong number. Please enter correct number:");
    scanf("%d", &idnumber);
    }
    }

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    check within while loop...if idnumber == q....then break out of loop.......

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    check within while loop...if idnumber == q....
    It will not work

    You need to change the input - for example use fgets to read all the string typed by the user
    Then check - if the fist char of the string is 'q' - exit
    Otherwise use sscanf or strtol to convert string to number
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Banned
    Join Date
    Nov 2007
    Posts
    678
    since you are supposed to quit when user enters 'q', and you are reading int ids also, i will say, use a char array for input, check for 'q', otherwise convert it to int, and check for id.

    edit: clash of the times! ditto timing! i love cboard!

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manav View Post
    since you are supposed to quit when user enters 'q', and you are reading int ids also, i will say, use a char array for input, check for 'q', otherwise convert it to int, and check for id.

    edit: clash of the times! ditto timing! i love cboard!
    If it's just an ID number, then there's absolutely no need to convert it into an integer, as I expect you'll never actually use it for calculations (ID number - 32 * 5.0 / 9 doesn't really make sense, unless you actually _REALLY_ want to know how many celcius degrees your fahrenheit ID number makes). So a string comparison would be fine.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  3. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 05:16 AM
  4. Program Loops Forver! HELP!
    By glossopjames in forum C Programming
    Replies: 6
    Last Post: 12-16-2004, 07:23 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

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