Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我需要一些货币计划方面的帮助_C++ - Fatal编程技术网

C++ 我需要一些货币计划方面的帮助

C++ 我需要一些货币计划方面的帮助,c++,C++,#包括 #包括 #包括 使用名称空间std; int main() { 内部宿舍; 整数一角=(四分之一*5); 整数便士=(四分之一*25); 整数镍币=(便士/10); 宿舍; 试试这个: int main() { int quarters; int dimes; int pennies; int nickles; cout << "Please enter the amount of quarters you wish to conv

#包括
#包括
#包括
使用名称空间std;
int main()
{
内部宿舍;
整数一角=(四分之一*5);
整数便士=(四分之一*25);
整数镍币=(便士/10);
宿舍;
试试这个:

int main()
{

    int quarters;
    int dimes;
    int pennies;
    int nickles;

    cout << "Please enter the amount of quarters you wish to convert" << endl;
    cin >> quarters;

    dimes = (quarters * 5);
    pennies = (quarters * 25);
    nickles = (pennies / 10);

    cout << "Pennies = " << pennies << endl;
    cout << "Dimes = " << dimes << endl;
    cout << "Nickles = " << nickles << endl;
    cin.get();
    cin.get();

    return 0;
}
intmain()
{
内部宿舍;
一角硬币;
整数便士;
内刻痕;
宿舍;
一角硬币=(四分之一*5);
便士=(四分之一*25);
镍币=(便士/10);

cout错误消息清楚地说明了问题所在。您已经声明了
quarters
,但在使用它之前尚未初始化。 把这个


初始化变量
quarters
,然后在计算中使用它

int quarters;
cout<<"Please enter the amount of quarters you wish to convert" << endl;
cin>>quarters;

int dimes = (quarters * 5);
int pennies = (quarters * 25);
int nickles = (pennies / 10);
int-quarters;

cout正如其他人所说,您必须在使用输入之前获得输入,例如:

int quarters;
cout << "Please enter the amount of quarters you wish to convert" << endl;
cin >> quarters;

也就是说,让我们看看它为什么会发生。也许你认为当你给一个变量分配一个“公式”时(比如
dimes=quarters*5
)这个值总是会被重新计算,类似于Excel所做的。C++没有这样的工作:<强>当你给变量赋值时,你并没有定义一个通式,它会自动更新,而是在那个时刻计算结果。< /强>程序从上到下运行。定义
一角硬币
便士
镍币
的行,您正在使用(尚未定义)
四分之一
的值,并尝试使用该值来计算其他变量。在这里,程序停止,但如果它继续运行,并且到达带有
cin
的行,它确实会将该值存储在
四分之一
中,但其他3个变量不会更新!

在计算中使用它之前,需要输入该值。A谢谢你的解释。这在我的C++课程中会很有用。
int quarters;
cout<<"Please enter the amount of quarters you wish to convert" << endl;
cin>>quarters;

int dimes = (quarters * 5);
int pennies = (quarters * 25);
int nickles = (pennies / 10);
int quarters;
cout << "Please enter the amount of quarters you wish to convert" << endl;
cin >> quarters;
int dimes = (quarters * 5);
int pennies = (quarters * 25);
int nickles = (pennies / 10);