Need a bit of help please....

This is a discussion on Need a bit of help please.... within the C Programming forums, part of the General Programming Boards category; Man. I got my second assignment for programming and it's really frustrating me to no end. I've tried everything I ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    3

    Need a bit of help please....

    Man. I got my second assignment for programming and it's really frustrating me to no end. I've tried everything I can think of. The assignment should be pretty simple, so I'm kind of embarrassed about posting this, but it's my first time to even look at C coding so I have to start somewhere I guess. Anyway, the assignment is to write a program to take two numbers as input and display their sum, their difference, their product, and their quotient. I am having a problem with decimals and all that though. Here is the code I have so far:

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void) {
      int first, second;
    
      printf("Enter two numbers> ");
      scanf("%d %d", &first, &second);
      printf("The two numbers are: %d and %d\n", first, second); 
      printf("Their sum is %.2f\n", first+second);
      printf("Their difference is %.2f\n", first-second);
      printf("Their product is %.2f\n", first*second);
      printf("Their quotient is %.2f\n", first/second);
      getch();
    }
    I know the code is probably horrible, but it's the best I could come up with and it's still not working. I need the input to be able to accept decimals to the hundredths and also the output to display them in the same format.

    If there is something obvious that I'm doing wrong, could you please let me know? I'm sure it's something that I overlooked, but it's getting extremely frustrating. Thank you for any help.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    The data type int can only hold whole numbers (integers). You need to use float or double to get what you're looking for.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    3
    Ok. I've done some more reading in my book and I think I understand it, but it's still not working. I don't want it done for me by any means...just a point in the right direction. Here is what I have now:


    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void) {
      double first, second;
    
      printf("Enter two numbers> ");
      scanf("%.2f %.2f", &first, &second);
      printf("The two numbers are: %.2f  %.2f\n", first, second); 
      printf("Their sum is %.2f\n", first+second);
      printf("Their difference is %.2f\n", first-second);
      printf("Their product is %.2f\n", first*second);
      printf("Their quotient is %.2f\n", first/second);
      getch();
    }
    Thanks again.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    %lf is the conversion specifier for doubles.
    If you understand what you're doing, you're not learning anything.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by TheflY
    Ok. I've done some more reading in my book and I think I understand it, but it's still not working.
    Don't just say "it doesn't work". Tell us what it:
    a) does, that it should.
    b) doesn't do that it should.
    c) does that it shouldn't.

    Pick one or more of the above when you're explaining your problem. It also helps if you include errors and warnings you get. For example, when I compile your code with warnings turned on, as you should, I get this:
    la.c: In function `main':
    la.c:7: warning: unknown conversion type character `.' in format
    la.c:7: warning: unknown conversion type character `.' in format
    la.c:7: warning: too many arguments for format
    la.c:14: warning: control reaches end of non-void function
    From there, you go find line 7 and see what it's complaining about. This tells us it doesn't like your scanf line. So we fix that with something like:
    Code:
    scanf("%lf %lf", &foo, &bar);
    And of course, we add the needed return statement to our main function. (And we add an "ar" on your getch function, and get rid of your unneeded conio.h line!)

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

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    3
    Thank you so much! You were a ton of help and I really appreciate it. The finished product:



    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void) {
      double first, second;
    
      printf("Enter two numbers separated by a space> ");
      scanf("%lf %lf", &first, &second);
      printf("The two numbers are: %.2f and %.2f\n", first, second); 
      printf("Their sum is %.2f\n", first+second);
      printf("Their difference is %.2f\n", first-second);
      printf("Their product is %.2f\n", first*second);
      printf("Their quotient is %.2f\n", first/second);
      getch();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bit manipulation
    By Neo1 in forum C++ Programming
    Replies: 8
    Last Post: 03-24-2008, 11:53 AM
  2. porting application from 32 bit to 64 bit error
    By gandalf_bar in forum Linux Programming
    Replies: 1
    Last Post: 09-14-2005, 09:20 AM
  3. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  4. Bitwise Operators - Setting and Retreiving a Bit
    By Spono in forum C Programming
    Replies: 2
    Last Post: 11-04-2003, 01:09 PM
  5. Bit Manipulation Questions
    By CPPNewbie in forum C++ Programming
    Replies: 7
    Last Post: 08-12-2003, 02:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21