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 trying to learn C and I've started by building a simple change calculator. At the moment, I'm just trying to take the user input 8.68 and output it, output the loonies and output the quarters. However, the last 3 printf() calls return nothing:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void){
double value = 0;
int loonies = value;
double remainder = value - loonies;
printf("Please enter the amount to be paid: $");
scanf("%lf", value);
printf("%lf", value);
printf("%d", loonies);
printf("%lf", remainder);
}
This is the output. The prompt asks for the user input and then the program ends:
Please enter the amount to be paid: $8.68
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
You are missing an '&'. Scanf requires you to pass pointers to the variables you want to read into.
scanf("%lf", &value);
The scanf expects the address of the variable that you are passing. Here in your case you have to give &value.
The full code I am pasting here
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void) {
double value = 0;
int loonies = value;
double remainder = value - loonies;
printf("Please enter the amount to be paid: $");
scanf("%lf", &value);
printf("Values- %0.2lf\n", value); //'\n' and 'Values-' added
printf("Loonies - %d\n", loonies); //'\n' and 'Loonies-' added
printf("remainder - %0.2lf\n", remainder); //'\n' and 'remainder-' added
}
Output I am getting is
Please enter the amount to be paid: $8.68
Values- 8.68
Loonies - 0
remainder - 0.00
Edit: Here I am giving %0.2lf for printing only two decimal characters
You are missing a & in your scanf.
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 3 years ago.
Improve this question
The program runs fine but keeps giving me an answer of 0.00. I have floated the numbers and answer and it asks for the first and second number but I cannot see where I have gone wrong.
#include <stdio.h>
#include <math.h>
int main()
{
float sub;
float num1 = 18.73;
float num2 = 20.00;
printf("Please enter the total of the meal: \n");
scanf("%f", &num1);
printf("Please enter the amount of money you have: \n");
scanf("%f", &num2);
sub = num2 - num1;
printf("\nYour change is: %.2f\n", &sub);
return 0;
}
You're printing the address of sub:
Do this:
printf("\nYour change is: %.2f\n", sub);
instead of:
printf("\nYour change is: %.2f\n", &sub);
In your code I have found 2 mistakes
Don't assign greater than 0 numbers to variables when you use scanf
float num1 = 0; //use this
float num2 = 0;
When you output a number don't use & in printf. That is the reason you were given 0.
printf("\nYour change is: %.2f\n", sub);
Finally, you don't need to use #include <math.h> in these king of programmes
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 6 years ago.
Improve this question
What code do you have to include to get C to produce an output from number inputted into the system.
#include <stdio.h>
int main()
{
int z;
printf("Please enter a number");
return 0;
}
I want the programme to work out a mathematical equation to the number that the user enters, for example if I enter 5 I want it to work out the exponent 2 of 1 up to that integer input.
You can use scanf function to get input from user like this:
#include <stdio.h>
int main()
{
int z;
printf("Please enter a number");
scanf("%d", &z);
// do your calculations on z
printf("Result: %d", z);
return 0;
}
You can use the scanf function.
For a number (or in your case an integer specifically) the line is
scanf("%d", &z);
where the %d specifies that its reading in an integer and the &z is a pointer to your integer variable z.
Information here: https://www.tutorialspoint.com/c_standard_library/c_function_scanf.htm
I'm guessing you're new to C programming, so once you get the basics down I would suggest familiarizing yourself with pointers as they are a critical aspect of the language. Some useful resource here using google-fu : https://www.tutorialspoint.com/cprogramming/c_pointers.htm
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 6 years ago.
Improve this question
Tried to write a program in C to say the amount of times you guessed the right number.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, searchNumber, Number, rightGuess;
rightGuess = 0;
printf("Give your number: ");
scanf("%d",&searchNumber);
printf("\n\n Give 10 numbers: ");
for(i=1;i<=9;i++){
scanf("%d \n",&Number);
if(Number == searchNumber){
rightGuess++;
}
}
printf("You guessed the number %d times",&rightGuess);
return 0;
}
However every time I run it, it says I guessed the number 6356736 times. Even though I only entered a number 0 times. Any help?
Maybe you made a mistake in printf().
If there is a variable named var, &var means the memory address where variable var is. Maybe the number 6356736 you saw in your program is the memory address, not the value in variable var.
You will have to change this line in order to print the value of variable rightGuess
printf("You guessed the number %d times", &rightGuess);
To this line.
printf("You guessed the number %d times", rightGuess);
Your call to printf ought to be
printf("You guessed the number %d times", rightGuess);
i.e. don't pass a pointer to rightGuess in correspondence to the %d format specifier. Currently the program behaviour is undefined! (It could well be outputting the address of rightGuess which accounts for the large number - but don't ever rely on that; you need to use %p to output pointer addresses.)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
I'm trying to make a simple currency converter using C, but for some reason it keeps giving me the error "error must be a modifiable lvalue".
I have checked up my code, and I don't see any problems with it. Any help?
#include <stdio.h>
#include <stdlib.h>
void main(){
float jod_usd=1.41, usd_jod=0.71, jod_eur=1.26, eur_jod=0.8, currency;
char ic;
int f;
printf("Enter the number of your conversion option to continue...\n1- Convert from JOD\n2- Convert to JOD\n");
scanf("%d",&f);
printf("Enter the character of your second currency to continue...\nU - - USD\nE - - EUR\n");
scanf("%c",&ic);
printf("Enter your amount:\n");
scanf("%f",¤cy);
if(f==1&&(ic='U'||ic='u')){
printf("%.2f JOD - - %.2f USD\n",currency,(currency*jod_usd));
}
if(f==1&&(ic='E'||ic='e')){
printf("%.2f JOD - - %.2f EUR\n",currency,(currency*jod_eur));
}
if(f==0&&(ic='E'||ic='e')){
printf("%.2f EUR - - %.2f JOD\n",currency,(currency*eur_jod));
}
if(f==0&&(ic='U'||ic='u')){
printf("%.2f USD - - %.2f JOD\n",currency,(currency*usd_jod));
}
system("pause");
}
TL;DR The mistake of using = instead of == caused this.
First, you're changing the value of ic unwantedly.
Second, due to operator precedence,
(ic='U'||ic='u')
is the same as
((ic='U'||ic) = 'u')
where, the result of (ic='U'||ic) is not an lvalue.
Are you sure it is not just a simple bug? You usually don't assign in a condition. If I fix that, then you get some program flow:
Enter the number of your conversion option to continue...
1- Convert from JOD
2- Convert to JOD
1
Enter the character of your second currency to continue...
U - - USD
E - - EUR
Enter your amount:
Code
#include <stdio.h>
#include <stdlib.h>
void main(){
float jod_usd=1.41, usd_jod=0.71, jod_eur=1.26, eur_jod=0.8, currency;
char ic;
int f;
printf("Enter the number of your conversion option to continue...\n1- Convert from JOD\n2- Convert to JOD\n");
scanf("%d",&f);
printf("Enter the character of your second currency to continue...\nU - - USD\nE - - EUR\n");
scanf("%c",&ic);
printf("Enter your amount:\n");
scanf("%f",¤cy);
if(f==1 && (ic=='U' || ic=='u')){
printf("%.2f JOD - - %.2f USD\n",currency,(currency*jod_usd));
}
if(f==1&&(ic=='E'||ic=='e')){
printf("%.2f JOD - - %.2f EUR\n",currency,(currency*jod_eur));
}
if(f==0&&(ic=='E'||ic=='e')){
printf("%.2f EUR - - %.2f JOD\n",currency,(currency*eur_jod));
}
if(f==0&&(ic=='U'||ic=='u')){
printf("%.2f USD - - %.2f JOD\n",currency,(currency*usd_jod));
}
system("pause");
}
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
Why does this code give a completely wrong answer if I use float or double but not int?
//C How to Program Exercises 2.33
#include <stdio.h>
#include <conio.h>
double dailyDrivingCost(double miles, double costPerGallon,double averageMilesPerGallon);
int main(void){
double a,b,c,d,e;
printf ("Please enter the number of miles daily\n");
scanf("%lf",&a);
printf ("Please enter the cost per gallon\n");
scanf("%lf",&b);
printf ("Please enter the average miles per gallon\n");
scanf("%lf",&c);
printf ("The number of miles per gallon is:%d",dailyDrivingCost(a,b,c));
getch();
return 0;
}
double dailyDrivingCost(double miles, double costPerGallon,double averageMilesPerGallon){
double overall_cost;
overall_cost= (miles/averageMilesPerGallon)+ costPerGallon;
return overall_cost;
}
Change
printf ("The number of miles per gallon is:%d",dailyDrivingCost(a,b,c));
^
to
printf ("The number of miles per gallon is:%f",dailyDrivingCost(a,b,c));
d conversion specifier is used to print an int, to print a double (or a float) you need the f conversion specifier.