CIN、COUT 未声明的标识符

CIN, COUT Undelcared Identifier

所以我仔细阅读了其他一些文章,但我似乎找不到这行不通的原因。我是 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;
}

貌似你出错了,然后补充:

`using namespace std;`

现在应该没有错误了

尝试创建一个空项目(取消选中预编译头文件)。然后复制您的代码,但删除#include "stdafx.h"。

您不需要包含#include "stdafx.h"。

另外一个更好的做法是不包含整个标准库 ("using namespace std")。您可以直接调用 std::cout、std::cin 等而不是这个...

在 "return 0" 之前的代码末尾调用 system("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;
}