Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm trying to convert a float value to an int in C. I'm using print statements to see what's happening and making sure I'm getting the desired results, but something is not working correctly. Here is my code:
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void){
float changeOwed = -1.00;
while(changeOwed < 0.00){
printf("How much change is owed?\n");
changeOwed = GetFloat();
}
printf("%f\n", changeOwed);
int centsOwed = roundf(changeOwed*100);
printf("%o\n", centsOwed);
If user input is, lets say 0.49, here is the output:
0.490000
61
I don't understand why the cast result is 61. I would expect normal errors to be a result of 0, 48 or 50, but I don't get this weird result and can't figure out the logic of it.
In case you don't get it yet ...
"061" is octal for "49".
Use printf("%d") instead of "%o" if you want to see a decimal "49".
Here is a good list of "printf" format options:
http://www.cplusplus.com/reference/cstdio/printf/
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
I'm new to c and I'm trying to preform maths on a user input, specifically the year of a date.
When I try to divide , or do any maths on the variable 'y' and store it in 'fpy' it will always print the value '6422032' or close to it. Any help would be great, I've been trying for hours now.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
int d,m,f,y,spY;
//char y[3];
printf("Day from date calculator \n");
printf("Please enter the date in DD/MM/YYYY formate. For example , 1/7/1440 \n");
printf(">");
scanf("%d/%d/%i",&d,&m,&y);
int fpy = y/10;
printf("%d",&fpy);
return(0);
}
this
printf("%d",&fpy);
should be
printf("%d",fpy);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
I was trying to multiply a double and an int but it shows nothing.
My code is:
#include <stdio.h>
int main()
{
double vergi = 0.01;
int fiyat = 20;
double sonuc = fiyat * vergi;
printf("%s", sonuc);
}
The %s specifier is for a String. As per the doc says, you should use the %f specifier in printf for a double result.
Wrong conversion type in printf.
You want to print variable which type is double.
This is correct - printf("%lf\n", sonuc);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
The prototype (void example();) I have mentioned for the program is correct, but the compiler is giving garbage value instead of correct values in the output.
What codes should be added or removed?
Here is my code:
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
void example()
{
static int x = 10;
x=x+10;
printf("\n%d",&x);
}
int main()
{
int i;
for(i=0;i<=3;i++)
{
example();
}
getch();
return 0;
}
You are using an adress of a variable where printf wants just the value:
printf("\n%d",&x);
->
printf("\n%d",x);
Your result might also be improved by using "%d\n" instead.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am very new to C and having a problem here. I am attempting to pass a file numbers.in through the below script. numbers.in contains 2 lines as follow:
12,34,56789
123456,789,0123
I am attempting to recognize the comma delineation.
#include <stdio.h>
void main(int argc, char *argv[])
{
int p ,n ,x ; //Converted ints.
while ( fscanf(stdin,"%d,%d,%d\n",&p,&n,&x) == 3 );
{
printf("got the sequence (%d,%d,%d)\n",x,p,n);
}
}
I am running the script like:
./a.out < numbers.in
Currently my script returns completely different numbers! What am I doing wrong here? Is the file sending them as characters so I need to somehow convert to ints? (I tried saving as chars and then later converting chars to ints and also got strange numbers - but different strange numbers!)
SOLVED, bad semicolon usage >_<
while ( fscanf(stdin,"%d,%d,%d\n",&p,&n,&x) == 3 ); <-- remove this semicolon
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am trying to make a program in C to find the approximate square root of a number using the formula NG = 0.5( LG + N/ LG). So far i have:
#include <stdio.h>
#include <math.h>
int main(){
double n;
double LG=1;
double NG;
printf("Enter number");
scanf_s("%lf",&n);
do{
NG=(.5*(LG+n/LG));
LG=NG;
}while((NG*NG-n)<.005);
printf("The root is %lf",NG);
}
This structure works fine in java, but the loop doesn't seem to be executing in C.
Thanks for any advice.
You do not want to loop while NG*NG-n is less than .005. You want to loop while NG*NG is farther from n than desired.
The distance between NG*NG and n is fabs(NG*NG - n).