谁能告诉我为什么我的上一次出现错误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;
}
Run Code Online (Sandbox Code Playgroud)
添加更多<<:
cout << saying << pennies << penn << "\n"
<< saying << nickles << five << "\n"
<< saying << dimes << ten << "\n"
<< saying << quarters << twentyfive << "\n"
<< saying << half_dollars << fifty << "\n";
Run Code Online (Sandbox Code Playgroud)
编辑:此外,您在内部块中声明变量 - 它们的名称在外部不再有效.早先声明你的字符串.
你错过<<了变量之间的差异.
尝试:
cout <<说<< pennies << penn <<"\n"<< << << << << << << << << << << << << << << << << << << << << << << << << << << <quarters << twentyfive <<"\n"<< say << half_dollars << five <<"\n";
更新:
某些变量的范围,例如penn,意味着它们无法在cout语句中看到.
您需要在if/else语句之外声明变量.
另外,正如@Color Bend所提到的,在keep_window_open()函数之后你缺少一个分号.