Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
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.
It should be:
if (toupper(custPlanType) == 'A' ....)
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Beginner here so I'm sorry if I made nooby mistakes
I assign di to be the array myworld[] depending the the user input it'll assign the di into the appropriate array position, but for some reason the if statement keep outputting "make" instead of "change" when my input is 'c'
I tried to remove else if and put if for all of them, or got rid of else if and just use else.
#include <iostream>
using namespace std;
int main() {
char di;
char myword[] = {'d','m','s' ,'c'};
do {
cout << "Make a selection:" << endl;
cout << "d - insert 1$ bill" << endl;
cout << "m - view menu" << endl;
cout << "s - select an item" << endl;
cout << "c - get change" << endl;
cin >> di;
if (di == 'd')
di = myword[0];
else if (di == 'c')
di = myword[3];
}while (!myword);
if (myword[0])
cout << "make";
else if (myword[3])
cout << "change";
return 0;
}
Probably you forgot to make a comparison inside if statement. For now you are just saying if('d'!= 0) which is always true. Perhaps you tried to make if(di == myword[0]). The same applies for the else if statement.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am working on a homework to where it asks the user to type in an abbreviation of states and when it gets the correct input it displays the full name of the state. I have the code below but for some reason even when I type the correct abbreviation, for example TN, I still get an Invalid Entry!. It has to be a simple mistake I am making in the while loop condition or I am missing something but I cannot seem to figure it out.. Help would be appreciated!!
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main() {
string state;
string KY, OH, IN, TN, WV;
cout << "Question 1:" << endl;
cout << "Enter state abbreviation (KY, OH, IN, TN, WV): ";
cin >> state;
while (state != KY && state != OH && state != IN && state != TN && state != WV) {
cout << "Invalid Entry!";
cout << "\nEnter state abbreviation (KY, OH, IN, TN, WV): ";
cin >> state;
}
if (state == KY)
cout << "Kentucky";
if (state == OH)
cout << "Ohio";
if (state == IN)
cout << "Indiana";
if (state == TN)
cout << "Tennessee";
if (state == WV)
cout << "West Virgnia";
system("Pause");
return 0;
}
If you don't specify the contents of an an std::string, it will be initialized as an empty string, so your OH, KY (etc.) are all empty strings. Presumably you want something more like this:
string KY = "KY", OH = "OH", IN = "IN", TN = "TN", WV = "WV";
That should at least be a move in the right direction.
You have defined the string variable KY, OH etc. these are different from strings "KY", "OH". You are comparing with the uninitialize string variables.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
#include <iostream>
#include <string>
using namespace std;
int main ()
{
do
{
string name, answer;
cout << "Welcome to the prime number checker! Please enter your name: ";
getline (cin, name);
int a;
cout << "\nHello " << name;
cout << "\nPlease enter an integer: ";
cin >> a;
cin.sync();
if (a == 2)
{
cout << "\nThis is a prime number" << endl;
}
else
{
for (int b = 2; b < a; b++)
{
if (a % b == 0)
{
cout << "This number is not prime number" << endl;
break;
}
else
{
cout << "This number is a prime number." << endl;
break;
}
}
}
cout << "Do you want to do this again (Yes or No)?";
getline (cin, answer);
}
while (answer == "yes" || answer == "YES" || answer == "Yes"); //Not declared in this scope
return 0;
}
You declared answer within the do block. But then try to reference answer outside of that scope block.
Declare answer at the top of main instead of in the do block.
You need to move the declaration of answer outside the loop:
string answer;
do {
string name;
...
} while (answer == "yes" || answer == "YES" || answer == "Yes");
If you declare it inside the loop, it no longer exists by the time the while clause is evaluated.
As other people said, the "answer" variable only exists inside the loop - it isn't accessible from outside it.
One other recommendation: rather than checking every possible permutation of capitalization just cast the whole string to lowercase. (You actually missed several - there are 6 total because each position could have one of 2 possible values. Presumably something like "YeS", for example, should still be accepted as "yes").
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm new to programming and this is my code.
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
char name[50];
cout << "Please enter your name : " << endl;
cin >> name;
if (name[0] = 'M')
{
cout << "Your initial name is M" << endl;
}
else
{
cout << "Your initial name is not M" << endl;
}
system("pause");
return 0;
}
When I run my code, I typed "Mark" in the window and the program said "Your initial name is M".That works fine
but when I type "John" in the window, the program still said "Your initial name is M" instead of "Your initial name is not M"
and I am wondering why.Are there something missing in my code? Thanks for your time.
if (name[0] = 'M')
should have to be
if (name[0] == 'M')
= is used as an assignment operator. it will assign M to name[0].
Use == to compare value.
= assign value from right hand side to left hand side.
== compare value of right hand side with left hand side.
its '==' and not '=' in your if statement. '=' is for simple assignment operator and == is for comparison operator
Change the assignment operator = in your if statement to an equal to operator ==
if (name[0] == 'M')
{
cout << "Your initial name is M" << endl;
}
else
{
cout << "Your initial name is not M" << endl;
}
if (name[0] = 'M')
In C and C++, = is for assignment.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a function (below) that checks the user's first name for invalid characters and it works fine.
while(run)
{
size_t positionFirstName = userFirstName.find_first_of(invalidCharacter, 0, sizeof(invalidCharacter));
if (positionFirstName != string::npos)
{
cout << "Please only use letters. Please re-enter your first name." << endl;
cin >> userFirstName;
}
else
{
run = false;
}
}
I also want to check that the user's first name is not shorter than 3 characters.
I have tried a few times, and can get the program to run the first function, but if I put in another function to check name length, it seems to skip it. Any ideas?
Here's a slightly adjusted way to do it:
cout << "Please enter your first name." << endl;
while( cin >> userFirstName )
{
size_t positionFirstName = userFirstName.find_first_of(invalidCharacter, 0, sizeof(invalidCharacter));
if (positionFirstName != string::npos)
{
cout << "Please only use letters.";
}
else if( userFirstName.size() < 3 )
{
cout << "Name must be at least 3 characters long."
}
else {
break;
}
cout << " Please re-enter your first name." << endl;
}
Note that I've avoided repetition, but printing only the errors and handling the input in one place.