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");
}
}
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 last month.
Improve this question
I'm trying to solve this problem with and without using an array.
Problem:
The weather report of Chefland is Good if the number of sunny days in a week is strictly greater than the number of rainy days.
Sample input:
4
1 0 1 0 1 1 1
0 1 0 0 0 0 1
1 1 1 1 1 1 1
0 0 0 1 0 0 0
Its output:
YES
NO
YES
NO
When I use an array to store all the values, it works. But when i use a single variable it throws a sigsegv error.
Code using array which works:
#include <stdio.h>
#define max 7
int main(void) {
int T,Arr[max];
scanf("%d",&T);
while(T--)
{
int sun=0, rn=0;
for(int i=0;i<max;++i)
{
scanf("%d",&Arr[i]);
Arr[i]==0 ? ++rn : ++sun;
}
sun>rn?printf("Yes\n") : printf("No\n");
}
return 0;
}
Code using variable which causes sigsegv:
#include <stdio.h>
#define max 7
int main(void) {
int T,a;
scanf("%d",&T);
while(T--)
{
int sun=0, rn=0;
for(int i=0;i<max;++i)
{
scanf("%d",a);
a==0 ? ++rn : ++sun;
}
sun>rn?printf("Yes\n") : printf("No\n");
}
return 0;
}
It's my understanding that, as 'a' is an integer variable, every time I perform a scanf it should store the new value and then perform the next operation, that way I don't have to waste more memory by using an array. But it's throwing sigsegv. Not sure why.
Your scanf usage is wrong in the second snippet.
scanf("%d",a);
should rather be
scanf("%d",&a);
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.
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()
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.
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 ^^