C++ C++;程序运行但不';不要让用户输入所有信息
这是我一直在做的一个项目。我刚刚开始编程,所以我现在只想写一些简单的程序。我所做的这些在这一次之前都非常有效。程序将运行,但不允许用户输入所有信息。我不知道为什么。如果有人能帮我理解。代码如下:C++ C++;程序运行但不';不要让用户输入所有信息,c++,C++,这是我一直在做的一个项目。我刚刚开始编程,所以我现在只想写一些简单的程序。我所做的这些在这一次之前都非常有效。程序将运行,但不允许用户输入所有信息。我不知道为什么。如果有人能帮我理解。代码如下: #include "stdafx.h" #include <iostream> #include <iomanip> using namespace std; int main () { double salary = 0.0; double wages = 0
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
double salary = 0.0;
double wages = 0.0;
float employee = 0;
int hours;
double total = 0.0;
cout << "Please enter an employee name or -1 to quit";
cin >> employee;
cout << "Please enter the employee wages: $" << endl;
cin >> wages;
cout << "Please enter how many hours the employee worked" << endl;
cin >> hours;
wages * hours == salary;
cout << "The employee total earnings is $" << total << endl;
cout << "Please enter an employee name or -1 to quit";
system("pause");
return 0;
}
#包括“stdafx.h”
#包括
#包括
使用名称空间std;
int main()
{
双薪=0.0;
双倍工资=0.0;
浮动员工=0;
整小时;
双倍合计=0.0;
cout>员工;
工资;
休息时间;
工资*工时==工资;
不能
wages * hours == salary;
是没有意义的(我甚至不确定它是做什么的)。你可能想计算工资,但应该是这样的
salary = wages * hours;
你也从不计算你的total
变量-它总是0。cout你需要一个循环。这个循环一直运行到员工姓名等于“-1”。你将员工设置为float
。我假设你指的是员工姓名,所以你想在那里有一个字符串
cout << "Please enter an employee name or -1 to quit";
system("pause");
return 0;
简单的解释是,cin
在它期待一个数字时会中断,而你给它一个字符串。所谓“中断”,我的意思是后续的cin
s,尤其是期待数字的那些,会被一团乱麻吞没。我不确定你想要实现什么,但也许你想要这样的东西:
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
double salary = 0.0;
double wages = 0.0;
String employee = "";
int hours = 0;
double total = 0.0;
cout << "Please enter an employee name or -1 to quit";
cin >> employee;
cout << "Please enter the employee wages: $" << endl;
cin >> wages;
cout << "Please enter how many hours the employee worked" << endl;
cin >> hours;
salary = wages * hours;
cout << "The employee total earnings is $" << total << endl;
cout << "Please enter an employee name or -1 to quit";
system("pause");
return 0;
}
#包括“stdafx.h”
#包括
#包括
使用名称空间std;
int main()
{
双薪=0.0;
双倍工资=0.0;
字符串employee=“”;
整小时=0;
双倍合计=0.0;
cout>员工;
工资;
休息时间;
工资=工资*工时;
cout这取决于你所说的“所有信息”是什么意思。我的猜测是,你试图为员工姓名输入名字和姓氏,它跳过了一些输入。原因是cin
(有些不直观)按空格分隔输入。这意味着它将每个字符串、数字、字符块等(称为“标记”)视为一个单独的cin对象,即使您输入多个并按enter键
所以,如果你看到这个:
请输入员工姓名或-1退出
进入“某个身份不明的人”,它会把“约翰”发给员工,打印“请输入员工工资:$$”,然后立即存储“DOE”给工资,不提示输入,因为“DOE”仍在等待存储,这是第一个C++“GOTCHA”!这是初学者的经验,有很多方法。其中一种是使用
cin.getline(…)
。另一种是使用cin>>noskipws>>employee;
显然,将一个单词存储到float
中有其自身的问题,但我假设您是单独处理的。如果您要存储字符串,请查看std::string。哪些信息您不能输入?这里还有一个错误:工资*小时==工资;
,应该只有一个=/code>符号,并且位置为可以冲销(工资=工资*工时
)感谢您提供的信息。它不允许用户输入工资或工时。它只允许用户输入姓名。当您输入姓名时会发生什么情况?它是否终止,或者它只是不继续输入工资,或者它是否要求输入工资,但不允许用户输入任何内容?它允许您在您输入姓名时输入姓名输入下一个输入时,它只是跳过输入并将其全部显示在屏幕上。然后它说按任意键继续执行您到底想在这里做什么?它不清楚…您想实现什么?更不用说,total
变量在尝试输出时从未赋值it@DanF它是0.0,在申报区。嗯,我假设它应该是某个计算值我只是输入一个名字而不是最后一个我不知道如何处理字符串
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
double salary = 0.0;
double wages = 0.0;
String employee = "";
int hours = 0;
double total = 0.0;
cout << "Please enter an employee name or -1 to quit";
cin >> employee;
cout << "Please enter the employee wages: $" << endl;
cin >> wages;
cout << "Please enter how many hours the employee worked" << endl;
cin >> hours;
salary = wages * hours;
cout << "The employee total earnings is $" << total << endl;
cout << "Please enter an employee name or -1 to quit";
system("pause");
return 0;
}