-8

As the title suggests, I have a working program for when a user inputs A B or C. My professor has said that we have not gone over repetition yet so we just need to put in a line of code that returns something like "Please enter either A B or C" when the user enters any other character but I am having trouble figuring out how to do this. Any help will be very appreciated. I'll post a file of the code I have now.

https://docs.google.com/document/d/1EVxLPtOsBbdmCCt0LwUDYkqgySg8bSm3w_d_CAcGW6g/edit?usp=sharing

Neil N
  • 1
  • 1
  • Have you considered putting all the `if-else` inside a `do{ .. }while (0);` loop and adding an `else` part where you through a message and a `continue`? Else return some return code, say, `-1` in the else part, and return `0` for the other a, b, c option. Check in the calling function if the value is `-1` was returned and take decision. – phoxis May 20 '18 at 17:01
  • I strongly recommend against posting images of code. They are much harder to search for than text and few if any compilers have reliable OCR built in. Remember that your question is being added to a database to help future programmers when they encounter a similar problem, so search-ability is a must. If you have problem with code, duplicating your error is often required, so providing code that can be compiled is a must. – user4581301 May 20 '18 at 17:06
  • @phoxis I'm sorry - what you're saying will probably work but I have no idea what that means, literally my first week of C++ with an online course. The book isn't really covering this type of situation at least that I can find. Thank you for the help though, appreciate it – Neil N May 20 '18 at 17:09
  • In this case I would definitely recommend to go through a text book for C and/or C++, as I believe this will be covered in chapters mentioning if-else, and functions. Also, please post the relevant code in plain text, that helps everyone. – phoxis May 20 '18 at 17:12
  • In that case you either [need a better book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or to read more carefully. `else` and `switch` will be covered early in any text that is not outright fraudulent. – user4581301 May 20 '18 at 17:12
  • I tried to help, but my IDE would not accept pasting of your code, from the screen snapshot. I believe if you posted **text** of your code, I could more easily paste it into my IDE and debug your code for you. – Thomas Matthews May 20 '18 at 17:14
  • 2
    Don't link to code. Put a [mcve] *in* your question. – Jesper Juhl May 20 '18 at 17:46
  • You just put an `else` at the end and output the message... I really don't get why you can't figure this out. – eesiraed May 21 '18 at 02:25

3 Answers3

0

Note: OP did not want a complete refactoring of his code. So this is the minimal-intervention solution.

Edit your if statements to:

if(plan=='A'){
    //...
} else if(plan=='B'){
    //...
} else if(plan=='C'){
    //...
} else {
    //handle the error here.
    cout << "Wrong input" << endl;
}
Attersson
  • 4,755
  • 1
  • 15
  • 29
0

EDIT: It'd be even better to use a std::string as a buffer to prevent receiving multiple errors from this if the user inputs more than one char.

The best way to handle this would probably be a simple do... while nested switch statement:

#include <string>


bool repeat = true;
do {
std::string buffer;
cout << "Which plan do you want to use?" << endl;
cin >> buffer;

// check if the user entered only one character
if (buffer.length() > 1) {
    cout << "Invalid Input" << endl;
    continue;
}

plan = buffer[0];
switch(plan) {
    case 'A':
        // do things
        repeat = false;
        break;
    case 'B':
        // do things
        repeat = false;
        break;
    case 'C':
        // do things
        repeat = false;
        break;
    default:
        cout << "Invalid input, please try again." << endl;
        break;
} while (repeat);

This keeps asking the user for which plan they want to use, until you receive valid input.

L. Kue
  • 473
  • 5
  • 19
0

Here is a common stencil for processing menus:

bool invalid_selection = true;
while (invalid_selection)
{
  // Output the menu with choices
  // ...
  char choice;
  std::cin >> choice;
  choice = std::toupper(choice);
  switch (choice)
  {
     case 'A':  
       do_something;
       break;
     // ... other choices ...
     default:
       std::cout << "Invalid choice.";
  }
  if (choice == quit_character)
  {
      break; // exit out of the loop
  }
}

There are many other alternatives. For example, one is a do-while loop.

If you don't know about switch, use your if-else-if ladder. The final else clause is equivalent to the default case.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154