New course! Every coder should learn Generative AI!
Try a free lesson+ 3
why does this code output that there is an error because the else statement doesn't have a corresponding if statement?. How do i correct this? #include <iostream> using namespace std; int main() { int a; for (int x =0; x<5;x++){ cin>>a; if (a<1){ cout<<"a cannot be less than 1\n"; } else if (a>10){ cout <<"a cannot be greater than 10\n"; } } else{ cout<<"code can run now"<<endl; } return 0; }
8 Answers
+ 6
I didn't even know it was possible to make a title that long...
+ 4
#include <iostream>
using namespace std;
int main() {
int a;
for (int x =0; x<5;x++)
{
cin>>a;
if (a<1){
cout<<"a cannot be less than 1\n";
}
else if (a>10){
/* as your doesn't uses multiple if statements no
need to use "else if" use "else only" */
cout <<"a cannot be greater than 10\n";
}
} //your loop terminates here
else{
/* no if statement for this else which is an error */
/*write it without else statement */
cout<<"code can run now"<<endl;
/* use break; here for 2. opinion.
}. /* for 2 and 3. opinion terminate loop here */
return 0;
}
1.one more thing you executes your loop 5 times,
I think it's not a good practice, if any user input wrong no each 5 times then also inlast this will say that "code can run now"
I think you may not use any loop,
2. or use a infinite loop depending on the value of a which doesn't stop until user input a<10 or a>1;
3. if you want to execute only five times, then terminate your loop after the termination of second else statement,but here if any user input the correct value at its first time, then it feels a find of irritating because it will ask for the value a every time;
to overcome this use break; in your second else statement.
+ 2
Urgh. Debugging code in posts using proportional (variable-width) fonts?
Elema's code is at:
https://code.sololearn.com/cmLr5M2EFJKg
+ 1
AKCodingBlog ...I tried that but it resulted in the code looping the 'code can run now' and displaying it five times.
Unless you meant taking away the two curly brackets and putting it after the else statement of the (which also resulted in an error).
For better clarity, can you please copy my code and put the curly brackets at the right place and send it here. thanks
+ 1
#include <iostream>
using namespace std;
int main() {
int a;
cin>>a;
for (int x =0; x<5;x++){
if (a<1){
cout<<"a cannot be less than 1\n";
}
else {
if (a>10){
cout <<"a cannot be greater than 10\n";
}
else{
cout<<"code can run now"<<endl;
}
}
}
return 0;
}
this will do the trick....... learn first
0
First please find a shorter title next time and write your problem with a well formed code in the description of your question because this is unreadable.
Now we come to your problem.
You definded following structure
for(){
if(){
... // lines of code
}else if(){
... // lines of code
} // <--- this bracket is at a wrong place, it should be after your else{} case
}else{}
0
what have been your input?
0
#wrong word understand it first then correct yours.....