-2

The goal is to ask for 2 colors then output the appropriate secondary color(purple,green,orange), I get an error telling me I can't convert standard strings to shorts.

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
    short result,col_1,col_2,blue,red,yellow;

    const string str1="Purple.",
    str2="Orange.",
    str3="Green.",
    str4="Incorrect, please re-enter.";

    cout<<"Please enter only 2 of the following colors (yellow,blue,red): ";
    cin>>col_1>>col_2;


    if((col_1==red||blue)&&(col_2==red||blue))
    {result=str1;
    }else{
    result=str4;
}
    if((col_1==red||yellow)&&(col_2==red||yellow))
    {result=str2;
    }else{
    result=str4;
}
    if((col_1==yellow||blue)&&(col_2==yellow||blue))
    {result=str3;
    }else{
    result=str4;
}
    cout<<"Your combined color is: "<<result<<endl;
    //for example red and blue should output : Purple.
    return 0;
}
  • Possible duplicate of [Can you use 2 or more OR conditions in an if statement?](https://stackoverflow.com/questions/8781447/can-you-use-2-or-more-or-conditions-in-an-if-statement) – phuclv Mar 03 '19 at 03:10
  • 2
    `col_1==red||blue` is always true. Also please indent your code properly and add spaces around operators to make it readable – phuclv Mar 03 '19 at 03:10
  • Also you're performing a case-sensitive string comparison (so `"red" != "Red"`) . You might want to use `boost:iequals` for case-insensitive comparison if you're only using ASCII-range characters or see this QA: https://stackoverflow.com/questions/11635/case-insensitive-string-comparison-in-c – Dai Mar 03 '19 at 03:11

3 Answers3

1

The error message you saw describes the problem:

short result;
const string str1="Purple.";

[...]

result=str1;

result is a short, and str1 is a std::string. There is no built-in way for C++ to convert a std::string into a short, so the compiler generates that error when you try to make that assignment.

Perhaps you meant to declare result as a string instead?

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
1

Even if you "fix" the bug about data types (short and string), your algorithm is not entirely correct and it won't procedure the right answer. Take these lines of code as an example:

    if((col_1==red||blue)&&(col_2==red||blue))
    {result=str1; // which is "purple"
    }else{
    result=str4;
}

or, re-written probably:

if ((col_1 == "red" || col_1 == "blue") && (col_2 == "red" || col_2 == "blue"))
{
    result = "purple";
}
else
{
    result = str4;
} 

What if col_1 = "red" and col_2 = "red"? According to this code, the result would be purple (which is unexpected). You should modify it a bit:

if ((col_1 == "red" && col_2 == "blue") || (col_1 == "blue" && col_2 == "red"))
{
    result = "purple"
}
else
{
    result = str4
}

So, what can you learn from my re-written code?

  1. Think about your algorithm carefully. Make sure it is correct before you get to coding.

  2. Find more information about the if-statement and data types. You basically cannot compare or assign an integer (or a short) to a string.

  3. Learn more about how to declare variables: only declare what you need, with the appropriate data type. You want to output the "color", or theoretically a string, so your result variable should also be a string. As you are only able to compare strings to strings, if you want to compare col_1 (or col_2) to another color (red, blue, yellow), these colors must be declared as strings too. It makes no sense that you're declaring a short variable name red and compare it to a string because you are comparing an unassigned integer to a string. The correct way to do this is either, you don't need any new variable to "store" this information, just put the string to want to compare in double-quote, or to define a variable like this:

    string red = "red"

(which I think is completely unnecessary, but whatever)

  1. Make sure that all of your input is in lowercase, or you'll have to handle something like "REd" and "reD" both are equals to the color "red". Either compare every single possible way of writing the word "red" or just convert all the words back to lowercase (or uppercase) before the comparison.

  2. This is just a minor thing, but if you want others to read your code and analyze it for you, make sure that your code is readable and is indented probably.

These are just the basics of programming, so you should practice harder so you won't encounter those bugs in the future. Good luck!

JamieNguyen
  • 291
  • 3
  • 15
0

short is number type, your result is short but your str1, str2, str3, str4 are string, in C++ there is no built-in way to covert string to short (or any to number data type). In your situation, you can declare the result as a string. And don't convert a string to a number if you don't have to!

Trí Phan
  • 1,123
  • 2
  • 15
  • 33