-6

Currently, I want my code to accept lowercase or uppercase a, b, c, or u as a valid entry from the user. However, anytime I enter the characters as lowercase, they respond with the error message and continue the loop until it is put in uppercase. I am new to C++, so I might be using toupper wrong.

#include <iostream>

using namespace std;

int main()
{
    bool custGBTypeValid = false;
    bool custPlnTypeValid = false;
    char custPlanType = toupper('Z');
    int custUsedData = 1;

    cout << "Hello, welcome to AT&T wireless. We're here to help you decide if your current plan is what's right for you." << endl;
    cout << "Here are our plans:" << endl;
    cout << "Plan A: For $25 per month 0GB are provided. Data is $10 per GB." << endl;
    cout << "Plan B: For $45 per month 2GB are provided." << endl;
    cout << "Plan C: For $80 per month 6GB are provided." << endl;
    cout << "Plan Unlimited: Unlimited data for $100 per month." << endl;

    while (custPlnTypeValid == false)
    {
        cout << "What type of plan are you on? (Please answer with A, B, C, or U): ";
        cin >> custPlanType;
        if (custPlanType == toupper('A') || custPlanType == toupper('B') || custPlanType == toupper('C') || custPlanType == toupper('U'))
            custPlnTypeValid = true;
        else
            cout << "ERROR: Incorrect data type entered." << endl;
    }
}

How would I get it to accept lowercase too? I have also tried changing each in the if statement to custPlanType == toupper('a') etc. and toupper(custPlanType == 'A') but this doesn't work either. The latter works if the characters within the code are lowercase, but then refuses to work with uppercase characters.

  • 6
    Think about what `toupper('A')` actually achieves. – paddy Nov 02 '21 at 00:33
  • 1
    Your code does exactly what you wrote. It doesn't do what you want, because you didn't write what you wanted to write. – gnasher729 Nov 02 '21 at 00:37
  • 1
    `toupper(custPlanType == 'A')` puts the close bracket in the wrong spot and tries to convert `custPlanType == 'A'`, a boolean, into upper case. – user4581301 Nov 02 '21 at 00:40
  • |toupper(custPkanType)` will change the case of the selected character to upper and THEN compare it with A, B et cetera. `custPlanType == 'A'` will result in either 0 or 1. Then toupper(0) or toupper(1) will give whatever that will probably be meaningless. – Tarik Nov 02 '21 at 01:59
  • 1
    Read your code aloud, then [explain to your rubber duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging) why it is correct. To start your explanation, you might want to read the code again, but this time with some interpretations, such as reading `custPlanType == toupper('A')` as *"the entry from the user... is equal to... the uppercase version of uppercase 'A'"*. Then you can explain how when the user enters a lowercase 'a', it will be equal to an uppercase uppercase 'A'??? (A triple question mark indicates success in rubber duck debugging.) – JaMiT Nov 02 '21 at 03:53

1 Answers1

2

It should be:

if (toupper(custPlanType) == 'A' ....) 
Tarik
  • 10,810
  • 2
  • 26
  • 40