expected `;' before "pennies"? C++ Debugging (Code Completed)
Can anyone tell me why I get an error on my last cout
?
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <cstring>
#include <conio.h>
using namespace std;
inline void keep_window_open() { char ch; cin>>ch; }
int main()
{
cout << "How many pennies do you have?\n";
int pennies;
cin >> pennies;
double total_pen;
total_pen = (0.01 * pennies);
if (pennies >= 1)
{
string penn = " pennies.";
}else
{
string penn = " penny.";
} cout << "How many nickles do you have?\n";
int nickles;
cin >> nickles;
double total_nic;
total_nic = (0.05 * nickles);
if (nickles >= 1)
开发者_如何学运维{
string five = " nickels.";
}else
{
string five = " nickel.";
} cout << "How many dimes do you have?\n";
int dimes;
cin >> dimes;
double total_dim;
total_dim = (0.10 * dimes);
if (dimes >= 1)
{
string ten = " dimes.";
}else
{
string ten = " dime.";
} cout << "How many quarters do you have?\n";
int quarters;
cin >> quarters;
double total_qua;
total_qua = (0.25 * quarters);
if (quarters >= 1)
{
string twentyfive = " quarters.";
}else
{
string twentyfive = " quarter.";
} cout << "How many half-dollars do you have?\n";
int half_dollars;
cin >> half_dollars;
double total_dol;
total_dol = (0.50 * half_dollars);
if (half_dollars >= 1)
{
string fifty = " half dollars.";
}else
{
string fifty = " half dollar.";
}
string saying = "You have ";
cout << saying pennies penn << "\n" << saying nickles five << "\n" << saying dimes ten << "\n" << saying quarters twentyfive << "\n" << saying half_dollars fifty << "\n";
keep_window_open()
return 0;
}
Add more <<
:
cout << saying << pennies << penn << "\n"
<< saying << nickles << five << "\n"
<< saying << dimes << ten << "\n"
<< saying << quarters << twentyfive << "\n"
<< saying << half_dollars << fifty << "\n";
EDIT: Also, you are declaring variables in inner blocks - their names are no longer valid outside. Declare your strings earlier.
You are missing the <<
between the variables.
Try:
cout << saying << pennies << penn << "\n" << saying << nickles << five << "\n" << saying << dimes << ten << "\n" << saying << quarters << twentyfive << "\n" << saying << half_dollars << fifty << "\n";
Update:
The scope of some of your variables, such as penn
, means they cannot be seen in the cout statement.
You need to declare the variables outside the if/else statements.
Also, as mentioned by @Color Bend, you are missing a semicolon after the keep_window_open()
function.
You're missing the <<
between the strings and the numbers. It should read:
cout << saying << pennies << penn << ...
The statement keep_window_open()
those not have a semicolon.
As well as the missing <<
, your penn
, five
, ten
, twentyfive
and fifty
string variables are all out of scope by the time you get to that cout.
You can't do this:
if (quarters >= 1)
{
string twentyfive = " quarters.";
}
else
{
string twentyfive = " quarter.";
}
As the string no longer exists when it leaves the scope of those braces. If you want to do it like that you need to declare the variable outside the braces first.
string twentyfive = " quarter.";
if (quarters >= 1)
{
twentyfive = " quarters.";
}
Or use the ternary operator to neaten it up:
string twentyfive = (quarters > 1 ? " quarters." : " quarter.");
精彩评论