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.
Related
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' ....)
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 2 years ago.
Improve this question
This a simple number guessing game. If you guessed the number right, it outputs "You win!!!", but if the number of tries (numberofguesses) is exceeded, it should output "You lose", but it is showing "You win!!!" even though I checked the values of numberofguesses, secretnum and guess after the while loop. Answer in simple words, I'm a beginner.
#include <iostream>
using namespace std;
int main()
{
int secretnum = 7;
int guess = 0;
int numberofguesses = 3;
while (secretnum != guess && numberofguesses != 0) {
cout << "enter your guess: ";
cin >> guess;
--numberofguesses;
}
if (secretnum = guess && numberofguesses != 0) {
cout << "You win!!!";
}
else
{
cout << "You lose";
}
}
You have mistaken the assignment operator = with the comparison operator ==.
In this line here:
if (secretnum = guess && numberofguesses != 0)
cout << "You win!!!";
Change it to:
if (secretnum == guess && numberofguesses != 0) {
cout << "You win!!!";
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
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.
Improve this question
string word;
int l,eFound,xFound;
l = word.size();
cout <<"Enter a word: ";
cin >> word;
for (l>0 ; word.at(l)!='x' || word.at(l)!='e'; l--)
if (word.at(l) == 'e'){
eFound = true;
}
else if (word.at(l) == 'x'){
xFound = true;
}
if (eFound == true && xFound == true){
cout << "Your word, "<<word<<", contains the character 'e'"<<"\n";
cout << "Your word, "<<word<<", contains the character 'x'";
}
if (eFound == true && xFound != true){
cout << "Your word, "<<word<<", contains the character 'e'";
}
if (xFound == true && eFound != true){
cout << "Your word, "<<word<<", contains the character 'x'";
}
I'm not sure what is going on I'm trying to use a for loop to detect either e or x in a input of some word. I've clicked on other pages with the same error but they have different codes and I don't really understand what is explained. So what is causing this error? I'm 2 weeks into my first programming class, sorry if I'm asking a dumb question.
The issue is that indexing of std::string starts from zero. Not from 1. So, word.at(l) will crash if l = word.size();.
You should change the statement to: l = word.size() - 1;.
Also, Change your loop condition to for (; l >= 0 ; l--)
Suggestion:
Please go for library functions:
Like this:
#include <iostream>
#include <string>
using namespace std;
int main() {
string word;
cout <<"Enter a word: ";
cin >> word;
bool eFound = word.find('e') != string::npos;
bool xFound = word.find('x') != string::npos;
if (eFound) {
cout << "Your word, "<<word<<", contains the character 'e'" << "\n";
}
if (xFound) {
cout << "Your word, "<<word<<", contains the character 'x'" << "\n";
}
return 0;
}
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.