C++ recursion repeating output -
i have code:
#include <iostream> #include <vector> #include <ctime> #include <math.h> #include <string>  using namespace std;  int main() {     srand(time(0));     string command_one;     int slot;     cout<<"one chip or quit?\n";     getline(cin, command_one);     if(command_one=="one chip"){         cout<<"pick slot between 0 , 8 (inclusive)\n";         cin>>slot;         if(slot>=0 , slot<=8){             double position=slot;         }         else{             cout<<"this option invalid!\n";             main();         }     }     else if(command_one=="quit"){         cout<<"have nice day! :d";     }     else{         cout<<"this option invalid!\n";         main();     } } when hits else loop nested in if(command_one=="one chip") returns 
 "this option invalid!
 1 chip, multi chip, or quit?
 option invalid!
 1 chip, multi chip, or quit?"
 should be:
 "this option invalid!
 1 chip, multi chip, or quit?"
 how can fixed?
cin doesn't take \n character.getline() takes empty line input. put getchar() take \n character.
#include <iostream> #include <vector> #include <ctime> #include <math.h> #include <string> #include <stdio.h>  using namespace std;  int main() {     string command_one;     int slot;     cout<<"one chip or quit?\n";     getline(cin, command_one);     if(command_one=="one chip")     {         cout<<"pick slot between 0 , 8 (inclusive)\n";         cin>>slot;          //putting getchar() after cin          getchar();         if(slot>=0 , slot<=8)         {             double position=slot;         }         else         {             cout<<"this option invalid!\n";             main();         }     }     else if(command_one=="quit")     {         cout<<"have nice day! :d";     }     else     {         cout<<"this option invalid!\n";         main();     } } 
Comments
Post a Comment