Error must be a modifiable lvalue [closed] - c

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",&currency);
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",&currency);
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");
}

Related

Getting Error while calculation of two float numbers [closed]

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 ran into a problem while calculating density of a box.
The output always comes out to be 0.00.
I tried type-casting but nothing worked.
Please help, I will be very grateful.
Here is the code...
#include <stdio.h>
void main()
{
int l, b, h;
float wt, vol;
float den = 0.0;
// Taking Length
printf("Enter Length ");
scanf_s("%d", &l);
//Taking Breadth
printf("\nEnter Breadth ");
scanf_s("%d", &b);
//Takhing Height
printf("\nEnter Heigh ");
scanf_s("%d", &h);
//Calculating Volume of the Box
vol = (l * b * h);
//Printing Volume
printf("\nVolume Of The Box Is %.2f\n\n", vol);
//Now taking mass of the box
printf("Enter Weight ");
scanf_s("%f", &wt);
//Calculating Density
den = wt /(float) vol;
//Printing Density
printf("\nDensity of the Box Is %.2f\n\n", &den);
}
The output of the program is as follows :
Enter Length 10
Enter Breadth 10
Enter Height 10
Volume Of The Box Is 1000.00
Enter Weight 5000
Density of the Box Is 0.00
Press any key to continue. . .
Note
Density = Mass(Weight)/Volume
Thanks :)
At least: wrong format specifier.
// double address
printf("\nDensity of the Box Is %.2f\n\n", &den);
Save time, enable all warnings. A good well enabled compiler will warn about this mismatch.
Recall &v prints the address of the variable v in memory, we use this in scanf (if you're a beginner), so change your code to:
printf("\nDensity of the Box Is %.2f\n\n", den);

Calling printf() after scanf() returns nothing [closed]

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.

Why can't I get the program to answer anything but 0.00? [closed]

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

i get Error while trying to compile and Run, [Linker error] undefined reference to `scand' [closed]

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
So i am making a Program using Dev C++ and here is the Program works:
Allow user to enter a weight in pounds then your program will convert the value into kilograms and grams.
Allow a user to enter a height in centimeters then your program will convert it to meters, feet and inches.
and my code in Dev C++
#include <stdio.h>
#define CENTIMETER 0.01
#define POUNDS 0.453592
int main (void)
{
float Pounds;
float Kilograms;
float Grams;
float Centimeters;
float Meters;
float Feet;
float Inches;
printf("Please Enter your weight in Pounds : ");
scanf("%f",&Pounds);
Kilograms=Pounds*POUNDS;
Grams=Kilograms*1000;
printf(" Weight in kilograms is %.2f", Kilograms);
printf(" Weight in grams is %.2f", Grams);
printf("\n\n\n\n\n\nAnd ");
printf("Please Enter your height in Centimeters: ");
scand("%f",&Centimeters);
Meters=Centimeters*CENTIMETER;
Feet=Meters*3.28084;
Inches=Feet*12;
printf(" Height in Meters is %.2f", Meters);
printf(" Height in Feet is %.2f", Feet);
printf(" Height in Inches is %.2f", Inches);
getch();
return 0;
}
and the problem is i can't save it and it said:
"[Linker error] undefined reference to `scand' " ,
" ld returned 1 exit status"
am new into programming, so i hope someone can help me..
The code contains a typo. In this statement
scand("%f",&Centimeters);
there shall be scanf instead of scand.
scanf("%f",&Centimeters);

C programming-temperature conversion issue [closed]

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 8 years ago.
Improve this question
Question from book:
Write a program that does temperature conversion from Fahrenheit to Celsius, your program should :
prompt the user for which type of conversion they want to do.
prompt the user for the temperature they want to convert.
I am getting incorrect output.I'm not sure where i'm going wrong.I'm new to c language. Any help is greatly appreciated.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
int main()
{int f, c, f_or_c;
printf("Would you like to convert Fahrenheit (1) or Celsius (2)?\n");
scanf("%d", &f_or_c);
if(f_or_c==1)
{
printf("Enter the temperature in Fahrenheit to convert?\n");
scanf("%d", &c);
f = 1.8*c + 32.0;
printf("Celsius of %d is %d degrees.\n");
}
if(f_or_c==2)
{
printf("Enter the temperature in Celsius to convert?\n");
scanf("%d", &f);
c = (f-32)*5/9;
printf("Fahrenheit of %d is %d degrees.\n");
}
return 0;
}
My guess is you just aren't printing the values out, but everything else looks pretty good.
printf("Fahrenheit of %d is %d degrees.\n");
You're not printing any variables.
This might work for you
printf("Fahrenheit of %d is %d degrees.\n", f, c);
You can take a look at general usage of printf here
http://www.cplusplus.com/reference/cstdio/printf/

Resources