0

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. enter image description here

5
  • You are assigning sStoreInput01 = 0 were you tryting to compare sStoreInput01 == "0"? Commented Jun 20, 2018 at 2:06
  • I want sStoreInput01 to be defined by the user and interpreted by the program for comparison, yes. I tried doubling the equals sign in the IF process but the program didn't like me doing that, it gave me an error specific to using "==" EDIT-EDIT: I changed all "=" to "==" in the IF process and all the errors went away except the one objecting to using "==" in the first place. It says "no operator "==" matches these operands" Commented Jun 20, 2018 at 2:07
  • Were you comparing sStoreInput01 == 0? This won't compile because you can't compare a string to an int, sStoreInput01 == "0" should work. Commented Jun 20, 2018 at 2:12
  • That's what I was thinking but I didn't realize doing that solved anything until I put quotations on everything. Thank you! Commented Jun 20, 2018 at 2:15
  • Welcome to Stack Overflow! Your image of text isn't very helpful. It can't be read aloud or copied into an editor, and it doesn't index very well, meaning that other users with the same problem are less likely to find the answer here. Please edit your post to incorporate the relevant text directly (preferably using copy+paste to avoid transcription errors). Commented Jun 20, 2018 at 10:39

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Changing all = to == and putting quotations on everything eliminated all errors. Thank you.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.