-1

I just started to learn C++ 2 days ago. I am just learning the basic syntax now. I just learned if statements, and i was trying to practice them. My problem is that at the "if(temp >= 60 && temp <= 70)" and "if(temp >= 75)" parts of the code, I get the error no match for 'operator' error. I tried looking this up. people with similar problems are using code that i simply cannot follow. I tried implementing their solutions, but i get even more error messages.If someone could please help, i would appreciate it, because i don't know anyone in real like that knows C++ or even anyone that codes. Thanks in advance.Here is the code. Sorry if its messy:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    //Get seed color
    string seedColor = "";
    cout << "Enter seed color (Red or Blue): \n";
    cin >> seedColor;
    //Get Temperature
    string temp = "";
    cout << "Enter the temperature (F): \n";
    cin >> temp;
    //Get soil moisture
    string soilMoisture = "";
    cout <<"Enter the soil moisture (wet or dry): \n";
    cin >> soilMoisture;
    //if red seed
    if(seedColor == "red")
    {
        //if temp is >= 75
        if(temp >= 75)
        {
            //if soil is wet
            if(soilMoisture == "wet")
            {
                //get sunflower
                cout << "A sunflower grew!";
            }

            //if soil is dry
            else
            {
                //get dandelion
                cout << "A dandelion grew!";
            }
        }
        else
        {
        //otherwise

            //get mushroom
            cout << "A mushroom grew!";
        }
    }
    //if blue seed
    if(seedColor == "blue")
    {
        //if temp is between 60 and 70
        if(temp >= 60 && temp <= 70)

        {
            //if soil is wet
        if(soilMoisture == "wet")
        {
            //get dandelion
            cout << "You grew a dandelion!";
        }
        else
        {
            //if soil is dry
            cout << "You grew a sunflower!";
                //get sunflower
        }
        }
        else
        {
        //Otherwise
            cout<< "You grew a mushroom!";
            //Mushroom
        }

} }

2 Answers2

2

This line in your code causes the error:

if(temp >= 75)

temp is of type std::string and 75 is considered a int value. The compiler is right, there's no intrinsic operator

bool operator>=(const std::string&,int);

declared.


You probably want to change your definition of the temp variable to

int temp = 0;

or

double temp = 0.0;

Also you had some small formatting problems. See the fixed code.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • If you are going to suggest using a floating point data type then you should also make them aware that [floating point math is broken](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) – NathanOliver May 19 '16 at 17:57
1

You cannot implicitly compare a std::string with an int, if you keep temp as an int then use std::stoi(temp) to convert it to an int and store this value somewhere. Or if you change it to a double as the other answer suggests then use std::stod(temp) to convert to a floating point number. E.g:

std::string temp_str = "";
std::cin >> temp_str;
int temp = std::stoi(temp_str);
// then do comparions/arithmetic with temp
sjrowlinson
  • 3,297
  • 1
  • 18
  • 35