expected identifier (in C) [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 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()

Related

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

What is the reason why isn't this code running properly 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 5 years ago.
Improve this question
I am trying to learn C but my code is not running properly.It always gives fatal error.I think there is a problem in for loop.How can i fix it?
#include<stdio.h>
int main( void )
{
int a ;
int b = 1 ;
int i = 0 ;
printf("Enter a number:");
scanf("%d",&a);
if(a=0)
printf("Factorial=1");
else if (a > 0){
for(i=1 ; i<=a ;i++){
b = 1;
b *= i;
}
printf("Factorial=%d",b);
}
else
printf("FATAL ERROR");
return 0;
}
if(a==0) Not assignment use comparison.
You wanted to use the comparison but you ended up using assignment.
if(a=0) is same as if(0) so else part is executed.1
But that part also looks for a>0 which is not the case.
So it prints FATAL ERROR.
1. This happens because the result of an assignment expression is the value of the expression
What do I need to do to calculate factorial?
fact(0)=1
fact(1)=1
fact(n)=n*fact(n-1);
so you will do something like
for(int i=1;i<=a;i++)
b*=i;
You don't need that b=1 part because it is making everything 1. So your calculated value is not retained.
So the complete corrected code will be
#include<stdio.h>
int main()
{
int a;
int b = 1 ;
int i = 0 ;
printf("Enter a number:");
scanf("%d",&a);
if(a=0)
printf("Factorial=1");
else if (a > 0){
for(i=1 ; i<=a ;i++){
b *= i; // don't overwrite value of b with 1
}
printf("Factorial=%d",b);
}
else
printf("FATAL ERROR");
return 0;
}
There are two major issues.
First being your use of if(a=0) which results in assignment in place of comparison which is achieved by if(a==0).
Secondly
for(i=1 ; i<=a ;i++)
{
b = 1;
b *= i;
}
This piece of code is faulty too. You are making b=1 after every iteration which overwrites the intermediate results Hence resulting in incorrect answer. As you have already initialized b to 1 in your code, you do not need this line at all.
for(i=1 ; i<=a ;i++)
{
b *= i;
}
Also it is worth mentioning that the reason you are always getting fatal error is because of what you are doing in the if condition. Due to if(a=0), variable a is assigned value 0 . Hence not satisfying both if and else if conditions and resulting in the execution of else block every time.

Errors: expected ';' after expression and expression result unused [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 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 ^^

Warning: Undefined reference to function [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 8 years ago.
Improve this question
I keep getting the warning that my function has an undefined reference and that doesn't really say much to me or how to fix it. Here are the errors
log_2.c: In function ‘main’: log_2.c:29: warning: implicit
declaration of function ‘logbase2’ /tmp/ccAXAmVb.o: In function
'main': log_2.c:(.text+0x5e): undefined reference to `logbase2'
collect2: ld returned 1 exit status
Heres my code:
int logbasetwo (int number)
{
int test;
for (int i = 0; i< number; i++){
test = 2 ^ i;
int result = i;
}
return result;
}
int main(){
printf("Enter a positive integer: ");
int number = get_int();
int logresult;
if (number > 0){
logresult=logbase2(number);
}
else (number < 0){
printf("Not a positive number. Re-enter: ");
number = get_int();
}
printf("Log base two of number is:%i", logresult);
}
return 0;
}
Well, in your code , both logbase2() and logbasetwo() are used, which are not the same !!!
You have defined a function named logbasetwo(), but you called logbase2().
Change either of them to match other one.
Also, you need to change the logic test = 2 ^ i;. As mentioned in earlier comment by Mr. #Bathsheba, ^ operator is for XOR, not exponentiation.
You need to use pow().

error C2449 and error C2059 [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 8 years ago.
Improve this question
I'm trying to do a program about printing trapezoids with their length of top and bottom edges. But i can't execute my code to see if it's working. Here's my code:
#include <stdio.h>
int main(void);
{
int top, bot, a, b;
printf("Please enter the length of top:");
scanf("%d", &top);
printf("Please enter the length of bottom:");
scanf("%d", &bot);
for(a = top; a < bot; a++) {
for(b = top; b < a+1; b++) {
printf("* ");}
printf("\n");}
return 0;
}
And here is my error texts:
error C2059: syntax error : '}'
error C2449: found '{' at file scope (missing function header?)
Remove the trailing ; from the int main(void) function line, that is not a valid syntax
int main(void) // this is wrong here: ';'
{
...
Remove the ; after int main(void).
Change
int main(void);
to
int main(void) //no semicolon

Resources