# 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. ## 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
// 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(){
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");
getchar();
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("|                 |\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");
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;
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;
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;
getchar();
return;
}```

2. 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. 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. 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. 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. &#37;2.3f is a float anyway -_-

7. Originally Posted by master5001
%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. 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
1: y
2: n
Input: 2
Cya!```

9. Originally Posted by master5001
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. 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. Originally Posted by master5001
^ Which is why I requested this earlier. To no avail.
lol, I hear ya!

12. 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. Just to start you off:
Code:
```#include<stdio.h>
#include<math.h>

//Functions
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
{

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");
getchar();
case 1:
system("cls");
main();
break;
case 2:
printf("Cya!");
break;
default:
break;
}
getchar();
return 0;
}```

14. 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

15. 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?

Originally Posted by vart
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 Last