Errors: expected ';' after expression and expression result unused [closed] - c

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
Why am I getting the errors of "expected ';' after expression, and expressions result unused? Here is my code:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
printf("How tall do you want your pyramid to be?\n");
int height = GetInt();
if (height > 23 && height < 1)
{
printf("Please use a positive number no greater than 23:\n");
}
else (height > 0 && height <= 23)
{
printf("Thanks!\n");
}
}

You are getting this error because you cannot give a condition check with else. You probably want to use else if
But, one thing you will notice is that both the condition is globally exhaustive, so you do not need an else if check with a condition. Just an else without any condition check will do the job for you.
Plus, according to the condition check that you have given, it will never be true. Since, height cannot be greater then 23 and lower then 1 both at the same time. What you need is an or || check instead of an and &&
So, your code would become
#include <cs50.h>
#include <stdio.h>
int main(void)
{
printf("How tall do you want your pyramid to be?\n");
int height = GetInt();
if (height > 23 || height < 1)
{
printf("Please use a positive number no greater than 23:\n");
}
else
{
printf("Thanks!\n");
}
}

There is no such things as
else (height > 0 && height <= 23)
else means everything else, so you can't give else a condition.
Use else if instead ^^

Related

C function still INVALID number [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 6 months ago.
Improve this question
#include <stdio.h>
int main() {
/*function declaration*/
double invalidInput(double);
int loop_obeTask;
double obe_Task[3];
double checkInput ;
for(loop_obeTask = 1 ; loop_obeTask < 4 ; loop_obeTask++)
{
printf("OBE%d : ", loop_obeTask);
scanf("%lf",&obe_Task[loop_obeTask]);
checkInput = invalidInput(obe_Task[loop_obeTask]);
if(checkInput = 1)
{
printf("INVALID INPUT \n" );
break;
}
}
return 0;
}
double invalidInput(double inputGrade)
{
double indicator;
if(inputGrade > 100)
{
indicator = 1;
}
else{
indicator = 0;
}
return indicator;
}
Hello Guys, want to validate the input numbers using the function once the user enters > 100 IT IS INVALID INPUT but in my case, it always shows invalid input both greater and less than 100. I can't post image because my reputation less than 10.
enter image description here
enter image description here
Short answer
You're not checking the value, but assigning it: if(checkInput = 1)
You should use the == operator instead.
Long answer
Even if it does compile, your code presents numerous conceptual mistakes:
You're using double as return value of invalidInput(). Since that function is for validation only and it returns either 1 or 0, you should use int as return type instead.
Your for loop starts at index number 1, which is wrong and will lead to undefined behaviour. C array indexes starts at 0, so if you define an array with size 3, the available indexes are going to be 0, 1, 2, and the loop should look something like for (index = 0; index < MAX; index++) where MAX is 3 in this case.
Your if condition is not checking the equality of checkInput and 1. That's because you're using the assignment operator =, instead of the equality operator ==.
It's common practice to put your function declarations at the top of your file and before the main definition, both for readability and scope reasons.

Getting error : multi-character character constant [-Werror,-Wmultichar] [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 1 year ago.
Improve this question
I was practising some programming and came up with this code. But I am getting this error message : multi-character character constant [-Werror,-Wmultichar] from the 8th line
#include <cs50.h>
#include <stdio.h>
int main (void)
{
int n = get_int("what is your age: ");
if( n <= '25' || n >= '0')
{
printf("you are young\n");
}
else
{
printf("you are old\n");
}
}
-CONCLUSION-
In the 8th line , the single quote on 25 and 0 was not needed because 25 and 0 are int and not char. And || should be replaced with && because n should be in between 0 and 25.
' ' is a literal char value. Because '25' is not a char, it throws an error. Looking at your code, maybe you don't need a literal char value, it looks like you need a literal int value. So, get rid of the single quotes brackets.
Also, n <= 25 or n >= 0 is true for every n. If you want the condition to check if n is in a range of 0 and 25, change || (logical OR) to && (logical AND)
So, in conclusion, this is the right code:
#include <cs50.h>
#include <stdio.h>
int main ()
{
int n = get_int("what is you age: ");
if( n <= 25 && n >= 0)
{
printf("you are young\n");
}
else
{
printf("you are old\n");
}
}

expected identifier (in C) [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 am currently doing CS50 introduction to computer science:
I started this code (written in C) where I have to code a pyramid based on what the user writes.
Here is my code :
#include <stdio.h>
#include <cs50.h>
int main(void);
int n;
do
{
n = get_int("Positive Number: \n");
}
while (n > 0 || n < 9);
Here is the error displayed by my terminal:
mario.c:6:1: error: expected identifier or '('
do
^
mario.c:10:1: error: expected identifier or '('
while (n > 0 || n < 9);
^
2 errors generated.
<builtin>: recipe for target 'mario' failed
make: *** [mario] Error 1
Can someone please help?
William
Look at your main(). You're not writing the definition; you're just declaring the main function prototype. Fix it by adding braces:
int main(void) {
.
.
.
return 0;
}
You placed a semicolon after the declaration of the function main
int main(void);
^^^^
Remove it and enclose the body of main in braces
int main(void)
{
//...
}
Also it seems the condition of the do-while statement
do
{
n = get_int("Positive Number: \n");
}
while (n > 0 || n < 9);
is incorrect. I suspect that you want to repeat the loop if the entered value of n is not positive or is greater than or equal to 9. In this case the condition should look like
do
{
n = get_int("Positive Number: \n");
} while ( !( n > 0 && n < 9 ) );
You have to code on function main()

New to C,Loop Issues [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 5 years ago.
Improve this question
I want to enter value to my array and between 0 and 101 and trying to put zero or 101 will return an error but putting anything from 1-100 will allow me to enter a second number.
but i am stuck when write code to add a second number the loop keeps going in 1 and i cant figure out my error. any help would be apreciated
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
char letter;
int main()
{
int i;
int grades[12] ;
for ( i = 0; i < 12; i++)
do{//loops the following peice of code if values over 100 or less than 0 are entered
printf("Please Enter Grade 1:\n");
scanf("%f", & grades[i]);
if (grades > 100 || grades < 0)
printf("Invalid Entry please enter another between 0 and 100:\n");
}while(grades < 100 || grades > 0);
}
The mistake here is you're comparing the whole array to a value. What you want is one element, plus you need to test that you're outside of the acceptable bounds:
while(grades[i] > 100 || grades[i] < 0);
As Jonathan points out below, the logic in your code tests that it's within, which means only valid answers will loop forever, the opposite of what you wanted.

C: why does my nested if else statement not work [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
I am making a greedy algorithm to calculate number of coins in a value in cents stored in a variable. I had it all working out basically, but I had no way of differentiating between singular and plural for the denominations, ( printing "1 quarters" instead of "1 quarter" for example) and the way how I was doing it with sequential if conditions for each denomination, I couldn't do the same for singular or plural differentiation for each coin since it would give me back two quarters and one quarter instead of either or, so I'm trying to use an if else statement but it says that the else statement basically has no if statement to relate to which I don't understand. My compiler also says that the if statement nested in the other one has "no body" for some reason.
Don't mind the scanf, I will change to a better input method once I get the rest figured out.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main (void)
{
int n;
scanf("%d",&n);
int q = (n/25);
if (n > 24)
{
if (n > 49);
printf("%d quarters\n", q);
else
printf("one quarter\n");
}
return 0;
}
You have bad syntax in your code. if (n > 49); {. This line is wrong, there should not be a semi-colon before your opening bracket. Try this.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void) {
int n;
scanf("%d", & n);
int q = (n / 25);
if (n > 24) {
if (n > 49) {
printf("%d quarters\n", q);
} else {
printf("one quarter\n");
}
}
return 0;
}
Remove the semicolon after:
if (n > 49);
This line:
if (n > 49);
has a semicolon. That semicolon is treated as the body of the true path of the if statement. You must also have gotten a compiler error about the unmatched else as well.
FYI: you will sometimes see an empty expression like this used by people writing loops that have no body.
Search well before asking a question.
See this answer to know about how to code if..else and nested if
if(
condition)
if(condition)
is only allowed nit
if
()

Resources