Reputation: 61
I am trying to set up a storefront simulation in the console for a school project. The user goes through some prompts and then has the opportunity to "buy supplies", which is what this module will do when it's done.
Right now, I am getting error messages about datatypes. Visual Studio is advising that I need my variable to be a boolean, which I don't know how to interpret in this context. I think it is easy for a person to see what I am trying to do.
Can you please advise me on how I can fix this ?
The outputs for each option are placeholders; I intend the user to be funneled through more menus specific to each of the 5 choices.
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <limits>
int main()
{
int nNumCup = 0, nNumLem = 0, nNumSug = 0, nNumIce = 0;
std::string sStoreInput01, A, B, C, D;
std::cout << "You currently have \n"
<< nNumCup << " cups, " << nNumLem << " lemons, " << nNumSug << " cups of sugar and " << nNumIce << " icecubes. \n"
"If you want to purchase more supplies, you can choose from \n"
"\n"
"A) cups \n"
"B) lemons \n"
"C) cups of sugar \n"
"D) icecubes \n"
"\n"
"Please make your selection. Otherwise, type 0 to finish shopping. \n";
getline(std::cin, sStoreInput01);
//Everything above this note is fine; below code is problematic:
if (sStoreInput01 = 0) {
std::cout << "return (this feature isn't set up yet)";
}
else if (sStoreInput01 = A) {
std::cout << "Go to Cups";
}
else if (sStoreInput01 = B) {
std::cout << "Go to Lemons";
}
else if (sStoreInput01 = C) {
std::cout << "Go to Sugar";
}
else if (sStoreInput01 = D) {
std::cout << "Go to Ice";
}
else
std::cout << "error";
return 0;
}
I didn't define "0" as a string because that caused errors and putting it in quotes didn't help.
Errors below, sans one which isn't shown but it's another thing about booleans.
Upvotes: 0
Views: 113
Reputation: 26
Well, "=" is the assignment operator, "==" is comparison one. Basically, you're assigning value 0 to your string, and the result of this operation isn't a Boolean, as needed by if conditional. Change "=" to "==" and you should be fine.
Edit: sorry, didn't see your comment. Try using sStoreInput01.compare("A"), for instance. See if it works.
Upvotes: 1
Reputation: 106
A couple of things to note. '=' is the assignment operator and you want '==' the equality operator.
if(sStoreInput01==0)
This will still throw an error as sStoreInput01 is a string and 0 is an int but you're closer.
For the if clauses it looks like you want something like:
if(sStoreInput01=="A")
to check for the input.
Visual studio is telling you that you need your variable to be a boolean because an if statement demands a boolean expression. Assignment (sStoreInput01=0) is actually an expression with the same type as the variable being assigned to. Hope that helps a bit.
Upvotes: 3