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
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 11 months ago.
Improve this question
I'm a beginner and I'm creating a C program to print numbers from 0 to n using while loop where n is input from user.
//program to print numbers 0 to n where n is input from user
#include<stdio.h>
int main()
{
int i=0,num;
printf("Enter number: ");
scanf("%d",&num);
while(i<=num)
{
printf('%d',i);
i++;
}
return 0;
}
Im getting error saying expected const char
I tried to get solution over several websites
since im new to this language I'm facing trouble in such simple code
I tried running this code on several online compilers but everywhere I get the same issue
In line 11 in the printf statement you have used single quotes - '%d' which does is giving you problems here, change it to a "%d". Hope that helps.
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
The task is to calculate how many times a certain digit occurs in the entered sequence of numbers. The number of numbers to be entered and the number to be calculated are set by typing. Ask me if you have got question about code. The problem in finding a match with the number entered in the array.Can you give me hints or instructions, also i think about loop while but i don't know how to realize it please
The code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, b, n, c=0, arr[30];
printf("The count of numbers: ");
scanf("%d", &n);
printf("The number what is finding: ");
scanf("%d", &b);
for (i = 0; i < n; ++i)
{
scanf("%d", &arr[i]);
}
for(i=0;i < n;i++)
{
if(arr[i]=b)
{
c++;
printf("%d", c);
}
}
}
You should be compiling your code with at least some basic compilation flags. If you do, you will get a heads up that something is wrong before having to run it to find out. It saves a lot of time in the long run. Consult your compiler's documentation.
For instance, it would point out that your if condition is using an assignment (=) instead of an equality comparison (==). It should be:
if (arr[i] == b)
Also, you probably want to print out the total count at the end of the program - after the loop is finished. So move the printf("%d\n", c); after the loop. (You were also missing a newline which you probably wanted).
Also, scanf has a return value - you should check it. If the user enters invalid integers, you want to catch that and handle it properly.
Finally, since you declare your array to be of size 30, you should add a check that the desired length of the input array is no longer than that -- otherwise, you would get a buffer overflow.
Side note: please use more descriptive variable names. Not doing so often leads to confusion, especially for beginners. A small exception to this is for loop counters, like i in this case -- its perfectly fine to use a single letter. But consider b -- there is no obvious meaning; it should be something like target or to_find. Also, c could be count or total. As for n, perhaps size or length would be more suited.
if(arr[i]=b) it's wrong. x = y is an assignment but you want to do a check. To check if two elements are equal you should write if(arr[i] == b).
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/
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;