[英]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;
}
您不需要包含 #include "stdafx.h"。
未來更好的做法是不包括整個 std 庫(“使用命名空間 std”)。 您可以直接調用 std::cout、std::cin 等,而不是這樣...
在“返回 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;
}
嘗試創建一個空項目(取消選中預編譯頭)。 然后復制您的代碼但刪除#include“stdafx.h”。
看起來好像有錯誤,然后補充道:
`using namespace std;`
現在應該沒有錯誤了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.