Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I was suppose to get distance and speed from user and to return the time.
Here's the code I did:
int main()
{
int distance, speed;
scanf("%d,%d", &distance, &speed);
printf("%d\n", distance / speed);
printf("%d hours and %d minutes", (distance/speed), (distance / speed)%60);
}
For the values:
10 10
I receive 0 as output.
The problem here is, you did not check the return value of scanf() to ensure it's success.
By property, the format string supplied with scanf() should exactly match with the input, otherwise, due to matching failure, the receiving arguments won't get the expected value.
With a format string like
scanf("%d,%d", &distance, &speed);
an input
10 10
is not proper, you need to enter like
10,10
to match the , in the format string.
Otherwise, you can remove the , from the format string also, and provide the input in space-delimited format.
[Edit]:
To enforce a floating point division, please chnage your statement to
printf("%f\n", ( (float)distance / speed ) );
printf("%f hours and %d minutes", ( (float)distance / speed ), (distance / speed)%60);
You need to enter 10,10 because that's what you require with your scanf().
Then of course your calculation are incorrect. You'll get 1 and 1 for both hours and minutes.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Greeting.
I am doing the following exercise: Run a program to determine an approximate value of π using the series shown below. The calculation is performed by generating a certain number of terms in the series. The number of terms to be considered is read from the standard input (keyboard) (greater than or equal to 30000).
Note: In the resolution of this issue, you cannot use functions from the math.h library of the C programming language.
example: input a value enter total terms >=30000: entering 30000 should give you the result o pi:3.141559
The prolem I'm having: Uppon entering the same value(30000)I am not getting the corret value o pi=3.14.... but instead it's something like:0.0067...
heres my code:
#include<stdio.h>
#include<math.h>
int main(void){
double numerator, denominator, pi=0.0;
int k;
printf("input a total number o terms >=30000:");
for ( k=1;k<=30000;k++){
scanf("%d",&k);
if(k>=30000){
if(k%2==0){
numerator=1;
}
else {
numerator=-1;
}
}
denominator=2.0*k+1.0;
pi+=numerator/denominator;
pi=4*pi;
printf("value of PI is= %lf",pi);
}
return 0;
}
Can someone point out what I am doing wrong and how can I solve it pls?
Your time and attention are deeply appreciated.
Thank You.
There are many problems with what your implementation of the algorithm:
Try avoiding scanf ad printf inside the for loop.
Instead of getting the k variable from the user try and get the maximum value of k.
denominator=2*k+1 is wrong if you follow the algorithm that you gave in your question and should be changed to denominator=2*k-1.
You repeat pi*=4 every iteration.
I applied all those improvement and i get pi=3.141926 for just 3000 iterations.
Here a little help on how your for loop should look like:
for (int k=1;k<iterations;k++){
numerator*=-1;
denominator=2.0*k-1.0;
pi+=numerator/denominator;
}
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 2 years ago.
Improve this question
I'm learning how to program in C. Can someone help me out to find a problem in my code? I'm trying to write a program that change seconds to minutes and seconds (like 100 seconds equals 1 minute and 40 seconds).
That's the program:
#include <stdio.h>
#define SEC_TO_MIN 60
int main(void)
{
int sec, min, left;
printf("Seconds to minutes and seconds!\n");
printf("Give me an amount of seconds (<=0 is the end):\n");
scanf("%d, &sec");
while (sec > 0)
{
min = sec / SEC_TO_MIN;
left = sec % SEC_TO_MIN;
printf("%d seconds is %d minutes, %d secunds.\n", sec, min, left);
printf("Next number of seconds (<=0 is the end):\n");
scanf("%d", &sec);
}
printf("The End!\n");
return 0;
}
Because code was not compiled with a good compiler with warnings all enabled. Example: "warning: format '%d' expects argument of type 'int*'
so if you replace
scanf("%d, &sec");
to be
scanf("%d", &sec);
it will works successfully
It is not the problem of while but the solution is simple
check out the scanf statement before the while
i.e
scanf("%d",&sec);
reason why that problem came is to read a value through scanf function u use the format specifier %d for signed integer that u r going to allocate a value and that value is stored in the variable sec so while coding always be care full about the inverted commas. it cannot be used where address is specified.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am starting to learn C. Today I was trying a little program that just do a average point starting from 3 input.
After all I wanted to print the number of the averages done in the session, so I insert a simple
counter=counter+1;
into the main while loop and a
printf("you done the average %d times", counter);
before the return 0.
The problem is: if I do the average for just 1 or 2 times, the counter show
every time a different number, never the right, but ever around the int maximum. I tried everything, but it don't work. Where is my mistakes?
This is my first post on this site, i read the rules but i'm sorry if i'm breaking just one. The variable "counter" is declared.
int main()
{
int vote1, vote2, vote3, tot, media, contatore, err;
char opz;
do{
after this, i start an while loop, and this is its end:
contatore=contatore+1;
} while(opz!='n');
printf("hai eseguito la media %d volte", contatore);
return 0;
obviously the code is in italian, where counter=contatore
You have to initialise the variable:
int contatore = 0;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am working on a program that will convert from dollars to cents. I take in user input as a float using a predefined function then i try to multiply this float by 100 but it gives me an incorrect value. Heres the code:
printf("Change in dollars: ");
change= GetFloat();
cents= round(change*100);
printf("Cents is %f",cents);
This is a logical error because the program runs fine but there is something wrong with the mathematics for example if i enter 1.50 when prompted, the return i get is 1004 which is clearly wrong. What i want to happen is 150 to be outputted.
This is most likely because you have some locale where the decimal separator is , instead of ., and since you are very likely not checking the return value of scanf() "which is what most books do and almost every tutorial", then the variable is not being initialized and what you are printing is a consequence of the layout of your program instead of a value inputed by a user.
To verify what I say, I suggest compiling the same program without modification but with different compilation flags.
What you must do is ensure that the input is correct, try this
float GetFloat(int *ok)
{
float value = 0;
if (scanf("%f", &value) != 1)
*ok = 0;
else
*ok = 1;
return value;
}
which you would call like this
int result = 0;
float change = GetFloat(&result);
if (result == 0)
return -1;
float cents = round(change * 100);
printf("Cents is %f",cents);
If the decimal separator is an issue, the above program will not output anything, because scanf() will return 0, and hence ok will be 0 after you call the function.
One important consequence of ignoring the return value of non-void functions is that if the value was not initialized like in this case with scanf(), and you don't know that because you didn't check the return value, then undefined behavior will happen, meaning that your program will contain bugs that are very difficult to find and hence very hard to fix.
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 9 years ago.
Improve this question
Today I tried to make a calculator which converts a distance to meters, inches, etc. I have written my little program in C with Visual Studio 2013 Ultimate.
My problem is, when I enter a number the program is calculating wrong!
PS. It's not homework, it's an exercise from "Let us C", which I'm reading ATM.
I get the following output:
Enter values of the distance between the two cities in km's10 Meter=0
inch=-1064763392 feet=-2147483648 cm=1103731097 Waiting for a
character to be pressed from the keyboard to exit.
/*Just for fun Author: Anru*/
#include <stdio.h>
int main()
{
int km;
float result_met, result_inch, result_feet, result_cm;
/*Display text*/
printf("Enter values of the distance between the two cities in km's");
scanf_s(&km);
/*Formular for a simple km conversion*/
result_met = km * 1000;
result_inch = km * 39370;
result_feet = km * 3280;
result_cm = km * 100000;
/*Result print*/
printf("Meter=%d\ninch=%d\nfeet=%d\ncm=%d",result_met, result_inch, result_feet, result_cm);
printf("\nWaiting for a character to be pressed from the keyboard to exit.\n");
getch();
return 0;
}
Your scanf_s() call is wrong. The first argument is a format string which describes what is being read. Your compiler should probably be issuing a warning with the current code, and if it is not, you need to increase the warning level, as heeding the compiler's warnings will save you headaches for things like this.
An appropriate call might look like:
scanf_s("%d", &km);
Use %f to print float type instead of %d, otherwise program will invoke undefined behavior.
printf("Meter=%f\ninch=%f\nfeet=%f\ncm=%f",result_met, result_inch, result_feet, result_cm);
It is the input reading that is wrong:
scanf_s(&km);
should be
scanf_s("%d",&km);
The value in km could be anything with the original code