+ 5
What is wrong with that code please help me I can't figure out
https://sololearn.com/compiler-playground/cwHaCiEelzQ6/?ref=app
16 Antworten
+ 3
don't put return 0; on the beginning. it will cause main() to exit immediately.
if conditions are enclosed in parenthesis
if (points<20)
#include <iostream>
using namespace std;
int main() {
// return 0; ❌ wrong
int points;
cin>>points;
cout << points;
if (points<20) { ✅ use ( )
cout << "Tu as rouge."<<endl;
}
return 0; ✅ put return 0; here
}
+ 6
Bhushan kale keep your language clean on Sololearn. Personal attacks are unacceptable and will get your account banned.
+ 4
There are two problems with your code.
The `return 0;` statement returns a value from the function(in this case, main()) and stops the function. Also, you need to wrap your conditions inside these brackets– ( )
Here is the fixed, working code
```
#include <iostream>
using namespace std;
int main() {
// remove return 0 from here
int points;
cin>>points;
cout << points;
if (points<20) { // wrap your condition in parenthesis
cout << "Tu as rouge."<<endl;
}
return 0; // putting it here ensures all code runs before termination
}
```
You might want to use `cout <<points << endl;` for better formatting
+ 3
As the code evolves, errors evolve too. Keep trying! Errors are a constant companion to developing code - even for professional programmers.
Though you corrected the above problems, new changes added another. The latest problem is that the less-than-or-equal comparison operator is mistyped. It should be <=, not =<.
+ 3
other than being written the wrong way, (most people would put the >= on the left and the <= on the right of &&), your conditions are mostly inverted.
also, else does not accept any condition.
#include <iostream>
using namespace std;
int main() {
int points;
cin>>points;
cout <<points <<" points.";
if(points>=0 && points<=19) {
cout << "Tu as rouge."<<endl;
}
else if (points>=20 && points<=39){
cout << "Tu as jaune."<<endl;
}
else if (points>=40 && points<=59){
cout << "Tu as vert pale."<<endl;
}
else if (points >=60 && points<=79){
cout << "Tu as vert."<<endl;
}
else if (points>=80 && points<=99){
cout << "Tu as vert +."<<endl;
}
else{
cout << "Tu ment on peut pas avoir plus de 100 points."<<endl;
}
return 0;}
+ 3
Thanks
+ 3
U may also use switch statement that is easy to understand and write
+ 2
The code has minor error you need to write return 0; line below if statement and in if statement you need to add common brackets inside the condition
Like this below
#include <iostream>
using namespace std;
int main() {
int points;
cin>>points;
cout << points;
if (points<20) {
cout << "Tu as rouge."<<endl;
}
return 0;
}
+ 2
Ok thanks guys
+ 2
Please I got another problem and I don't understand it
+ 2
Alban you can give range in switch statement too so less error. Check out the link below for understanding how it works
https://www.geeksforgeeks.org/cpp/using-range-in-cpp-switch-case/
+ 2
Ok I will do it
+ 1
What did he said ?
+ 1
No because I have to say case 1,case 2 and do it untik I reach 100 so it is way more dificult to read and I will make more mistakes
+ 1
Aysha, Alban
while using range in switch-case works here in Sololearn, you should be aware that it's a compiler dependent feature.
it does not work for non-GCC compilers, so you should test it and be aware that it's a GCC extension and might not work if your code happens to be compiled with other compilers...
+ 1
Ok