EvilZone
Programming and Scripting => C - C++ => : Madsam97 May 02, 2011, 04:34:07 PM
-
I have to write a program for school where i have nested if else statments. Its required so that you can go into the nested statements and stay there until you tell the program to go back to the original if else statements.
Any code snippets or ideas would be wonderful.
-
I must say, nesting If's to decide whether the program closes or not doesn't sound much useful.
Also nested if's and else's are usually the lack of the operand && as you can easily make a check in the same line.
if(opt1==true){
if(opt2==true){
cout<<"alsas";
}else{
cout<<"Missing 2"<<endl;
}
}else{
cout<<"Missing 1"<<endl;
}
The code above could be coded like:
if( (opt1==true) && (opt2==true) ){
cout<<"alsas";
}else if(opt2!=true){
cout<<"Missing 2"<<endl;
}else{
cout<<"Missing 1"<<endl;
}
If you want to perform something untill you get an input you should simply make a while().
Like:
while(strncasecmp(option, "exit", 4) != 0){
//yourcode
}
return 0; // or exit(0);
Anyhow I made some stupid thing, I don't know if it's any helpful, hope it is.
int main (int argc, char * argv[]){
bool opt=false;
cout<<"Insert a number"<<endl;
while(opt==false){
cin>>mynum;
if((mynum >0)&& (mynum<100)){
cout<<mynum*mynum<<endl;
cout<<"Do you wish to continue? or exit?"<<endl;
if((strncasecmp(option, "exit", 4)) == 0){
opt=true;
}else if((strncasecmp(option, "continue", 12)) == 0){
cout<<"You can simply insert a number if you wish to continue"<<endl;
}
}
}
return 0;
}
-
Thats actually exactly what I needed. Thanks!
-
Since when was this a homework hub?
DIY
-
I'm not making anyone's homework, I'm solving an exercise and explaining it, for those that want to learn, read the explanation, and with the homework done, they'll learn, those that don't will have to do it themselves next time.
But hey, at least I'm contributing.