problem w/ doubles in friend's program

This is a discussion on problem w/ doubles in friend's program within the C Programming forums, part of the General Programming Boards category; Hi, my friend recently tried writing a program to do his physics homework calculations(instead of using...yknow a calculator), but there's ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    62

    problem w/ doubles in friend's program

    Hi, my friend recently tried writing a program to do his physics homework calculations(instead of using...yknow a calculator), but there's this weird problem it's been having.

    Initially I thought it was because of how he wrote his scanf's, so I just sorta had some fun and rewrote it since I haven't coded in a while.

    He wrote it with the variables for the calculations as doubles, so all the decimal places would be shown, but it doesn't show any of the digits to the right of the decimal point(it just puts 0s there) and some of the functions won't even run. I rewrote it w/ the variables set to int and it will run every function fine, but we don't get all the decimals.

    Why won't this program run with the doubles!?!?

    Below is the code...it's a lot of stuff because I wanted to try and recall some of my old skills so I did unnecessary things, probably wasting lines and time >.<

    this was compiled and written in Dev-C++ 5.0 w/ the MingW Compiler packaged w/ it.
    Code:
    #include<stdio.h>
    #include<math.h>
    //Functions
    void printMenu(),speedCacl(),timeCacl(),distanceCacl(),aveVeloCacl();
    // Variables.
    int speed,time,distance,velo,x1,x2,x3,t1,t2,t3; // intended to be DOUBLE
    int input,answer; // int so they work w/ switch statement below
    
    main(){
           printMenu();
           scanf("&#37;d",&input);
           system("cls");
           switch (input){
                  case 1:
                       speedCacl();
                       getchar();
                       break;
                  case 2:
                       timeCacl();
                       getchar();
                       break;
                  case 3:
                       distanceCacl();
                       getchar();
                       break;
                  case 4:
                       aveVeloCacl();
                       getchar();
                       break;
                  case 5:
                       return 0;
                  default:
                       printf("Sorry, you put in an incorrect value.\n");
                       getchar();
                       break;
           }
           system("cls");
           printf("Do you want to return to the main menu?\n\t1: y\n\t2: n\n Input: ");
           scanf("%d",&answer);
           getchar();
           switch (answer){
                  case 1:
                       system("cls");
                       main();
                       break;
                  case 2:
                       printf("Cya!");
                       break;
                  default:
                       break;
           }  
           getchar();
           return 0;
    }
    
    void printMenu(){ // Could be done w/ one printf, but easier to view >.<
         printf("|-----------------|\n");
         printf("|                 |\n");
         printf("| Physics Program |\n");
         printf("|    Main Menu    |\n");
         printf("|                 |\n");
         printf("|-----------------|\n\n");
         printf("1: Average Speed\n");
         printf("2: Time\n");
         printf("3: Distance\n");
         printf("4: Average Velocity\n");
         printf("5: Quit\n\n");
         printf("Input your selection: ");
         return;
    }     
    void speedCacl(time,distance){
          printf("\nSpeed\n");
          printf("-----\n\n");
          printf("Input the distance in m: ");
          scanf("%d", &distance);
          printf("Input the time in sec: ");
          scanf("%d", &time);
          speed = distance/time;
          printf("Your speed is: %d\n", speed);
          getchar();
          return;
    }
    
    void timeCacl(){
           printf("\nTime\n");
           printf("----\n\n");
           printf("Input the distance in m: ");
           scanf("%d", &distance);
           printf("Input the speed(m/s): ");
           scanf("%d" , &speed);
           time = distance/speed;
           printf("Your time is: %d\n" , time);
           getchar();
           return;
    }
    
    void distanceCacl(){
           printf("\nDistance\n");
           printf("--------\n\n");
           printf("Input the speed(m/s): ");
           scanf("%d" , &speed);
           printf("Input the time in seconds: ");
           scanf("%d", &time);
           distance = speed*time;
           printf("Your distance is: %d\n",distance);
           getchar();
           return;
    }
    void aveVeloCacl(){
           printf("\nAverage Velocity\n");
           printf("----------------\n\n");
           printf("Input the initial(first) time: ");
           scanf("%d",&t1);
           printf("Input the final(second) time: ");
           scanf(" %d",&t2);
           printf("Input distance one: ");
           scanf("%d",&x1);
           printf("Input distance two: ");
           scanf("%d",&x2);
           t3=t2-t1;
           x3=x2-x1;
           velo = x3/t3;
           printf("Your Average Velocity is: %d\n",velo);
           getchar();
           return;
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Heh... You shouldn't be writing code to do stuff to make life easier if writing the code is harder for you than actually just doing the assignment.

  3. #3
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    957
    It's really hard to say without seeing the code that uses doubles. Since you get a decimal point, it sounds like you're outputting with %f instead of %d, which is right. Are you using %lf with scanf()? printf() uses %f for doubles, and scanf() uses %lf (C99 can use %lf for printf(), but it's safer to just use %f). Make sure the format string matches what you're passing printf()/scanf(). That's the best guess I can come up with without seeing code.

    Please, in the future, post the code that's actually causing the problem.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Paste the code that doesn't work. Not have me alter your code that does. Because when I alter it, I will make it work correctly since I know what I am doing. Since the question really is "what am I doing wrong?" and I am no mind reader, code will be necessary.

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Non of your functions will work as expected as you are not properly formating your printf's to support floating point numbers. Also your variables such as

    Code:
    speed = distance/time;
    Has not even been properly defined? For this example, make speed a double. Then your printf format specifier should be something like, dependingon the size of your output...

    Code:
    %2.3f

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    &#37;2.3f is a float anyway -_-

  7. #7
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Quote Originally Posted by master5001 View Post
    %2.3f is a float anyway -_-
    That is actually a specified for the printf. This is used in order for them to display the float how they see fit. With their variables as they stand they are displaying int variables and not even assigining the correct values to them per their math.

    Code:
    speed = distance/time;

    Will never get them what they want.

  8. #8
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    With the correct changes made you should get the output as below when testing the Time function path.

    Code:
    Time
    ----
    
    Input the distance in m: 400
    Input the speed(m/s): 100
    Your time is: 4.000
    Do you want to return to the main menu?
            1: y
            2: n
     Input: 2
    Cya!

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by master5001 View Post
    Paste the code that doesn't work. Not have me alter your code that does. Because when I alter it, I will make it work correctly since I know what I am doing. Since the question really is "what am I doing wrong?" and I am no mind reader, code will be necessary.
    ^ Which is why I requested this earlier. To no avail.

  10. #10
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Though you may want to specify what 4.00 is in the correct units. As this may be confusion for those "not in the know"

  11. #11
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Quote Originally Posted by master5001 View Post
    ^ Which is why I requested this earlier. To no avail.
    lol, I hear ya!

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Even though it wouldn't be too big to just fix the code and repost it as I would do it, its annoying to have to basically rewrite an entirely unintestesting program from scratch. The pyramid printing programs and anagram programs are actually fun to write. But stuff like this--not so much... I get enough applied formula programs at work.

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Just to start you off:
    Code:
    #include<stdio.h>
    #include<math.h>
    
    //Functions
    // Please do not lazily declare functions like this. Please
    // void printMenu(),speedCacl(),timeCacl(),distanceCacl(),aveVeloCacl();
    void printMenu(void);
    void speedCacl(void);
    void timeCacl(void);
    void distanceCalc(void);
    void aveVeloCacl(void);
    
    // Variables.
    double speed,time,distance,velo,x1,x2,x3,t1,t2,t3;
    int input,answer; // int so they work w/ switch statement below
    
    int main(void )
    {
           do
           {
                  printMenu();
    
                  if(scanf("&#37;d",&input) != 1)
                         continue;
    
                  system("cls");
    
                  switch (input){
                         case 1:
                              speedCacl();
                              break;
                         case 2:
                              timeCacl();
                              break;
                         case 3:
                              distanceCacl();
                              break;
                         case 4:
                              aveVeloCacl();
                              break;
                         case 5:
                              return 0;
                         default:
                              printf("Sorry, you put in an incorrect value.\n");
                              break;
                  }
           } while(i < 1 && i > 5);
           getchar();
    
           system("cls");
           printf("Do you want to return to the main menu?\n\t1: y\n\t2: n\n Input: ");
           scanf("%d",&answer);
           getchar();
           switch (answer){
                  case 1:
                       system("cls");
                       main();
                       break;
                  case 2:
                       printf("Cya!");
                       break;
                  default:
                       break;
           }  
           getchar();
           return 0;
    }

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    if(scanf("&#37;d",&input) != 1)
    continue;

    if scanf failed - you need to flush the input stream to get read of the garbage scanf failed to parse
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #15
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    Well I didn't say I enjoyed writing this kind of crap...I really don't care for it, I was just trying to get the feel for it all back; I completely forgot about do-while statements and everything.

    I do apologize for posting that huge hunk of code, I was in a rush so I just copied and pasted the whole thing, sorry...

    I'm wondering, what was wrong with how I declared the functions? Aside from it being considered lazy, is there anything wrong with it?

    Quote Originally Posted by vart View Post
    if(scanf("&#37;d",&input) != 1)
    continue;

    if scanf failed - you need to flush the input stream to get read of the garbage scanf failed to parse
    Um...what? I've really got no knowledge of this anymore, I definitely need to spend time relearning this >.<

    Huge thanks to master5001(and everyone else too) for being so patient with me and giving me a hand.

    MKylman

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  2. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 10:10 AM
  3. I have finished my program, one problem
    By sloopy in forum C Programming
    Replies: 4
    Last Post: 11-29-2005, 01:10 AM
  4. Some Problem With My Program
    By Americano in forum C Programming
    Replies: 5
    Last Post: 10-18-2003, 01:58 AM
  5. Problem with Program not Quitting
    By Unregistered in forum Windows Programming
    Replies: 20
    Last Post: 06-11-2002, 11:06 PM

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