Fruit Inspector

The user will input a string representing a color (either "yellow" or "red"), followed by a second string representing a shape ("circular" or "oval").

If the user says "yellow" and "circular" or "red" and "circular" print out the word "apple"

If the user says "yellow" and "oval" or "red" and "oval" print out the word "mango"



I am really lost and need help to figure why my code is not working.




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

int color;
cin >> color;



string color = "color";
string shape = "shape";

getline( cin, color);
getline( cin, shape);

if (color = "yellow")
{
if (shape = "circular")
{
cout << "apple" << endl;
}

else if (shape = "oval")
{
cout << "mango" << endl;
}
}

else if (color = "red")
{
if (shape = "circular")
{
cout << "apple" << endl;
}

else if (shape = "oval")
{
cout << "mango" << endl;
}
}


system ("pause");
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
using namespace std;

int color; // Color should be a string because you aren't looking for integers.
cin >> color;


// You want the user to input the color and the shape, therefore you should not initialize it
string color = "color"; // You can instead initialize it as: string color = "";
string shape = "shape";// You can instead initialize it as: string shape = "";

getline( cin, color);
getline( cin, shape);

// You do no use single '=' rather you use '==' because you want to compare
// the user's input for color rather than setting color to "yellow"
// Learn the logical operator '&&' and '||'. This allows you to use only a single if-else statement
if (color = "yellow")
{ 
if (shape = "circular")
{
cout << "apple" << endl;
}

else if (shape = "oval")
{
cout << "mango" << endl;
}
}

else if (color = "red")
{
if (shape = "circular")
{
cout << "apple" << endl;
}

else if (shape = "oval")
{
cout << "mango" << endl;
}
}


system ("pause");
return 0;
}
Last edited on
Hopefully this will help you start:
http://coliru.stacked-crooked.com/a/24fb573e3f8e6910

You will find it useful to compile with all warnings enabled.
You will find it useful to compile your code often, to fix errors as they arise.
Ideally, except while you're in the middle of a single particular edit, your code should be ready to compile and test. Source control can help with this.
Topic archived. No new replies allowed.