Main cpp expected primary expression before else

Fix “Expected Primary-Expression Before” in C++ or Arduino

“Expected primary-expression before ‘some‘ token” is one of the most common errors that you can experience in Arduino code. Arduino code is written in C++ with few additions here and there, so it is a C++ syntax error. There are multiple versions of this error, depends on what is it that you messed up. Some are easy to fix, some not so much.

Most of the times (but not always), the error occurs because you have missed something or put it at the wrong place. Be it a semicolon, a bracket or something else. It can be fixed by figuring out what is that you missed/misplaced and placing it at the right position. Let us walk through multiple versions of the error and how to fix them one by one.

We all like building things, don’t we? Arduino gives us the opportunity to do amazing things with electronics with simply a little bit of code. It is an open-source electronics platform. It is based on hardware and software which are easy to learn and use. If I were to explain in simple language what Arduino does – it takes an input from the user in different forms such as touch or light and turns it into an output such as running a motor. Actually, you can even post tweets on Twitter with Arduino.

Читайте также:  Css карта для bhop

How to fix “Expected Primary-Expression Before” error?

I’ll walk you through multiple examples of where the error can occur and how to possibly fix it. The codes that I use as examples in this article are codes that people posted on forums asking for a solution, so all credits of the code go to them. Let’s begin.

Type 1: Expected primary-expression before ‘>’ token

This error occurs when when the opening curly brackets ‘’. To fix this, what you have to do is: check if all of your opening and closing curly brackets match properly. Also, check if you are missing any curly brackets. There isn’t much to this, so I’ll move on to the other types.

Type 2: Expected primary expression before ‘)’ token

Example 1: All credits to this thread. Throughout all of my examples, I will highlight the line which is causing the issue with red.

#include #include #include #define PIN D1 #define NUMPIXELS 597 int red = 0; int green = 0; int blue = 0; int game = 0; Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); /////////////////////////////////////////////////////////////////////////////////////////////////////////// void setup() < Blynk.begin("d410a13b55560fbdfb3df5fe2a2ff5", "8", "12345670"); pixels.begin(); pixels.show(); >//////////////////////////////////////////////////////////////////////////////////////////////////////////// BLYNK_WRITE(V1) < game = 1; int R = param[0].asInt(); int G = param[1].asInt(); int B = param[2].asInt(); setSome(R, G, B); >BLYNK_WRITE(V2) < if (param.asInt()==1) < game = 2; rainbow(uint8_t); // Rainbow > else < >> //////////////////////////////////////////////////////////////////////////////////////////////////////////// void loop() < Blynk.run(); >//////////////////////////////////////////////////////////////////////////////////////////////////////////// void rainbow(uint8_t wait) < uint16_t i, j; for(j=0; jpixels.show(); delay(wait); > // delay(1); > uint32_t Wheel(byte WheelPos) < WheelPos = 255 - WheelPos; if(WheelPos < 85) < return pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3); >if(WheelPos < 170) < WheelPos -= 85; return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3); >WheelPos -= 170; return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0); >BLYNK_WRITE(V3) < if (param.asInt()) < game = 3; setAll(125, 47, 0); //candle >else < >> BLYNK_WRITE(V4) < game = 4; int Bright = param.asInt(); pixels.setBrightness(Bright); pixels.show(); >BLYNK_WRITE(V5) < if (param.asInt()) < game = 5; setAll(85, 0, 255); >else < >> BLYNK_WRITE(V6) < if (param.asInt()) < game = 6; oFF(red, green, blue); // fullOff(); >else < >> BLYNK_WRITE(V7) < if (param.asInt()) < game = 7; setAll(255, 0, 85); >else < >> BLYNK_WRITE(V8) < if (param.asInt()) < game = 8; setAll(90, 90, 90); >else < >> BLYNK_WRITE(V9) < if (param.asInt()) < game = 9; setAll(255, 130, 130); >else < >> //////////////////////////////////////////////////////////////////////////////////////////////////////////// void oFF(byte r, byte g, byte b) < if (game == 1) < offsome(r, g, b); >else if (game == 2) < offall(r, g, b); >else if (game == 3) < offall(r, g, b); >else if (game == 4) < offall(r, g, b); >else if (game == 5) < offall(r, g, b); >else if (game == 6) < offall(r, g, b); >else if (game == 7) < offall(r, g, b); >else if (game == 8) < offall(r, g, b); >else if (game == 9) < offall(r, g, b); >> void offall(byte r, byte g, byte b) < uint32_t x = r, y = g, z = b; for (x; x >0; x--) < if( y >0 ) y--; if( z > 0 ) z--; for(int i = 0; i < NUMPIXELS; i++ ) < pixels.setPixelColor(i, pixels.Color(x, y, z)); >pixels.show(); delay(0); > //delay(0); > void offsome(byte r, byte g, byte b) < uint32_t x = r, y = g, z = b; for (x; x >0; x--) < if( y >0 ) y--; if( z > 0 ) z--; for(int i = 87; i < 214; i++ ) < pixels.setPixelColor(i, pixels.Color(x, y, z)); >for(int i = 385; i < 510; i++ ) < pixels.setPixelColor(i, pixels.Color(x, y, z)); >pixels.show(); delay(0); > > void setAll(byte r, byte g, byte b) < uint16_t x = 0, y = 0, z = 0; for (x; x < r; x++) < if( y < g ) y++; if( z < b ) z++; for(int i = 0; i < NUMPIXELS; i++ ) < pixels.setPixelColor(i, pixels.Color(x, y, z)); >pixels.show(); red = r; green = g; blue = b; delay(0); > //delay(0); > void setSome(byte r, byte g, byte b) < uint16_t x = 0, y = 0, z = 0; for (x; x < r; x++) < if( y < g ) y++; if( z < b ) z++; for(int i = 86; i < 212; i++ ) < pixels.setPixelColor(i, pixels.Color(x, y, z)); >for(int i = 385; i < 512; i++ ) < pixels.setPixelColor(i, pixels.Color(x, y, z)); >pixels.show(); red = r; green = g; blue = b; delay(0); > //delay(0); > void fullOff() < for(int i = 0; i < NUMPIXELS; i++ ) < pixels.setPixelColor(i, pixels.Color(0, 0, 0)); >pixels.show(); >

