simple noob problem with variables

Hi, I have seen variables such as 'int' for non decimal numbers and 'char' for letters... but how would I go about declaring cout text?

Thanks.

1
2
3
4
5
6
7
8
9
if(answer >= 10)
{
n = cout << " es.";
}
else 
{
n = ".";
}
	cout << "You need " << answer << " box" << n << endl;
Last edited on
1
2
          string n=" es.";
          cout << n << '\n';
thanks for the response, but I not 100 on how I would implement it.

Here is the whole code
I declared 'n' as 'char', but I don't know if that is correct or not.

regards
-James.




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
#include <iostream>
using namespace std;

int main()
{
	int apples;
	int oranges;
	int sum;
	
	int answer;
	char n;
	
	
cout << "This should hopefully work out how many boxes will be needed to hold apples and oranges." << endl;
cout << "How many apples do you have? ";
cin >> apples;
cout << endl << apples << " apples." << endl;
cout << endl << "How many oranges do you have? ";
cin >> oranges;
cout << endl << oranges << " oranges" << endl;



sum = apples + oranges;

answer = sum / 10+1;

if(answer >= 10)
{
string n = " es.";
}
else 
{
string n = ".";
}
	cout << "you need " << answer << " box" << n << endl;
	cout << n << '\n';
		
	


return 0;
}
Something like this? I don't really understand the question. (:
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
	int oranges = 1;
	int apples = 3;

	string text = "Orange";
	string text_2 = "Apple";

	if( oranges > 1 )
		text += "s.";
	else
		text += ".";

	if( apples > 1 )
		text_2 += "s.";
	else
		text_2 += ".";

	cout << "You have " << oranges << " " << text << '\n';
	cout << "You have " << apples << " " << text_2 << '\n';
   
   return 0;
}


Check out this. This is how it will run.
Output will be displayed under the code window.

http://liveworkspace.org/code/SGOAp$1
Last edited on
You declared n as a char but never used it. You also declared n as a string twice, locally.

1
2
3
4
5
6
7
8
if(answer >= 10)
{
string n = " es.";
}
else 
{
string n = ".";
}


This code is redundant because you declared n locally so it gets destroyed as soon as the code block ends. So basically, the string n only exists for one line of code.
Thanks for the help guys! 2 days later and it's finally solved.

Thanks for the string posts, I declared 'n' as a string, but I didn't not put in the header #include<string>, because int, char, bound, etc. were part of iostream, I thought string would be too.

The whole 'n' thing was about changing the plural on box(es) if it needs more than 1 box to make it correct English.

Thanks again guys, this has been a good learning experience

Here is the code if you guys want to try it out.

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
#include <iostream>
#include <string>

using namespace std;

int main()
{
	int apples;
	int oranges;
	int sum;
	
	int answer;
	string n;
	
	
cout << "This should hopefully work out how many boxes will be needed to hold apples and oranges (10 fruit per box)." << endl;
cout << "How many apples do you have? ";
cin >> apples;
cout << endl << apples << " apples." << endl;
cout << endl << "How many oranges do you have? ";
cin >> oranges;
cout << endl << oranges << " oranges" << endl;



sum = apples + oranges;

answer = sum / 10+1;

if(answer < 10)
{
n = "es.";
}
else 
{
n = ".";
}
	cout << endl << "you need " << answer << " box" << n << endl;


return 0;
}




Topic archived. No new replies allowed.