integer input

This is a discussion on integer input within the C Programming forums, part of the General Programming Boards category; I am writing a program for an assignment which accepts a int number input for an age then prints out ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    8

    integer input

    I am writing a program for an assignment which accepts a int number input for an age then prints out a relative message using if else statements. My problem is that it also converts an input when a letter is entered, Is there a way to stop this?

  2. #2
    Registered User ivandn's Avatar
    Join Date
    Oct 2001
    Posts
    49
    You can read the input into a char[] and then convert it to a number using atoi. if a non-number is entered then you would display an error message.
    Ivan

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    8
    Thanks for the info but I dont have enough knowledge to apply what you suggested, can you post an example?

  4. #4
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    .
    Last edited by Troll_King; 11-30-2001 at 10:46 AM.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    8

    Question

    Here is my code so where does the new part fit in?

    # include <stdio.h>
    # include <conio.h>
    void main(void)
    {
    int age;
    char answer;

    clrscr();

    do
    {
    {
    printf("Please enter your age for a honest comment!\n");
    scanf("%d", &age);
    fflush(stdin);

    clrscr();

    if (age <= 0)
    printf("\nSorry thats not possible:-(\n");

    else if (age == 1 || age <= 29)
    printf("\nIf you are %d years old then you are a youngster.\n", age);

    else if (age <= 64)
    printf("\nIf you are %d years old then welcome to mid life.\n", age);

    else if (age <= 99)
    printf("\nIf you are %d years old then you should be retired.\n", age);

    else if (age >= 100)
    printf("\nIf you are %d years old or over, are you still alive?\n", age);

    else
    printf("Please enter a number\n");
    }

    printf("\n\nDo you want to exit the program?(y/n)\n");
    scanf("%c", &answer);
    fflush(stdin);
    clrscr();

    }
    while(answer == 'n' || answer == 'N');

    printf("\nThis program was brought to you by ZeD\n");

    scanf("%c");
    fflush(stdin);
    }

  6. #6
    Registered User ivandn's Avatar
    Join Date
    Oct 2001
    Posts
    49
    # include <stdio.h>
    # include <conio.h>
    void main(void)
    {
    char agetext[500];

    int age;
    char answer;

    clrscr();

    do
    {

    {

    while (1)
    {
    printf("Please enter your age for a honest comment!\n");
    scanf("%s", agetext);

    // convert text to number
    age = atoi(agetext);

    // agetext not a number
    if ( age == 0 )
    printf("You did not enter a number");
    else
    break;
    }

    clrscr();

    if (age <= 0)
    printf("\nSorry thats not possible:-(\n");

    else if (age == 1 || age <= 29)
    printf("\nIf you are %d years old then you are a youngster.\n", age);

    else if (age <= 64)
    printf("\nIf you are %d years old then welcome to mid life.\n", age);

    else if (age <= 99)
    printf("\nIf you are %d years old then you should be retired.\n", age);

    else if (age >= 100)
    printf("\nIf you are %d years old or over, are you still alive?\n", age);

    else
    printf("Please enter a number\n");
    }

    printf("\n\nDo you want to exit the program?(y/n)\n");
    scanf("%c", &answer);
    fflush(stdin);
    clrscr();

    }
    while(answer == 'n' || answer == 'N');

    printf("\nThis program was brought to you by ZeD\n");

    scanf("%c");
    fflush(stdin);
    }
    Ivan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf %s or %d integer input space char stop question...
    By transgalactic2 in forum C Programming
    Replies: 5
    Last Post: 04-14-2009, 10:44 AM
  2. Reading Input from file (Integer array)
    By Govalant in forum C Programming
    Replies: 9
    Last Post: 07-23-2007, 06:13 PM
  3. Validate a user input integer?
    By criticalerror in forum C++ Programming
    Replies: 20
    Last Post: 12-07-2003, 07:30 PM
  4. Reading the input file into integer arrays
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 12-06-2002, 06:04 PM
  5. Reading input as an integer
    By n0de in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2002, 06:52 PM

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