Help with my code PLEASE!

this is what the code does:
1. ask user for input (seeds color red or blue)
2. check the temp
for blue seed: if temp from 60-70, it grows to flower, else mushroom
for red seed: if temp is >= 75, it grows to flower,else mushroom

3. check for soil
for blue seed: if soil is try--->grows into sunflower
if soil is wet--->grows into dendelion

for red seed: the opposite from blue seed.

here is my code:


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

int main()
{
string color="";
cout<<"what is the color of the seed? please either enter blue or red!: \n";
cin >> color;

int temp;
cout<<"what is the temperture? :\n";
cin >> temp;

string soil="";
cout<<"what kind of the soil? please either enter wet or dry! :\n";
cin >> soil;

if (color == "red")
{
if (temp >= 75)
{
if (soil=="wet")
{
cout <<"you get yourself a sunfolower :\n";
}
if (soil=="dry")
{
cout <<"you get yourself a dendelion :\n";
}
}
else
{
cout <<"Shit, you have a mushroom now! :\n";
}
}

if (color=="blue")
{

if (temp >= 60 || temp <= 70)
{
if (soil=="wet")
{
cout <<"you get yourself a dendelion :\n";
}
if(soil=="dry")
{
cout <<"you get yourself a sunfolower :\n";
}
else{
cout <<"Shit, you have a mushroom now! :\n";
}
}
}

}

when I run the code, it stops executing after asking the 3rd question, why? Help please.



stops executing ? what does that mean ?
does it close down without output ?

and there is many wrong in this code I think

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
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string color="";
	cout<<"what is the color of the seed? please either enter blue or red!: \n";
	cin >> color;
	
	int temp;
	cout<<"what is the temperture? :\n";
	cin >> temp;
	
	string soil="";
	cout<<"what kind of the soil? please either enter wet or dry! :\n";
	cin >> soil;	

	if (color == "red")
	{
		if (temp >= 75)
		{
			if (soil=="wet")
			{
				cout <<"you get yourself a sunfolower :\n";
			}
			else if (soil=="dry")
			{
				cout <<"you get yourself a dendelion :\n";
			}
		}
		else
		{
			cout <<"Shit, you have a mushroom now! :\n";
		}
	}
	
	else if (color=="blue")
	{
		if (temp >= 60 && temp <= 70)
		{
			if (soil=="wet")
			{
				cout <<"you get yourself a dendelion :\n";
			}
			else if(soil=="dry")
			{
				cout <<"you get yourself a sunfolower :\n";
			}
		}
		else{
			cout <<"Shit, you have a mushroom now! :\n";
		}
	
	}
	
	return 0;

}


or see it executing here
http://ideone.com/fbTeTC

I will point out a few wrong

you shouldn't check of what's impossible
for example
1
2
3
4
5
6
if( x == 5 ){
   cout << "It's five" << endl;
}
if( x == 7 ){
   cout << "It's seven" << endl;
}


if the first condition is true then the second condition isn't

so to speed things up
you should use else if
1
2
3
4
5
6
7
if( x == 5 ){
   cout << "It's five" << endl;
}
else if( x == 7 ){
   cout << "It's seven" << endl;
}


other than to speed things up, it's also for the else condition
for example
1
2
3
4
5
6
7
8
9
if( input == "yes"){
   cout << "Accepted" << endl;
}
else if( input == "no"){
   cout << "Why not ?" << endl;
}
else {
   cout << "You didn't answer the question in a way I can understand" << endl;
}


this is just a dumb example but try to get it


then please check if your condition is always true no matter what is the input
 
if( temp >= 60 || temp <= 70 ){


I take the example from your code

just try , whatever the temp is it's always true
so becareful

and also one more thing
use indentation ( space or tab ) to make your code easier to read for yourself at least
it's not hard to put the curly brace in the wrong place


Last edited on
Thank you so much!
Topic archived. No new replies allowed.