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; } | Sololearn: Learn to code for FREE!
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; }

26th Dec 2016, 9:30 AM
Cody Arthur
Cody Arthur - avatar
8 Answers
+ 6
I didn't even know it was possible to make a title that long...
26th Dec 2016, 10:54 AM
Ahri Fox
Ahri Fox - avatar
+ 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.
26th Dec 2016, 12:38 PM
Nikhil Dhama
Nikhil Dhama - avatar
+ 2
Urgh. Debugging code in posts using proportional (variable-width) fonts? Elema's code is at: https://code.sololearn.com/cmLr5M2EFJKg
26th Dec 2016, 11:07 AM
Kirk Schafer
Kirk Schafer - avatar
+ 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
26th Dec 2016, 9:58 AM
Cody Arthur
Cody Arthur - avatar
+ 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
26th Dec 2016, 11:24 AM
Leon lit
Leon lit - avatar
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{}
26th Dec 2016, 9:41 AM
Andreas K
Andreas K - avatar
0
what have been your input?
26th Dec 2016, 11:09 AM
Andreas K
Andreas K - avatar
0
#wrong word understand it first then correct yours.....
26th Dec 2016, 11:27 AM
Leon lit
Leon lit - avatar