Frage

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;
}
War es hilfreich?

Lösung 2

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.

Andere Tipps

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.

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

         invColorCode = "25" << endl;

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top