Thread: Newbie need help in repeating a program after user input

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    3

    Newbie need help in repeating a program after user input

    Hi,i am currently having problem with the following code. I want the program to ask the user whether he wants to repeat himself at the end of the program.This is what i come up with and i am not sure why it doesn't work.Hope u all can help me.THANK!
    Code:
    #include <stdio.h>
    
    int main(void){
    int age;
    char answer;
    
    do{
        printf("Please enter a number between 1 and 100.\n");
        scanf("%d", &age);
    if (age>= 1 && age<= 20 )
        printf("you are very young\n");
    else if (age> 20 && age<= 50 )
        printf("you are old\n");
    else {
        printf("u are very old\n");
    }
        printf("Do u want to continue?\n");
        scanf("%c",&answer);
        printf("\n");
    
    }while (answer ='Y');
    
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Do you know the difference between = and ==?


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

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    3
    Quote Originally Posted by matsp View Post
    Do you know the difference between = and ==?


    --
    Mats
    Sorry, am not really that that sure when should use = and ==, i only know i have to use = when declaring variable?

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Then you probably didn't write
    if (age>= 1 && age<= 20 )
    did you?

    Have a look at the tutorials on this website, or the list of books at the top of the C board.

  5. #5
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72
    I am fairly new to programming myself, but I noticed something about your code that I thought I would comment on.

    What if the user answers with yes instead of 'Y'? Maybe you should clarify that they should input 'Y' or 'N'.
    "The art of living is more like wrestling than dancing." - Marcus Aurelius

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Whilst nitpicking:
    Code:
        printf("Please enter a number between 1 and 100.\n");
    Would it not be better to ask for "enter your age", rather than "enter a number ..." - if the user knows what the number is used for, it will help them input something that is meaningfull. If I get a prompt to enter a number 1-100, I may well enter 42 - but that's not my age [it's close, and the output would actually be the same - but still].

    And to explain:
    = assigns a value. E.g. a = 7;
    == compares a value. E.g. a == 1 is true if a has the value 1.
    Your while-condition sets answer to 'Y'. And because the way that C works, it then says "is the value we set answer to non-zero" - yes, then continue the loop. So I expect that your program loops forever, whatever the user inputs.

    Your main should also end with "return 0;" to indicate that you successfully finished.

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

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    3
    Quote Originally Posted by matsp View Post
    Whilst nitpicking:
    Code:
        printf("Please enter a number between 1 and 100.\n");
    Would it not be better to ask for "enter your age", rather than "enter a number ..." - if the user knows what the number is used for, it will help them input something that is meaningfull. If I get a prompt to enter a number 1-100, I may well enter 42 - but that's not my age [it's close, and the output would actually be the same - but still].

    And to explain:
    = assigns a value. E.g. a = 7;
    == compares a value. E.g. a == 1 is true if a has the value 1.
    Your while-condition sets answer to 'Y'. And because the way that C works, it then says "is the value we set answer to non-zero" - yes, then continue the loop. So I expect that your program loops forever, whatever the user inputs.

    Your main should also end with "return 0;" to indicate that you successfully finished.

    --
    Mats
    Thanks everybody for the help!
    I dun really understand what u mean by this
    " And because the way that C works, it then says "is the value we set answer to non-zero" - yes, then continue the loop."
    Do u mean that the C will only not loop if the answer is zero?
    I knew there is something wrong with my while function but i still dun really understand why doesn't compare correctly to my input.
    Thanks!

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by dragee View Post
    Thanks everybody for the help!
    I dun really understand what u mean by this
    " And because the way that C works, it then says "is the value we set answer to non-zero" - yes, then continue the loop."
    Do u mean that the C will only not loop if the answer is zero?
    I knew there is something wrong with my while function but i still dun really understand why doesn't compare correctly to my input.
    Thanks!
    In C, any condition (as in, if, while and similar), the determination of whether it is true or false is "if it's not zero, it's true".

    So if you do "if (x = y)" then we are assiging x with the value of y, then checking if the "result" is zero - so if y is zero, then x is zero and the "result" is zero - that is, false. If y is not zero, then the whole thing is true.

    Same with your "answer = 'Y'" - you are setting the variable answer to 'Y'. Since 'Y' is not the value zero, so the loop continues. (In ASCII/ANSI, 'Y' is 89 - there are other values that may generate the letter Y on some other computers, I'm 99.999% sure that there is not a single one where the letter 'Y' is zero - because zero is very very often used as a special character, such as marking the end of strings and such).

    --
    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. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. How to limit user input to a certain number of digits?
    By NewbGuy in forum C Programming
    Replies: 7
    Last Post: 05-08-2009, 09:57 PM
  3. Newbie Help: Currency Converter
    By Ashfury in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 01:21 PM
  4. Inheritance and program structure planning please help a newbie
    By ninjacookies in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2005, 12:18 PM
  5. Program doesn't always do whats expected... any ideas? (newbie)
    By photoresistor in forum C++ Programming
    Replies: 4
    Last Post: 12-07-2002, 02:49 AM