Development C Programming...help!

Discussion in 'Software' started by bahgger, 20 Oct 2007.

  1. bahgger

    bahgger Minimodder

    Joined:
    13 Apr 2005
    Posts:
    925
    Likes Received:
    10
    Hey guys, I'm a total noob to C Programming and I'm trying to get my head around why this set of code won't work. I need to find the largest of three user-defined floating point numbers and then print "The largest number is X" where X is that number. Anyone can help? When I compile and type in three numbers, the program just closes and quits.

    #include <stdio.h>

    int main()
    {
    float A;
    float B;
    float C;

    printf("Please enter three floating-point numbers (e.g. 3.12):\n");
    scanf("%d %d %d", &A, &B, &C);

    if (A > B && A > C)
    printf("\nThe largest floating-point number entered is %d", A);
    if (B > A && B > C)
    printf("\nThe largest floating-point number entered is %d", B);
    if (C > A && C > B)
    printf("\nThe largest floating-point number entered is %d", C);
    if (C == A && C == B)
    printf("\nThe numbers entered are equal.");
    scanf("%d");
    return 0;
    }
     
  2. Ramble

    Ramble Ginger Nut

    Joined:
    5 Dec 2005
    Posts:
    5,596
    Likes Received:
    43
    Should be in hardware & software or technical help.
     
  3. liquid_gen

    liquid_gen What's a Dremel?

    Joined:
    12 Feb 2006
    Posts:
    287
    Likes Received:
    0
    The reason it just closes and quits is because its run entirely and then closes, all before the blink of an eye.
    Add
    system("PAUSE");
    before the return statement so that you can see the result.

    Also, your algorithm doesn't look very efficient. I haven't really been bothered to look at it much but for starters you should use "if else" statements, that way if A is the largest it won't bother testing the others.
    If you want to rewrite it more even more efficiently Id suggest using a loop statement.
     
  4. bahgger

    bahgger Minimodder

    Joined:
    13 Apr 2005
    Posts:
    925
    Likes Received:
    10
    Well, I placed the scanf("%d") in there at the end to mimic the system(pause) since our lecturer hasn't moved that far into the course. Also, I just tried it again and the printed result is 0, not the value of X :( What gives?
     
  5. liquid_gen

    liquid_gen What's a Dremel?

    Joined:
    12 Feb 2006
    Posts:
    287
    Likes Received:
    0
    I think that would be due to your place holders: %d is for ints, %f is for floats.
     
  6. bahgger

    bahgger Minimodder

    Joined:
    13 Apr 2005
    Posts:
    925
    Likes Received:
    10
    OMG you're a STAR, I'll try that.. doh!!!
     
  7. bahgger

    bahgger Minimodder

    Joined:
    13 Apr 2005
    Posts:
    925
    Likes Received:
    10
    :) :) You're a superstar - it worked!
     
  8. liquid_gen

    liquid_gen What's a Dremel?

    Joined:
    12 Feb 2006
    Posts:
    287
    Likes Received:
    0
    I'm glad I could help.
    Still, I don't know how far into the language you are, but if you want to impress you could, as i said, do this more efficiently using loops and I'd be more than willing to help.
    Though I'd understand if you haven't done loops yet and don't want to use something you haven't covered in class.
     
    Last edited: 20 Oct 2007
  9. bahgger

    bahgger Minimodder

    Joined:
    13 Apr 2005
    Posts:
    925
    Likes Received:
    10
    The tutorial asks for if/ ifelse statements so I can't use loops, although I'm sure I'll learn them soon enough.. I need to get my head around all these new variables like char and how to initialise it. This is only my second attempt at programming! :X
     
  10. capnPedro

    capnPedro Hacker. Maker. Engineer.

    Joined:
    11 Apr 2007
    Posts:
    4,381
    Likes Received:
    241
    Yes and while he's at it, why doesn't he implement a bubble sort algorithm? :p

    Seriosuly though, just from glancing at your code, it looks like there may be a slight problem if two of the numbers are equal and larger than the third. Tried it?
     
  11. Bogomip

    Bogomip ... Yo Momma

    Joined:
    15 Jun 2002
    Posts:
    5,161
    Likes Received:
    39
    Yeah, thats nothing a few >= wouldnt solve though, as he is looking for the highest number not the biggest individual variable..
     
  12. bahgger

    bahgger Minimodder

    Joined:
    13 Apr 2005
    Posts:
    925
    Likes Received:
    10
    Yeah, I did think about that problem with the variable equalling another..!

    I'm now trying to sort out this next problem where I've got to receive an input from the user stating which arithmetic operation to complete (x, /, + or -), then two floating point numbers which will then have that operation completed on them. Here is my code:

    #include <stdio.h>

    int main()
    {
    float A, B;
    char op = '\0';

    printf("Please enter the operator (+, -, * or /):\n");
    scanf("%c", &op);
    printf("Please enter two numbers:\n");
    scanf("%f%f", &A,&B);

    if (op = '+')
    printf("Answer: %f %c %f = %f", A, op, B, A+B);

    if (op = '-')
    printf("Answer: %f %c %f = %f", A, op, B, A-B);

    if (op = '/')
    printf("Answer: %f %c %f = %f", A, op, B, A/B);

    if (op = '*')
    printf("Answer: %f %c %f = %f", A, op, B, A*B);

    scanf("%f");
    return 0;
    }

    I have scrounged around my notes and there isn't any bit about declaring char variables. My if statements seem useless in the program because all printf statements are run, which leads me to believe that I did not initialise "char op" correctly..
     
  13. glaeken

    glaeken Freeeeeeeze! I'm a cawp!

    Joined:
    1 Jan 2005
    Posts:
    2,041
    Likes Received:
    50
    First you need to compare in your if statements with '==' not '=' (eg if(op == '+') ).

    Second you should be using if-else statements or a switch statement (but I'm guessing you haven't learned about those yet).

    Code:
    if(op == '+')
        printf()
    else if(op == '-')
        printf()
    ...
    You also don't need to assign to a char when you declare it. You can just do: char c;


    Also, once you feel comfortable with input/output in C, you should write your own input functions as scanf is susceptible to buffer overflow attacks.
     
  14. Bbq.of.DooM

    Bbq.of.DooM Custom User Title

    Joined:
    12 Feb 2005
    Posts:
    1,477
    Likes Received:
    1
    I'd go into great detail about why system('PAUSE') is a bad idea, but it works just fine for what you're doing.

    Congrats.
     
  15. bahgger

    bahgger Minimodder

    Joined:
    13 Apr 2005
    Posts:
    925
    Likes Received:
    10
    Thanks a lot for that :) Sometimes I get really annoyed at my syntax errors and hopefully more exposure to the stuff will make me improve.
     
  16. liquid_gen

    liquid_gen What's a Dremel?

    Joined:
    12 Feb 2006
    Posts:
    287
    Likes Received:
    0
    Don't worry it will. Id say it happens to all of us, I know it did to me, but over time those kinds of errors will become less and less common.
     

Share This Page