I’ve got a problem with something I’m programming.
I get this error over and over;
jharvard@appliance (~/Dropbox/pset1): make mario
clang -ggdb3 -O0 -std=c99 -Wall -Werror mario.c -lcs50 -lm -o mario
mario.c:23:5: error: expected identifier or '('
do
^
mario.c:32:1: error: expected identifier or '('
do
^
2 errors generated.
I searched all over the internet but couldn’t find the problem..
removing the ; after int main(void)
didn’t help
This is my code:
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void);
//Ask User for Height, and check
int a, b, rows, height;
int a = 0;
int b = 0;
int rows = 1;
do
{
printf ("Height: ");
height = GetInt();
}
while (height <=0 || height > 23);
//build half pyramid
do
{
do
{
printf("r");
a++;
}
while (a < height - rows);
do
{
printf("#");
b++;
}
while (b < rows + 1);
printf("n");
rows++;
while (rows <= height);
}
I’ve been trying to solve this problem for a few days, but i just can’t figure it out!
Thank you so much in advance!
asked Dec 23, 2012 at 14:12
3
int main(void);
You have just declared the main
. You need to define it and add the code inside that definition.
int main()
{
.....
}
answered Dec 23, 2012 at 14:17
Alok SaveAlok Save
200k51 gold badges425 silver badges531 bronze badges
0
You got nested loop with do/while. Make sure that each start with do end with while.
Look like at the end of file, the «while» is not correct.
printf("n");
rows++;
while (rows <= height);
}
That could be you missing the close ‘}’ before ‘while (rows <= height);’
Correct code could be:
int main(void)
{
//Ask User for Height, and check
int a, b, rows, height;
a = 0; // <- removed int
b = 0; // <- removed int
rows = 1; // <- removed int
do
{
printf ("Height: ");
height = GetInt();
}
while (height <=0 || height > 23);
//build half pyramid
do
{
do
{
printf("r");
a++;
}
while (a < height - rows);
do
{
printf("#");
b++;
}
while (b < rows + 1);
printf("n");
rows++;
} // <- add }
while (rows <= height);
}
answered Dec 23, 2012 at 14:21
0
Main post is edited, so clear answer.
All your code is outside of a function because you’re doing int main(); you’re declaring a function. Use {} brackets instead.
int main() {
//Code here.
}
answered Dec 23, 2012 at 14:16
BlastcoreBlastcore
3507 silver badges19 bronze badges
2
int a, b, rows, height;
int a = 0;
int b = 0;
Here in your above statements a
and b
are re-declared more than once causing compilation error.
answered Dec 23, 2012 at 14:25
Take the last while outside of the do scope:
while (rows <= height);
answered Dec 23, 2012 at 14:22
Konstantin DinevKonstantin Dinev
33.9k14 gold badges74 silver badges100 bronze badges
If you don’t indent your code, which you (by all means) should do, at least write the starting and the ending curly brackets at once when you write the loop statement, before putting any code into that loop’s body (which goes between the curly brackets). It will save you from troubles like these in the future.
answered Dec 23, 2012 at 16:14
VoicuVoicu
16.3k10 gold badges58 silver badges67 bronze badges
0
In case of react native project …. its a simple issue.
You can go to you project target in ios and check Build, version and build identifier… they might have extra space on the end which should be removed
Hope it helps. Worked for me
answered Jun 4, 2021 at 3:57
Rahul ShakyaRahul Shakya
1,13914 silver badges13 bronze badges
Raindrops 0 / -1 / 0 Регистрация: 23.03.2017 Сообщений: 41 |
||||
1 |
||||
26.03.2017, 00:39. Показов 20361. Ответов 8 Метки c++, errore (Все метки)
Ошибка следующая — «error: expected identifier or ‘(‘ Казалось бы, где тут можно накосячить, ан нет, нашел таки как. Может кто подскажет где именно?
__________________
0 |
3433 / 2812 / 1249 Регистрация: 29.01.2016 Сообщений: 9,426 |
|
26.03.2017, 00:42 |
2 |
Может кто подскажет где именно? Компилятор строку не указывает? В 13-й убери точку с запятой.
0 |
0 / -1 / 0 Регистрация: 23.03.2017 Сообщений: 41 |
|
26.03.2017, 00:48 [ТС] |
3 |
Вот черт. Дело и правда было в точке с запятой. Спасибо! К сожалению — нет, лишь таким вот макаром ^ подчеркивает ошибку.
0 |
3433 / 2812 / 1249 Регистрация: 29.01.2016 Сообщений: 9,426 |
|
26.03.2017, 01:11 |
4 |
К сожалению — нет, лишь таким вот макаром ^ подчеркивает ошибку. Сомневаюсь. Что за среда? Добавлено через 5 минут «error: expected identifier or ‘(‘
0 |
0 / -1 / 0 Регистрация: 23.03.2017 Сообщений: 41 |
|
26.03.2017, 01:28 [ТС] |
5 |
Среда — ide.cs50.io
Перед этим, что в строке написано? hello.c:15:1: error: expected identifier or ‘(‘ Ошибка целиком. Hello.c — название файла с кодом
0 |
3433 / 2812 / 1249 Регистрация: 29.01.2016 Сообщений: 9,426 |
|
26.03.2017, 01:30 |
6 |
hello.c:15:1: Файл hello.c, строка 15-я, символ 1-й.
0 |
0 / -1 / 0 Регистрация: 23.03.2017 Сообщений: 41 |
|
26.03.2017, 01:42 [ТС] |
7 |
Оуу. Спасибо) Но проблему все ровно решило удаление » ; «. Хотя, наверное именно из за точки с запятой программа ругалась на скобку
0 |
3433 / 2812 / 1249 Регистрация: 29.01.2016 Сообщений: 9,426 |
|
26.03.2017, 01:46 |
8 |
Хотя, наверное именно из за точки с запятой программа ругалась на скобку Естественно, но место в коде, которое компилятору не нравится, он всегда указывает.
0 |
zss Модератор 12641 / 10135 / 6102 Регистрация: 18.12.2011 Сообщений: 27,170 |
||||
26.03.2017, 08:21 |
9 |
|||
Raindrops, а где объявлен тип «string»? ДА, и по формату
printf(«Привет,%sn»,name); Печатаются char* переменные, а не string Total:
0 |
IT_Exp Эксперт 87844 / 49110 / 22898 Регистрация: 17.06.2006 Сообщений: 92,604 |
26.03.2017, 08:21 |
Помогаю со студенческими работами здесь Ошибка компиляции «expected ; before }» Перечисление символов выдает ошибку «Expected identifier» Компилатор, ошибка «expected primary-expression before «int» « int… При компиляции ошибка: C2228: left of «.real»,».imag» must have struct/class/union Искать еще темы с ответами Или воспользуйтесь поиском по форуму: 9 |
-
01-29-2014
#1
Registered User
Expected Identifier ‘(‘ in a Do/While loop
I keep getting the «Error: expected identifier ‘(‘» message when I’m compiling. Can someone please explain to me what I’m doing wrong? I’ve been stuck on this for hours.
Code:
#include <cs50.h> #include <stdio.h> int main (void); //loop until user provides integer between 0 and 23 int blknm; {do { printf("Give me an int between 0 and 23n"); blknm = GetInt(); } while (blknm <= 0 && blknm >= 23);}
-
01-29-2014
#2
Registered User
The proper form of main is:
Code:
int main(void) { // code return 0; }
No semicolon after the main declaration, and all the code in «main()» enclosed in braces { }
[edit]
Well, one of the proper forms — more info here if you’re interested: http://faq.cprogramming.com/cgi-bin/…&id=1043284376
Last edited by Matticus; 01-29-2014 at 05:23 PM.
-
01-29-2014
#3
Registered User
It’s giving me that error on my Do function. I’m using an appliance for class (my first time attempting programming) and it shows me the following:
error: expected identifier or ‘(‘
{do
^Thanks for your help already!
-
01-29-2014
#4
Registered User
Move that brace to immediately after «int main(void)» (before you declare your variable «blknm»).
You should always have a matching number of opening and closing braces. In this case, you do, but the major ones (encapsulating the code within «main()») are leaving out the code which declares that variable.
-
01-29-2014
#5
Registered User
Matticus is well aware of where the compiler said your problem was. He told you how to fix that error. When you are a newbie, and ask an experts advice, you should try their advice before you tell them they’re wrong.
«do» is not a function, it’s a keyword, used for loops. The error is thus being reported on your do loop. Unfortunately, where the compiler reports the error, and where the actual error are, are not always the same. The reason for this is simple: the compiler does it’s best to interpret the code you provided, and only throws an error when the code it’s compiling can only be interpreted in a way that is an error, per the C language. I.e. where you have invalid C code. Basically, it tries to get as far as it can. Sometimes, a line of code you wrote has a logical/semantic error in it, but this error results in valid C code, that simply is not the code you intended, i.e. you «misspoke» in your code. Since the compiler can’t read your mind and know your intentions, it must work with the code you give it.
This is the case here. The declaration of main on line 5 is valid, but that extraneous semicolon means it is only a declaration (i.e. a prototype, telling the compiler what the function looks like), not a definition. That is, the extraneous semicolon prevents the compiler for being able to interpret lines 8-16 as the body of the main function. Still, it happily accepts line 5, and moves on down to line 9. It sees a valid variable definition (although it’s a global — ick!), and accepts line 9.
Then it gets to line 11, and is confused. You’re not in a function (for the above reason), but you’re trying to write a loop. Loops can only exist inside functions. That is where the compiler complains since that is the first line of invalid C code. The problem is still, as Matticus stated, to remove the extraneous semicolon, and put all the code (including variable declarations) inside the body of the main function.
-
01-30-2014
#6
Registered User
I wasn’t trying to tell him he was wrong…just that I was still getting the error. But once I removed the semicolon and redid my indentations, it started working.
Anduril, thanks for your help in explaining the ‘behind-the-scenes’ of the program. I never would have realized the problem with the Do loop was stemming from the int main(void).
Now, you said «(although it’s a global — ick!).) What would be the danger with a global variable?
-
01-30-2014
#7
Registered User
-
01-30-2014
#8
Registered User
Awesome…thanks for the resource!!
-
02-03-2014
#9
Registered User
I’m sorry to come back to you guys again, but I’m getting that same error code again now that I’m further into my code. Can you see what is causing it this time?
Code:
int main(void) { int height; do { printf("Give me an int between 0 and 23n"); height = GetInt(); } while (height <= 0 || height >= 23); } int space; int count; int hash; for (count = 0, count < height; count++) { for (space = 0, space < height - 2; space++) {printf(" ")} { for (hash = 0, hash < height; hash++) {printf("#n")} } }
-
02-03-2014
#10
Registered User
All your code must be in a function. Some of the code you posted is not in a function (e.g. not in main).
Pay careful attention to your curly braces. Also, enable or learn to use the auto-indent function in your editor, it will help you find where you have misplaced/missing curly braces. Also, most good programming editors have a function that will find the matching parenthesis, square or curly bracket, which is also helpful.
Topic: 50$ Robot: expected identifier or ‘(‘ before ‘while’ (Read 2618 times)
0 Members and 1 Guest are viewing this topic.
Hello,
I have been following the 50$ robot tutorial and I have only the programming step left!
I added the Photovore_v1.c to source files under the AVR Studio, and followed the tutorial to the point of «Rebuild All»
At that point, I received 1 error and 3 warnings; the error being «expected identifier or ‘(‘ before while.«
Here is the code that I copied and pasted in:
LED_off();//turn LED on
while(1)
{
//store sensor data
sensor_left=a2dConvert8bit(5);
sensor_right=a2dConvert8bit(4);
//detects more light on left side of robot
if(sensor_left > sensor_right && (sensor_left — sensor_right) > threshold)
{//go left
servo_left(44);
servo_right(44);
}
//detects more light on right side of robot
else if(sensor_right > sensor_left && (sensor_right — sensor_left) > threshold)
{//go right
servo_left(25);
servo_right(25);
}
//light is about equal on both sides
else
{//go straight
servo_left(25);
servo_right(44);
}
/* Servo Test Code
i=250;
while(i>0)
{
servo_left(40);
i—;
}
i=250;
while(i>0)
{
servo_left(24);
i—;
}
*/
//rprintf(«Initialization Completern»);
//output message to serial (use hyperterminal)
//print(«Hello, World! Read My Analog: %urn», sensor_0);
delay_cycles(500);//a small delay to prevent crazy oscillations
}
My Intuition of my issue:
I have little knowledge of C; I am somewhat conversant in Python and Basic, I would think there must be «definitions» or parameters that I am missing. The code above is simply the bit that the downloaded Photovore_v1.c file shows to be added.
Any help is appreciated!
nfwill
Logged
It looks right . . . I suspect there is other code above or below that, which could cause a problem . . .
Also, try to match the line number of the error to the actual code.
Logged
is LED_off() supposed to be a function?
the following code would make this a function
LED_off() {//turn LED on
while(1)
{
//store sensor data
sensor_left=a2dConvert8bit(5);
sensor_right=a2dConvert8bit(4);
//detects more light on left side of robot
if(sensor_left > sensor_right && (sensor_left - sensor_right) > threshold)
{//go left
servo_left(44);
servo_right(44);
}
//detects more light on right side of robot
else if(sensor_right > sensor_left && (sensor_right - sensor_left) > threshold)
{//go right
servo_left(25);
servo_right(25);
}
//light is about equal on both sides
else
{//go straight
servo_left(25);
servo_right(44);
}
/* Servo Test Code
i=250;
while(i>0)
{
servo_left(40);
i--;
}
i=250;
while(i>0)
{
servo_left(24);
i--;
}
*/
//rprintf("Initialization Completern");
//output message to serial (use hyperterminal)
//print("Hello, World! Read My Analog: %urn", sensor_0);
delay_cycles(500);//a small delay to prevent crazy oscillations
}
Based on code spacing and the trailing ‘}’ you have either not copied the entire code, or the code you copied was bunk to begin with.
Logged
-garrett
Is that the whole file you’re posting or a snippet? The whole file should probably have some #includes etc. at the top
Logged
Thank you for your responses Admin, garrettg84 and richiereynolds.
I first tried garrettg84’s code; the compiler unfortunately complained and mentioned more ‘undefined variables’.
My problem ended up being I read too much into the process, and richiereynolds implication that a ‘snippet’ can be the source of my issues helped. The code I did use was a snippet; complications I faced with AVR Studio caused me to use the snippet which at the time made a lot of sense: the ‘entire code’ mentioned using a specific part of its own code, perhaps I misunderstood. Getting the entire code into AVR through crude ‘copying and pasting’ rather than browsing through the files allowed it to successfully compile and functions correctly. I am very excited to be finishing up my first robot!
Thank you all for your time and help!
Much Appreciated,
nfwill
« Last Edit: May 27, 2011, 09:14:19 PM by nfwill »
Logged
EDIT Git code for this post here: https://gist.github.com/anonymous/6566542 (Sorry, the ordering of the files seems off from the description posted for it)
Hi, I am seeing a strange error trying to compile a code. Here’s the error message:
In file included from basic.h:55:0,
from ADMM_alg.h:12,
from ADMM_alg.c:1:
/usr/include/stdlib.h:771:12: error: expected identifier or '(' before 'int'
/usr/include/stdlib.h:771:12: error: expected ')' before '>=' token
In file included from ADMM_alg.h:11:0,
from ADMM_alg.c:1:
mymatlib.h:24:8: error: expected identifier or '(' before 'double'
mymatlib.h:24:8: error: expected ')' before '>=' token
mymatlib.h:25:8: error: expected identifier or '(' before 'double'
mymatlib.h:25:8: error: expected ')' before '<=' token
ADMM_alg.c is the main code here, and line 1 is:
#include "ADMM_alg.h"
And line 12 of ADMM_alg.h is :
#include "basic.h"
I have used basic.h in other codes too, where it compiled and worked just fine. Besides, the error here seems to be shown inside a standard library, stdlib.h. I don’t know where to look for bugs.
And the other header file where errors are being pointed out, mymatlib.h, has also been used in other codes, and it worked fine.
This definitely has something to do with ADMM_alg.c, or ADMM_alg.h, but I don’t know what to look for, the error message seems very ambiguous to me.
Can someone please tell what this error message could mean? Any help is much appreciated!
Thank you!
EDIT: To make the question a little clearer:
Basically what’s happening is, when I do:
gcc main.c a1.c a2.c mymatlib.c -o main -lsomelibraries
It works just fine.
Now when I do :
gcc main.c a1.c a2.c NewFile.c mymatlib.c -o main -lsomelibraries
I get the above error, which says there’s an error in mymatlib.c. Now that is what doesn’t make sense to me- if you see the first line of code I wrote (in this reply), then you’ll see that the very first time that it did work, I did have mymatlib.c also being compiled. Why does the inclusion of NewFile.c cause problems in compiling, and more importantly, why are those errors being shown to be part of mymatlib.c?
Hello,
I’m trying to work with I2C and I have the following code:
#include <stdio.h>
#include <avr/io.h>
#include <util/delay.h>
#include <compat/twi.h>
#define I2C_START
#define MAX_TRIES
#define I2C_DATA
#define I2C_STOP
#define i2c_writebyte
unsigned char i2c_transmit(unsigned char type) {
while (!(TWCR & (1 << TWINT)));
return (TWSR & 0xF8);
}
{
switch(type) {
case I2C_START:
TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);
break;
case I2C_DATA:
TWCR = (1 << TWINT) | (1 << TWEN);
break;
case I2C_STOP:
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
return 0;
}
}
int i2c_writebyte (unsigned int i2c_address, unsigned int dev_id,
unsigned int dev_addr,char data) {
unsigned char n = 0;
unsigned char twi_status;
char r_val = -1;
i2c_retry:
if (n++ >= MAX_TRIES) return r_val;
twi_status=i2c_transmit(I2C_START);
int i2c_readbyte(unsigned int i2c_address, unsigned int dev_id,
unsigned int dev_addr, char *data)
{
unsigned char n = 0;
unsigned char twi_status;
char r_val = -1;
i2c_retry:
if (n++ >= MAX_TRIES) return r_val;
twi_status=i2c_transmit(I2C_START);
if (twi_status == TW_MT_ARB_LOST) goto i2c_retry;
if ((twi_status != TW_START) && (twi_status != TW_REP_START)) goto i2c_quit;
TWDR = (dev_id & 0xF0) | ((dev_addr << 1) & 0x0E) | TW_WRITE;
twi_status=i2c_transmit(I2C_DATA);
if ((twi_status == TW_MT_SLA_NACK) || (twi_status == TW_MT_ARB_LOST)) goto i2c_retry;
if (twi_status != TW_MT_SLA_ACK) goto i2c_quit;
TWDR = i2c_address;
twi_status=i2c_transmit(I2C_DATA);
if (twi_status != TW_MT_DATA_ACK) goto i2c_quit;
TWDR = i2c_address >> 8;
twi_status=i2c_transmit(I2C_DATA);
if (twi_status != TW_MT_DATA_ACK) goto i2c_quit;
twi_status=i2c_transmit(I2C_START);
if (twi_status == TW_MT_ARB_LOST) goto i2c_retry;
if ((twi_status != TW_START) && (twi_status != TW_REP_START)) goto i2c_quit;
TWDR = (dev_id & 0xF0) | ((dev_addr << 1) & 0x0E) | TW_READ;
twi_status=i2c_transmit(I2C_DATA);
if ((twi_status == TW_MR_SLA_NACK) || (twi_status == TW_MR_ARB_LOST)) goto i2c_retry;
if (twi_status != TW_MR_SLA_ACK) goto i2c_quit;
twi_status=i2c_transmit(I2C_DATA);
if (twi_status != TW_MR_DATA_NACK) goto i2c_quit;
*data=TWDR;
r_val=1;
i2c_quit:
twi_status=i2c_transmit(I2C_STOP);
return r_val;
}
int main(void)
{
char buffer[34]= {0b00001111,0b11110000,
0b00000001,
0b00000011,
0b00000110,
0b00001100,
0b00011001,
0b00110011,
0b01100110,
0b11001100,
0b10011000,
0b00110000,
0b01100000,
0b11000000,
0b10000000,
0b00000000,
0b00000000,
0b00000000,
0b10000000,
0b11000000,
0b01100000,
0b00110000,
0b10011000,
0b11001100,
0b01100110,
0b00110011,
0b00011001,
0b00001100,
0b00000110,
0b00000011,
0b00000001,
0b00000000,
0b00000000,
0b00000000,
};
char data,id1,id2;
unsigned int dev_address,i,idelay;
DDRD=0xFF;
PORTD=0x00;
ADMUX=0x00;
ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1);
ADCSRB = 0x00;
DIDR0 = 0x01;
TWSR = 0x00;
TWBR = 0x30;
dev_address=0;
i2c_readbyte(dev_address,EEPROM_ID,EEPROM_ADDR,&id1);
i2c_readbyte(dev_address + 1,EEPROM_ID,EEPROM_ADDR,&id2);
if (id1 != buffer[0] || id2 != buffer[1]) {
for(i=0;i < 34;i++) {
i2c_writebyte(dev_address + i,EEPROM_ID,EEPROM_ADDR,buffer[i]);
_delay_us(1);
}
}
idelay=100;
for(;;) {
for(i=2;i < 34;i++) {
ADCSRA |= (1<<ADSC);
while (ADCSRA & (1<<ADSC));
idelay = ADCW;
i2c_readbyte(dev_address + i,EEPROM_ID,EEPROM_ADDR,&data);
PORTD=data;
_delay_ms(idelay);
}
}
return 0;
}
I get 2 errors and 1 warning.
The errors are:
../1121.c:32: error: expected identifier or ‘(‘ before ‘unsigned’
../1121.c:17: error: expected identifier or ‘(‘ before ‘{‘ token
how can I fix them?
best regards,
Floris