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