0

First things first: I know this code is overlong and could be shortened quite a bit. However, I don't want help with how to shorten it, I am just trying to understand some basics and my problem right now is with operators and storing values. As you can probably see from the code, I am trying to use a bunch of if statements to store specific values in variables and then display those values together in a string at the end. The compiler does not like my code and is giving me a bunch of operator related errors.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string type = "";
    string color = "";
    string invTypeCode = "";
    string invColorCode = "";
    string fullCode = "";

    cout << "Use this program to determine inventory code of furniture.";
    cout << "Enter type of furniture: ";
    cin >> type;

    if (type.length() == 1)
    {
        if (type == "1" or "2")
        {
         if (type == "1")
         { 
              invTypeCode = "T47" << endl;
              }
         if (type == "2")
         { 
              invTypeCode = "C47" << endl;
              }
              else 
              cout << "Invalid inventory code." << endl;
    }}

    else
         cout << "Invalid inventory code." << endl;

    cout << "Enter color code of furniture: ";
    cin >> color;

    if (color.length() == 1)
    {
        if (color == "1" or "2" or "3")
        {
         if (color == "1")
         { 
              invColorCode = "41" << endl;
              }
         if (type == "2")
         { 
              invColorCode = "25" << endl;
              }
         if (type == "3")
         { 
              invColorCode = "30" << endl;
              }
              else 
              cout << "Invalid inventory code." << endl;
    }}

    else
         cout << "Invalid inventory code." << endl;

    fullCode = invTypeCode + invColorCode;
    cout << fullcode << endl;

    system("pause"); 
    return 0;
}
2
  • Write type == "1" || type == "2" instead of type == "1" or "2", etc. Commented Apr 23, 2013 at 19:36
  • Research std::stringstream for using operator<< with strings. Commented Apr 23, 2013 at 19:53

3 Answers 3

5
if (color == "1" or "2" or "3")

should be

if (color == "1" or color == "2" or color == "3")

or if you prefer the normal style then

if (color == "1" || color == "2" || color == "3")

Operators || and or are the same, but || is the older version, so it's the one that most people use.

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

Comments

3

It looks like you're not using input and output streams properly. For example:

         invTypeCode = "T47" << endl;

Normally, if you are using endl and <<, the lhs side for the << operator is an std::ostream. In this case the lhs is a string, which is why the compiler is complaining. In this case, what I think you really want is to replace "T47" with "T47\n", and remove "<< endl".

You've also got the case wrong on your very last reference to "fullcode"; it needs to be "fullCode", with an upper case 'C'. C++ is case sensitive.

Comments

1

Also, the statements like this one won't work:

         invColorCode = "25" << endl;

Not really sure what you're trying to accomplish with the endl.

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.