Thread: Im new with C i really need help please !!

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    8

    Wink Im new with C i really need help please !!

    im doing this program for school... ive spent the whole day trying to do it and i cant seem to get any close than this please help me!!!!

    Write a program that can serve as a simple calculator. This calculator keeps track of a single number (of type double) that is called result and that starts out as 0.0. Each cycle allows the user to repeatedly add, subtract, multiply, or divide by a second number. The result of one of these operations becomes the new value of result. The calculation ends when the user enters the letter R for “result” (either in uppercase or lowercase). The user is allowed to do another calculation from the beginning as often as he or she wants. Use the scanf for input.

    the program has to look kinda like this...

    Calculator is on.
    result = 0.0
    +5
    result + 5.0 = 5.0
    new result = 5.0
    *2.2
    result * 2.2 = 11.0
    updated result = 11.0
    % 10
    % is an unknown operation
    Reenter, your last line:
    * 0.1
    result * 0.1 = 1.1
    uptdated result = 1.1
    r
    Final result = 1.1
    Again? (y/n)
    yes
    result = 0.0
    +10
    result + 10.0 =10.0
    new result = 10.0
    /2
    result / 2.0 = 5.0
    updated result = 5.0
    r
    Final result = 5.0
    Again? (y/n)
    N
    End of Program


    this is wat i got so far...

    Code:
    #include <stdio.h>
    int main()
    {
    float num1=0.0, num2;
    char oper, a,exit;
    printf("Calculator is on.\n");
    do{
    while(a!= 'r')
    
    {
    scanf("%c", &oper);
    scanf("%f",&num2);
    
    if(oper=='+')
    {
    
    printf("result= %lf\n", num2+num1);
    num1=num2+num1;
    }
    else if(oper=='-')
    {
    printf("result= %lf\n", num2-num1);
    num1=num2-num1;
    scanf("%c",&a);
    }
    scanf("%c",&a);
    }
    printf("final result is %d\n",num1);
    
    printf("would you like to go again?\n");
    fflush(stdin);
    scanf("%c",&exit);
    }while(exit!='n');
    return 0;
    }

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Code:
    // Please format your code. It is unreadable otherwise.
    
    #include <stdio.h>
    
    int main()
    {
        float num1=0.0, num2;       // Specs called for type double, why is this float?
        char oper, a, exit;
        printf("Calculator is on.\n");
        do{ 
            while(a!= 'r')          // _a_ is uninitialized, Universe ends here.
                                    // Specs also said that it can be either 'r' or 'R';
                                    // have a look at toupper() or tolower() function
                                    // and think about how you can use either here.
            {
                scanf("%c", &oper);
                scanf("%f",&num2);
    
                if(oper=='+')
                {
                    printf("result= %lf\n", num2+num1);
                    num1=num2+num1;
                }
                else if (oper=='-')
                {
                    printf("result= %lf\n", num2-num1);
                    num1=num2-num1;
                    scanf("%c",&a);
                }
                scanf("%c",&a);
            }
            printf("final result is %d\n",num1);        // %d is the format specifier for integers
                                                        // floats and doubles have their own -- %f
            printf("would you like to go again?\n");
            fflush(stdin);          // Don't, just don't use fflush() with input streams
                                    // for all intents and purposes, it only work for output.
            scanf("%c",&exit);
    
        } while (exit!='n');
        
        return 0;
    }

  3. #3
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Two more things.
    (1) printf() uses %f for both type floats and type double. It is because type float is promoted to type double in function calls anyways. %lf is, technically, undefined behavior; or so I've been told by people smarter then myself. Found it!

    (2) If you do change num1 and num2 to type double, you'll have to change format specifiers in scanf(), it has to be %lf for type double. I know this conflicts with what you'd expect after reading (1) but &double and &float are (likely) different.
    Last edited by msh; 10-07-2010 at 12:30 AM.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    %lf for double in printf is OK in C99.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    fflush(stdin) is, as noted, bad. So why is it bad and how do we fix it?
    Solution: SourceForge.net: Fflush - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    8
    its cuz it need the #include <stdio.h> in the beggining...
    but there is no changes to this code from the one i posted... they both do the same thing....

    when i try to subtract it just adds the negative number...can someone please help me...
    thank you

Popular pages Recent additions subscribe to a feed