CIN,COUT 未删除标识符

CIN, COUT Undelcared Identifier

本文关键字:删除 标识符 COUT CIN      更新时间:2023-10-16

所以我仔细阅读了其他一些文章,但我似乎找不到为什么这不起作用的原因。我是新来的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;
}

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

此外,将来更好的做法是不要包含整个 std 库("使用命名空间 std")。取而代之的是,您可以直接调用 std::cout、std::cin 等...

此外,在

代码末尾的"return 0"之前进行系统("暂停")调用也会有所帮助(在您的示例中)。因此,当程序执行时,控制台不会关闭,您可以看到结果。

代码示例:

#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;
}

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

看起来好像你有一个错误,然后添加:

`using namespace std;`

现在应该没有错误。