The error occurs in this code because the rainbow function is supposed to have a variable as its argument, however the argument given here is ‘uint8_t’ which is not a variable.

BLYNK_WRITE(V2) < if (param.asInt()==1) < game = 2; rainbow(uint8_t); // Rainbow > else < >>

Here all you have to do is define uint8_t as a variable first and assign it a value. The code will work after that.

Читайте также:  Python except try passing

Type 3: Expected primary-expression before ‘enum’

Example 1: All credits to this thread.

#include using namespace std; int main() < enum userchoice < Toyota = 1, Lamborghini, Ferrari, Holden, Range Rover >; enum quizlevels < Hardquestions = 1, Mediumquestions, Easyquestions >; return 0; >

The “expected primary-expression before ‘enum’ ” error occurs here because the enum here has been defined inside a method, which is incorrect. The corrected code is:

#include using namespace std; enum userchoice < Toyota = 1, Lamborghini, Ferrari, Holden, RangeRover >; enum quizlevels < HardQuestions = 1, MediumQuestions, EasyQuestions >; int main()

Note: Another mistake has been fixed in this code i.e. the space in “Range Rover” variable. Variable names cannot contain spaces.

Type 4: Expected primary expression before ‘.’

Example 1: All credits go to this thread.

#include using std::cout; using std::endl; class square < public: double length, width; square(double length, double width); square(); ~square(); double perimeter(); >; double square::perimeter() < return 2*square.length + 2*square.width; > int main()

Solution 1: Here the error occurs because “square” is being used as an object, which it is not. Square is a type, and the corrected code is given below.

 #include using std::cout; using std::endl; class square < public: double length, width; square(double length, double width); square(); ~square(); double perimeter(); >; double square::perimeter() < return 2*length + 2*width; > int main()

Type 5: Expected primary-expression before ‘word’

Example 1: All credits go to this thread.

#include #include using namespace std; string userInput(); int wordLengthFunction(string word); int permutation(int wordLength); int main() < string word = userInput(); int wordLength = wordLengthFunction(string word); cout string userInput() < string word; cout > word; return word; > int wordLengthFunction(string word) < int wordLength; wordLength = word.length(); return wordLength; >int permutation(int wordLength) < if (wordLength == 1) < return wordLength; >else < return wordLength * permutation(wordLength - 1); >>

Here, they are incorrectly using string inside wordLengthFunction().

Fixing it is simple, simply replace

int wordLength = wordLengthFunction(string word);
int wordLength = wordLengthFunction(word);

Type 6: Expected primary-expression before ‘else’

Example 1: All credit goes to this thread.

