User not logged in - login - register
Home Calendar Books School Tool Photo Gallery Message Boards Users Statistics Advertise Site Info
go to bottom | |
 Message Boards » » C programming Page [1]  
shmorri2
All American
10003 Posts
user info
edit post

I've confused myself and forgot how to do this.

I want to take a floating input, multiply it by 100, and then change it to an integer without rounding.

ie:  650.127  = 65012
23.3 = 2330
45.01 = 4501


This is what I have so far and I know this is wrong, but this is the general idea... If I can get the input correctly, the rest of the program will work.
--------------------------------------------------------------------------------------------------

#include < stdio.h >

void main()
{
double input;
scanf("%e", &input);

printf("%e\n", input);
int g=(input*100);
printf("new input is %d\n", g);
if(input<=0)
printf("No input");
}


--------------------------------------------------------------------------------------------------
The printf are just to check the input to make sure I'm actually getting what I desire (which I am not) They will be removed in the final product.

When I execute and input 650.23, My output is:
--------------------------------------------------------------------------------------------------
1.057003e-307
new input is 0


however, if I change the code such that


#include < stdio.h >

void main()
{
double input;
input = 650.23;

printf("%e\n", input);
int g=(input*100);
printf("new input is %d\n", g);
if(input<=0)
printf("No input");
}


--------------------------------------------------------------------------------------------------

This works and my output is as it should be.

what am I doing wrong with scanf?

[Edited on March 16, 2011 at 12:17 PM. Reason : .a]

[Edited on March 16, 2011 at 12:17 PM. Reason : .a]

3/16/2011 11:49:59 AM

shmorri2
All American
10003 Posts
user info
edit post

[EDIT]


I changed the following because I realize I was using exponential form and I meant not to.

scanf("%f", &input);
printf("%f\n", input);

3/16/2011 12:20:41 PM

shmorri2
All American
10003 Posts
user info
edit post

Figured it out (after scratching my head for about an hour on this ) nm

[/thread].

[Edited on March 16, 2011 at 12:28 PM. Reason : .]

3/16/2011 12:28:02 PM

qntmfred
retired
40435 Posts
user info
edit post

so you gonna share your solution then?

3/16/2011 1:29:53 PM

dakota_man
All American
26584 Posts
user info
edit post

I think scanf needs %lf for the double. The following seems to work:

#include <stdio.h>

int main()
{
double input;
scanf("%lf", &input);

printf("%f\n", input);
int g=(input*100);
printf("new input is %d\n", g);
if(input<=0)
printf("No input");

return 0;
}

3/16/2011 1:38:29 PM

shmorri2
All American
10003 Posts
user info
edit post

^^ dakota_man posted it.

Works perfectly. After writing a few functions, tons of if/else statements, I brain farted over simple syntax

[Edited on March 16, 2011 at 2:19 PM. Reason : .]

3/16/2011 2:18:46 PM

lewisje
All American
9196 Posts
user info
edit post

well at least you didn't post this on StackOverflow

3/16/2011 2:26:40 PM

Chance
Suspended
4725 Posts
user info
edit post

Any particular reason you can't explicitly type cast (and then output if thats ultimately what you are trying to do)?

3/16/2011 5:33:14 PM

shmorri2
All American
10003 Posts
user info
edit post

because I'm a n00b, learning C, and this is for a project for my ece 220 class.

3/16/2011 7:53:17 PM

FykalJpn
All American
17209 Posts
user info
edit post

they teach C in 220 now?

3/16/2011 8:30:26 PM

lewisje
All American
9196 Posts
user info
edit post

my first thout was to cast also, something like (int)(100*input)

3/16/2011 8:30:38 PM

EuroTitToss
All American
4790 Posts
user info
edit post

they teach C in 220 now?

3/16/2011 8:48:42 PM

Specter
All American
6575 Posts
user info
edit post

they teach C in 220 now?

3/17/2011 12:31:21 AM

shmorri2
All American
10003 Posts
user info
edit post

I meant 209. My bad

3/17/2011 9:50:52 AM

 Message Boards » Tech Talk » C programming Page [1]  
go to top | |
Admin Options : move topic | lock topic

© 2024 by The Wolf Web - All Rights Reserved.
The material located at this site is not endorsed, sponsored or provided by or on behalf of North Carolina State University.
Powered by CrazyWeb v2.38 - our disclaimer.