所以我仔细阅读了其他一些文章,但我似乎无法找到理由为什么这样做不会奏效。我是C ++的新手,请你好心。
// User Pay.cpp : Defines the entry point for the console application.
// This program calculates the user's pay.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
double hours, rate, pay;
// Get the number of hours worked.
cout << "how many hours did you work? ";
cin >> hours;
// Get the hourly pay rate.
cout << "How much do you get paid per hour? ";
cin >> rate;
//C alculates the Pay.
pay = hours * rate;
// Display the pay.
cout << "You have earned $" << pay << endl;
return 0;
}
答案 0 :(得分:1)
您不需要包含#include“stdafx.h”。
未来更好的做法是不要包含整个std库(“using namespace std”)。而不是这个你可以直接调用std :: cout,std :: cin等...
在“返回0”之前,代码末尾的系统(“PAUSE”)调用也会有所帮助(在您的示例中)。因此,当程序执行时,控制台不会关闭,您可以看到结果。
代码示例:
#include <iostream>
//using namespace std;
int main()
{
double hours, rate, pay;
// Get the number of hours worked.
std::cout << "how many hours did you work? ";
std::cin >> hours;
// Get the hourly pay rate.
std::cout << "How much do you get paid per hour? ";
std::cin >> rate;
//C alculates the Pay.
pay = hours * rate;
// Display the pay.
std::cout << "You have earned $" << pay << std::endl;
system("PAUSE");
return 0;
}
答案 1 :(得分:0)
尝试创建一个空项目(取消选中预编译的头文件)。然后复制你的代码,但删除#include&#34; stdafx.h&#34;。
答案 2 :(得分:-2)
看起来好像有错误,然后添加:
`using namespace std;`
现在应该没有错误。