RevengeOfTidus
January 12th, 2010, 05:51 AM
Hey everyone,
My friend and I are trying to create a basic command line interface to a research program we are doing. We have basic/intermediate knowledge of C++ but are having trouble with making it "idiot-proof." The program responds accordingly when we pass any integer value to the menu. However, whenever we pass a char/string value, it freaks out and prints the menu loop infinite amounts of times, not prompting the user again. What type of error-checking/correcting code should we use so that when the user inputs a char/string value, it re-prompts the user again?
Thank you for your time,
-RoT
#include <iostream>
#include <cmath>
using namespace std;
int main( int argc, char** argv ) {
int choice = 0;
cout << "Welcome to the OpenCV Test Program\n";
cout << "Please choose from the following to test out certain functions\n";
while (true){
choice = 0;
cout << "Note: Please do not enter anything but numbers!\n";
cout << "1. Display snap.jpg \n";
cout << "2. Display snap.jpg with 'grain'\n";
cout << "3. Display snap.jpg with applied edge detection algorithm\n";
cout << "4. Stream video\n";
cout << "5. Stream video with applied edge detection algorithm\n WARNING: Resource intensive\n";
cout << "6. Take picture from camera\n";
cout << "7. Take picture and compare to snap.jpg (experimental)\n";
cout << "8. Exit\n";
cin >> choice;
switch (choice){
case 1 : //choice 1
break;
case 2 : //choice 2
break;
case 3 : //choice 3
break;
case 4 : //choice 4
break;
case 5 : //choice 5
break;
case 6 : //choice 6
break;
case 7 : //choice 7
break;
case 8 : "Thank you for your time \n";
exit(0);
default : "Please enter a choice from the menu\n";
choice = 0;
break;
}//end switch
}//end while
}
My friend and I are trying to create a basic command line interface to a research program we are doing. We have basic/intermediate knowledge of C++ but are having trouble with making it "idiot-proof." The program responds accordingly when we pass any integer value to the menu. However, whenever we pass a char/string value, it freaks out and prints the menu loop infinite amounts of times, not prompting the user again. What type of error-checking/correcting code should we use so that when the user inputs a char/string value, it re-prompts the user again?
Thank you for your time,
-RoT
#include <iostream>
#include <cmath>
using namespace std;
int main( int argc, char** argv ) {
int choice = 0;
cout << "Welcome to the OpenCV Test Program\n";
cout << "Please choose from the following to test out certain functions\n";
while (true){
choice = 0;
cout << "Note: Please do not enter anything but numbers!\n";
cout << "1. Display snap.jpg \n";
cout << "2. Display snap.jpg with 'grain'\n";
cout << "3. Display snap.jpg with applied edge detection algorithm\n";
cout << "4. Stream video\n";
cout << "5. Stream video with applied edge detection algorithm\n WARNING: Resource intensive\n";
cout << "6. Take picture from camera\n";
cout << "7. Take picture and compare to snap.jpg (experimental)\n";
cout << "8. Exit\n";
cin >> choice;
switch (choice){
case 1 : //choice 1
break;
case 2 : //choice 2
break;
case 3 : //choice 3
break;
case 4 : //choice 4
break;
case 5 : //choice 5
break;
case 6 : //choice 6
break;
case 7 : //choice 7
break;
case 8 : "Thank you for your time \n";
exit(0);
default : "Please enter a choice from the menu\n";
choice = 0;
break;
}//end switch
}//end while
}