error prevention

Discussion in 'Programmer's Corner' started by mentaaal, Mar 16, 2008.

  1. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    Hi all,

    Everytime i have made a program and received an input from the user, such as:

    int choice;
    cout << " Please enter 1 for addition or 2 for subtraction.\n";
    cin >> option;

    if the user's input is not a number like say for example the user decides to press a letter iinstead of a number, the program would go into an infinite loop. Is there any way of preventing an error such as this?

    Thanks,
    Greg
     
  2. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Can you provide us with the code that you are using so that we can see firsthand how you are handling the input?

    hgmjr
     
  3. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
    There are several ways to prevent such things from killing your code. You have to test each input to see if it is within an expected range.

    If your language had a SELECT CASE structure, for instance, then something like this might work:

    SELECT CASE input (where input is the variable holding the input character)
    Case 1
    Set addition flag to true
    Case 2
    Set subtraction flag true
    Case Else
    input = 0
    END SELECT

    You could also do a couple of IF - THEN test to eliminate all inputs except 1 or 2.

    Never let an unexamined input affect program execution.
     
  4. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    Ok well this is the code i made for a homework question.
    If i press a letter key anywhere in this program it will loop:

    Code ( (Unknown Language)):
    1.  
    2. #include <iostream>
    3. using namespace std;
    4. void feet2metres(float, float&);
    5. void metres2feet(float, float&);
    6. void menu();
    7. void displayresults(float,float,int);
    8.  
    9. void main()
    10. {
    11.     menu();
    12. }
    13.  
    14. void menu()
    15.  
    16. {
    17.     float var1,var2;
    18.     int option;
    19.     char goagain;
    20.      cout << "Welcome to the distance converter.\n";
    21.     do
    22.     {
    23.         do
    24.         {
    25.             cout << "Please press 1 if you wish to convert from metres to feet.\n"
    26.                  << "Please press 2 if you wish to convert from feet to metres.\n";
    27.             cin >> option;
    28.         }
    29.         while (option != 1 && option != 2);
    30.         switch (option)
    31.          {
    32.             case 1:
    33.             do
    34.             {
    35.                 cout << "Please enter the distance in metres.\n";
    36.                 cin >> var1;
    37.                 metres2feet(var1,var2);
    38.                 displayresults(var1,var2,option);
    39.             }
    40.             while (var1 < 0);
    41.             break;
    42.             case 2:
    43.             do
    44.             {
    45.                cout << "Please enter the distance in feet.\n";
    46.                cin >> var1;
    47.                feet2metres(var1,var2);
    48.                displayresults(var1,var2,option);
    49.              }
    50.             while (var1 < 0);
    51.             break;
    52.          
    53.             default:
    54.             cout << "Invalid Option.\n";
    55.          }
    56.         do
    57.         {
    58.             cout << "Would you like to go again? (Y/N)\n";
    59.             cin >> goagain;
    60.         }
    61.         while (goagain != 'Y' && goagain !='y' && goagain != 'N' && goagain != 'n');
    62.        
    63.     }
    64.     while (goagain == 'Y' || goagain == 'y');
    65. }
    66.  
    67. void feet2metres(float var1, float &var2)
    68. {
    69.  var2 = (0.3048*var1);
    70. }
    71. void metres2feet (float var1, float &var2)
    72. {
    73.     var2 = (1/0.3048)*var1;
    74. }
    75.  
    76. void displayresults(float var1, float var2, int option)
    77. {
    78.     cout.setf(ios::fixed);
    79.     cout.setf(ios::showpoint);
    80.     cout.precision(3);
    81.     switch (option)
    82.     {
    83.         case 1:
    84.             cout << var1 << " metres is: " << var2 << " feet.\n";
    85.             break;
    86.         case 2:
    87.             cout << var1 << " feet is: " << var2 << " metres.\n";
    88.             break;
    89.         default:
    90.             cout << "Invalid option.\n";
    91.     }
    92. }
     
  5. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    When posting source code, you should use the code /code tags so that your original formatting is preserved.

    Code ( (Unknown Language)):
    1. #include <iostream>
    2. using namespace std;
    3. void feet2metres(float, float&);
    4. void metres2feet(float, float&);
    5. void menu();
    6. void displayresults(float,float,int);
    7.  
    8. void main()
    9. {
    10.     menu();
    11. }
    12.  
    13. void menu()
    14.  
    15. {
    16.     float var1,var2;
    17.     int option;
    18.     char goagain;
    19.      cout << "Welcome to the distance converter.\n";
    20.     do
    21.     {
    22.         do
    23.         {
    24.             cout << "Please press 1 if you wish to convert from metres to feet.\n"
    25.                  << "Please press 2 if you wish to convert from feet to metres.\n";
    26.             cin >> option;
    27.         }
    28.         while (option != 1 && option != 2);
    29.         switch (option)
    30.          {
    31.             case 1:
    32.             do
    33.             {
    34.                 cout << "Please enter the distance in metres.\n";
    35.                 cin >> var1;
    36.                 metres2feet(var1,var2);
    37.                 displayresults(var1,var2,option);
    38.             }
    39.             while (var1 < 0);
    40.             break;
    41.             case 2:
    42.             do
    43.             {
    44.                cout << "Please enter the distance in feet.\n";
    45.                cin >> var1;
    46.                feet2metres(var1,var2);
    47.                displayresults(var1,var2,option);
    48.              }
    49.             while (var1 < 0);
    50.             break;
    51.          
    52.             default:
    53.             cout << "Invalid Option.\n";
    54.          }
    55.         do
    56.         {
    57.             cout << "Would you like to go again? (Y/N)\n";
    58.             cin >> goagain;
    59.         }
    60.         while (goagain != 'Y' && goagain !='y' && goagain != 'N' && goagain != 'n');
    61.        
    62.     }
    63.     while (goagain == 'Y' || goagain == 'y');
    64. }
    65.  
    66. void feet2metres(float var1, float &var2)
    67. {
    68.  var2 = (0.3048*var1);
    69. }
    70. void metres2feet (float var1, float &var2)
    71. {
    72.     var2 = (1/0.3048)*var1;
    73. }
    74.  
    75. void displayresults(float var1, float var2, int option)
    76. {
    77.     cout.setf(ios::fixed);
    78.     cout.setf(ios::showpoint);
    79.     cout.precision(3);
    80.     switch (option)
    81.     {
    82.         case 1:
    83.             cout << var1 << " metres is: " << var2 << " feet.\n";
    84.             break;
    85.         case 2:
    86.             cout << var1 << " feet is: " << var2 << " metres.\n";
    87.             break;
    88.         default:
    89.             cout << "Invalid option.\n";
    90.     }
    91. }
    My C is mighty rusty, but... you're testing for numeric values when the input may very well not be numeric. Try testing against char values instead.
     
  6. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    Thanks sgtwookie, sounds like a good idea. I definitely will insert code in a code window, its just that i didnt know how to do it before but i fiddled and now i do!
     
  7. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    Hey guys, i have done what was suggested and used a char for the menu options. I however want to take this to the next level and include error prevention for the float input. As it stands, when a letter is input for when the program asks for a value, the program goes into an infinite loop. How do i stop this from happening? Must strings be used? I tried doing this but am not really sure what I am doing.


    Code ( (Unknown Language)):
    1. /*  
    2.     Homework assignment
    3.     Gregory Kuhn
    4.     Student ID: 20027104
    5. */
    6.  
    7. #include <iostream>
    8. using namespace std;
    9. void feet2metres(float, float&);
    10. void metres2feet(float, float&);
    11. void menu();
    12. void displayresults(float,float,char);
    13.  
    14. void main()
    15. {
    16.     menu();
    17. }
    18.  
    19. void menu()
    20.  
    21. {
    22.     float var1,var2;
    23.     char option; //a char is used here as a error prevention technique to
    24.                  //stop the program from going into an infinite loop whenever a letter is pressed
    25.     char goagain;
    26.      cout << "Welcome to the distance converter.\n";
    27.     do
    28.     {
    29.         do
    30.         {
    31.             cout << "Please press 1 if you wish to convert from metres to feet.\n"
    32.                  << "Please press 2 if you wish to convert from feet to metres.\n";
    33.             cin >> option;
    34.         }
    35.         while (option != '1' && option != '2');
    36.         switch (option)
    37.          {
    38.             case '1':
    39.             do
    40.             {
    41.                 cout << "Please enter the distance in metres.\n";
    42.                 cin >> var1;
    43.                 metres2feet(var1,var2);
    44.                 displayresults(var1,var2,option);
    45.             }
    46.             while (var1 < 0);
    47.             break;
    48.             case '2':
    49.             do
    50.             {
    51.                cout << "Please enter the distance in feet.\n";
    52.                cin >> var1;
    53.                feet2metres(var1,var2);
    54.                displayresults(var1,var2,option);
    55.              }
    56.             while (var1 < 0);
    57.             break;
    58.          
    59.             default:
    60.             cout << "Invalid Option.\n";
    61.          }
    62.         do
    63.         {
    64.             cout << "Would you like to go again? (Y/N)\n";
    65.             cin >> goagain;
    66.         }
    67.         while (goagain != 'Y' && goagain !='y' && goagain != 'N' && goagain != 'n');
    68.        
    69.     }
    70.     while (goagain == 'Y' || goagain == 'y');
    71. }
    72.  
    73. void feet2metres(float var1, float &var2)
    74. {
    75.  var2 = (0.3048*var1);
    76. }
    77. void metres2feet (float var1, float &var2)
    78. {
    79.     var2 = (1/0.3048)*var1;
    80. }
    81.  
    82. void displayresults(float var1, float var2, char option)
    83. {
    84.     cout.setf(ios::fixed);
    85.     cout.setf(ios::showpoint);
    86.     cout.precision(3);
    87.     switch (option)
    88.     {
    89.         case '1':
    90.             cout << var1 << " metres is: " << var2 << " feet.\n";
    91.             break;
    92.         case '2':
    93.             cout << var1 << " feet is: " << var2 << " metres.\n";
    94.             break;
    95.         default:
    96.             cout << "Invalid option2.\n";
    97.     }
    98. }
     
  8. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    Greg,
    If you want to bullet-proof your input, you'll need to accept string input for your numeric values, and then parse it into numbers. You can use atof() or atof() in the standard library to do the conversion for you. Be sure to #include <stdlib.h>.The first function takes a string argument and returns a float. The second takes a string argument and returns a double. The idea is to ask for a, say, float value, and store it in a string, convert the string to a float value (atof()) or double value (atod()), check that the value entered is legitimate (each returns 0.0 for invalid data). If the value is reasonable, off you go; otherwise you can loop back and try again. It's a bit of extra work.

    OTOH, you have to assume some intelligence on the part of the user, and that if the user persists in entering weird data, your program at least has enough idiot-proofing to go down gracefully.
    Mark
     
  9. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    Hi mark,
    thats exactly what i guessed had to be done but had no idea on how to do it! Thanks for sending me in the right direction, i was trying to look up string name.compare but was getting nowhere haha.
     
Loading...