// Items for sale: // Gizmos - Product number 0-999 // Widgets - Product number 1000-1999 // doohickeys - Product number 2000-2999 // thingamajigs - Product number 3000-3999 // Product number >3999 = Invalid Item #include #include #include using namespace std; float ProdNumb; // Product Number double PrG; // Product Number for Gizmo double NG; // Number of items double PG; // Price of Item double PrW; // Product Number for Widgets double NW; // Number of items double PW; // Price of Item double PrD; // Product Number for Doohickeys double ND ; // Number of items double PD ; // Price of Item double PrT; // Product Number for Thingamajigs double NT; // Number of items double PT; // Price of Item double PrI; //Product Number for Invalid (> 3999) double NI; // Number of items double PI; // Price of Item double total = 0; int main () < cout > ProdNumb; while (ProdNumb != -1) < if (ProdNumb >= 0 && ProdNumb > NG; cout > PG; > cout > ProdNumb; else (ProdNumb >= 1000 && ProdNumb > NW; cout > PW; cout > ProdNumb; > else (ProdNumb >= 2000 && ProdNumb > ND; cout > PD; cout > ProdNumb; > else (ProdNumb >= 3000 && ProdNumb > NT; cout > PT; cout > ProdNumb; > else (ProdNumb = 4000) < ProdNumb == PrI; cout > NI; cout > PI; cout > ProdNumb; > > cout

This code is not correct because after the if statement is closed with ‘>’ in this code, there are two statements before the else statement starts. There must not be any statements between the closing curly bracket ‘>’ of if statement and the else statement. It can be fixed by simply removing the part that I have marked in red.

Conclusion

And that’s it, I hope you were able to fix the expected primary-expression before error. This article wasn’t easy to write – I’m in no way an expert in C++, but I do know it to a decent level. I couldn’t find any articles related to fixing this error on the internet so I thought I’d write one myself. Answers that I read in forums helped me immensely while researching for this article and I’m thankful to the amazing community of programmers that we have built! If you would like to ask me anything, suggest any changes to this article or simply would like to write for us/collaborate with us, visit our Contact page. Thank you for reading, I hope you have an amazing day.

Also, tell me which one of the 6 types were you experiencing in the comments below.

Источник

Main cpp expected primary expression before else

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 using namespace std; int main() < int currentparagon; int desiredparagon; int paragonxp; int difference; cout "What is your current paragon level: "; cin>> currentparagon; //Gets current paragon cout << endl; cout "What is your desired paragon level: "; cin>> desiredparagon; cin.ignore(2); if (desiredparagon else if ( desiredparagon >= 61 && else if ( desiredparagon >= 72 && else if ( desiredparagon >= 81 && else if ( desiredparagon >= 81 && return 0; > 
C:\Users\SpencerXZX.marge-HP\Desktop\Paragon Xp\main.cpp||In function 'int main()':| C:\Users\SpencerXZX.marge-HP\Desktop\Paragon Xp\main.cpp|23|error: 'else' without a previous 'if'| C:\Users\SpencerXZX.marge-HP\Desktop\Paragon Xp\main.cpp|23|error: expected primary-expression before '>=' token| C:\Users\SpencerXZX.marge-HP\Desktop\Paragon Xp\main.cpp|23|error: expected primary-expression before ' token| C:\Users\SpencerXZX.marge-HP\Desktop\Paragon Xp\main.cpp|27|error: 'else' without a previous 'if'| C:\Users\SpencerXZX.marge-HP\Desktop\Paragon Xp\main.cpp|27|error: expected primary-expression before ' token| C:\Users\SpencerXZX.marge-HP\Desktop\Paragon Xp\main.cpp|31|error: expected primary-expression before ' token| C:\Users\SpencerXZX.marge-HP\Desktop\Paragon Xp\main.cpp|35|error: expected primary-expression before ' token| C:\Users\SpencerXZX.marge-HP\Desktop\Paragon Xp\main.cpp|39|error: expected '>' at end of input| ||=== Build finished: 8 errors, 0 warnings (0 minutes, 0 seconds) ===|

Источник

Ошибка компиляции "expected '>' before 'else'"

Ошибка при компиляции [Error] expected primary-expression before '==' token
#include <iostream> #include <iomanip> #include <math.h> using namespace std; int main() < .

Ошибка компиляции ":2:1: error: expected unqualified-id"
Привет. Сижу значит пишу код, вроде как написал, сверил с остальными, все точно должно быть верно.

Эксперт CЭксперт С++

Лучший ответ

Сообщение было отмечено DR01D как решение

Решение

Эксперт С++

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include using namespace std; int main()  int a, b, c, d; cin >> a>> b >> c >> d; int k = abs (a - c); int v = abs (b - d); if ((k == 1 && v == 2)  else { cout  "NO"; } return 0; }

Ошибка компиляции "expected ; before >"
Ошибка в программе. Что не так? П.5.18.Правил Запрещено размещать задания и решения в виде.

Создание форм, ошибка при компиляции Fatal: Expected a file name
При компиляции выдает эту ошибку Fatal: Expected a file name: . В чем ошибка, не могу понять.

Ошибка при компиляции - Expected '(' before 'void'
Добрый день. Я компилирую библиотечные функции под арм. На данной функции возникает ошибка.

Источник

Оцените статью