Thread: I am new to C and I suck. Help me?

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    Portland, OR
    Posts
    11

    I am new to C and I suck. Help me?

    I am taking C to fullfil a requirement, but I do have some genuine interest in it. I will most likely see it later on in my life, however, so far I am not enjoying it. Things aren't working for me and I guess I am not understand the details that are required to understand C programming. I am going to give you what I wrote for my last assignment. If someone could help me understand why a few of these issues are happening I would really appreciate it. I use Dev C++ as a compiler.

    1) the command prompt will not stop shutting off once I've entered the desired characters and numbers into the line.
    2) I can't get " % " used correctly in an equation. I need it used as a remainder and it reads as a %d or %f command.
    3) my intA values keep getting made up in random integers.
    4) I don't think I am setting up the arithmetic right.

    I am not asking for someone to do the work for me, but I am trying to better understand this. Below is the assignment (how the program should look) and below that is what I turned in, my next assignment is a variation of this so I need to understand this so I can move on. thanks!




    This program will prompt the user for 3 arithmetic expressions and display the results of each. The expressions are:

    * (a * a) + (a / b) + (c % a)
    o a, b, and c are INT values chosen by the user
    o The user must enter the expression with the exact parentheses, spaces, and operators shown
    o The user may enter any valid values for the variables
    o The result is an INT value

    * (a / b) * (b / a)
    o a and b are FLOAT values chosen by the user
    o The user must enter the expression with the exact parentheses, spaces, and operators shown
    o The user may enter any valid values for the variables
    o The resuld should be displayed with 3 decimal points of precision

    * (a - b) % (int)(c * d)
    o a and b are INT values chosen by the user
    o c and d are FLOAT values chosen by the uer
    o The user must enter the expression with the exact parentheses, spaces, and operators shown
    o The user may enter any valid values for the variables
    o The result is a FLOAT value

    Your program must read the operators, parentheses, and values in the expressions, and then display the result. Here is an example of how the program dialog should look:

    Enter '(int * int) + (int / int) + (int % int)': (12 * 5) + (12 / 5) + (1 % 5)
    (12 * 5) + (12 / 5) + (1 % 5) = 63

    Enter '(float / float) * (float / float)': (1.54 / 0.2313) * (-3.5234 / 0.1232)
    (1.540 / 0.231) * (-3.523 / 0.123) = -190.413

    Enter '(int - int) % (float * float)': (10 - 4) % (4.15 * 6.54)
    (10 - 4) % (4.150000 * 6.540000) = 6


    my code:
    Code:
    #include <stdio.h>
    
    int main()   
    {
        int    varA, varB, varC, int1, varD, varE, equals;
        float  floatA, floatB, floatC, floatD, floatE, floatF, f1sum, fsum;
        
        
        printf("Enter '(intA * intA) + (intA / intB) + (intC '%' intA)': ");
        scanf ("%d%d%d%d%d%d", &varA, &varB, &varA, &varB, &varC, &varA);
        
        equals = (varA * varB) + (varA / varB) + ((varC) % (varA));
        printf("(%d * %d) + (%d / %d) + (%d '%' %d) = %d \n\n", varA, varB, varA, varB, varC, varA, equals);
        
        printf("Enter '(float / float) * (float / float)': ");
        scanf ("%f%f%f%f", &floatA, &floatB, &floatC, &floatD);
        
        f1sum = (floatA / floatB) * (floatC / floatD);
        printf("(%f / %f) * (%f / %f) = %.3f \n\n", floatA, floatB, floatC, floatD, f1sum);
        
        printf("Enter '(int - int) % ((int)(float * float))': ");
        scanf ("%d%d%f%f", &varA, &varB, &floatE, &floatF);
        
        fsum = (varD - varE) % (int1)*(floatE * floatF);
        printf("(%d - %d) % (%d)(%f * %f) = fsum \n", varD, varE, floatE, floatF, int1, fsum);
        
        
        scanf ("%*c");
        return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You aren't actually scanning for parenthesis and such. You're just scanning for a series of numbers.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    My first advice(but this is just a preference) is to drop that Dev-C++ trash and use CodeBlocks which is also free and IMO a much better IDE for C/C++.

    Secondly google and read about scanf flags. This seems to be the main part of your program, reading some values and then computing their results.

    http://homepages.cwi.nl/~aeb/linux/m...3/scanf.3.html

    Thirdly, don't give up! C is not the most readable or easy to learn language but a very powerful one nonetheless. Persistence will get you to become more and more proficient. Don't be afraid of the steep slope to get there. We've all been through it at some point.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    printf("Enter '(intA * intA) + (intA / intB) + (intC '%' intA)': ");
    If you want to print a ' you should escape it: \'
    If you want to print a %, you also must escap that: %%
    Otherwise, it's going to try to translate whatever's after the % into something to scan for.
    Code:
    printf("Enter \'(intA * intA) + (intA / intB) + (intC %% intA)\': ");
    But really, you should just ask them to enter A B and C, and then put them in on your own.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Location
    Portland, OR
    Posts
    11
    Quote Originally Posted by quzah View Post
    Code:
    printf("Enter '(intA * intA) + (intA / intB) + (intC '%' intA)': ");
    If you want to print a ' you should escape it: \'
    If you want to print a %, you also must escap that: %%
    Otherwise, it's going to try to translate whatever's after the % into something to scan for.
    Code:
    printf("Enter \'(intA * intA) + (intA / intB) + (intC %% intA)\': ");
    But really, you should just ask them to enter A B and C, and then put them in on your own.


    Quzah.
    those were my thoughts exactly, but he wants the user to have to enter it in like that. its tedious. but this is good information, that gave me a better insight as to how printf works.

    edit: "wants" not was

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Then you need to make your scanf line scan for that as well. The f in those two functions means "formatted". That means you have to type exactly what it expects you to type. So you have to tell it what to expect. You need a scan line that scans for:

    (
    %d
    space
    *
    space
    %d
    )
    space
    +
    space
    (
    %d
    space
    /
    space
    %d
    ... and so on. You don't need format specifiers to capture each individual character into its own variable, you only need varaibles for the parts that are supposed to be numbers here. But you do need to tell it to scan for the string you want (that's what the "...yourstuffhere..." part of scanf is. It's a scan string.)


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed