Thread: f1 =P

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    6

    f1 =P

    *hmm..I can't seem to make this code to work,it always bypasses reading the "operation" and doesn't read it.I would apperciate it ,if you could help me out..thnx in advance =)


    **program basically reads 2 float numbers from the user and a character '*', '-' , '/', '+' and does one of these operations and prints out the result..and also goes into a loop~

    ***operations is a subprogram..I think that covers it all..I know 1st post shouldn't be a question..sorry & thnx ..


    Sample example ( user input in bold )
    Enter first number 3.4
    Enter second number 2.7
    Enter operation +
    The result of 3.4 + 2.7 = 6.1
    Another operation ? ( Y/N ) Y
    Enter first number 1.2
    Enter second number 4.0
    Enter operation /
    The result of 1.2 / 4.0 = 0.3
    Another operation ? ( Y/N) N

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void math (float a, float b,char f)
    {
    float result;
    if ( f=='+')
    result = a+b;
    else if (f=='-')
    result = a-b;
    else if (f=='*')
    result = a*b;
    else if (f=='/')
    result = a/b;
    esle
    print("wrong input");
    
    printf("result = %f",result);
    }
    
    void main()
    {
    float x,y;
    char operation,answer;
    
    do{
    
    printf("enter 1st number\n");
    scanf("%f",&x);
    
    printf("enter 2nd number\n");
    scanf("%f",&y);
    
    printf("enter operation \n");
    scanf("%c",&operation);
    
    math(x,y,operation);
    
    printf("Do it again?\n");
    scanf("%c",&answer);
    
    }while((answer=='y')||(answer=='Y'));
    getch();
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Add a space before "%c" to make it " %c", and it will skip the newline left behind by the previous scanf() operation.

    --
    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
    May 2009
    Posts
    6

    ....

    lol..tht was the issue -.-...kinda annoying..well, thank you, but I wanna learn..so could u exlain a lil more in details..about why to leave space..i would be greatly thankful
    thanks anyways =)

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    scanf (and it's relatives) will read as much data as it can according to the format. So %f will read a floating point number, including sign, numbers, one decimal point and exponent. Once it can not read any more "number stuff" it leaves whatever is left in the input buffer. In the normal case, that maens that you have a newline left behind after the number has been read (because you hit enter to "enter" the number, and enter "is" newline ('\n')).

    When you add a space to the format string, the space means "read any number of whitespaces" - and newline counts as whitespace.

    For most types of input, whitespace is automatically "eaten" by the magical scanf whitespace muncher, but not for %c or %s.

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

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    6
    ok ,that helped alot ..now I understand a lil more about C.
    Thanks,mr.Mats =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prime Numbers, Modulus Operator Help
    By dcwang3 in forum C Programming
    Replies: 34
    Last Post: 02-07-2008, 03:16 PM
  2. MD5 hashing problems =(
    By Uncle Rico in forum C Programming
    Replies: 4
    Last Post: 02-28-2005, 10:31 AM
  3. MD5 Algorithm
    By Inquirer in forum C Programming
    Replies: 2
    Last Post: 12-28-2003, 11:55 PM
  4. F1 Key input
    By Kebab Monster in forum C Programming
    Replies: 4
    Last Post: 03-20-2002, 04:56 PM
  5. F1 track analyser
    By Unregistered in forum Game Programming
    Replies: 1
    Last Post: 01-22-2002, 09:48 AM

Tags for this Thread