[英]cin throwing error message
I was having issues with another program. 我遇到了另一个程序的问题。 It was something like this: 它是这样的:
if(n < 15000)
{
printf("I am < 15000");
return 0;
}
else if(n > 19000)
{
printf("I am > 19000");
return 0;
}
else if(n > 51000)
{
printf("I am > 51000");
return 0;
}
If n was above 51000 then it would still return "I am > 19000". 如果n高于51000,那么它仍将返回“我> 19000”。 I figured I needed to "close the gap" by using else if(n > 19000 && n < 51000)
so to test this I wrote this program: 我想我需要通过使用else if(n > 19000 && n < 51000)
缩小差距else if(n > 19000 && n < 51000)
所以为了测试这个我编写了这个程序:
#include <iostream>
int main()
{
printf("Please enter a #: ");
int block;
cin >> n;
if(n <= 10 )
{
printf("I am <= 10");
return 0;
}
else if(n <= 15000)
{
printf("I am <= 15000");
return 0;
}
else if(n > 15000 && n <= 19000)
{
printf("I am > 15000 and <= 19000");
return 0;
}
else if(n > 19000)
{
printf("I am > 19000");
return 0;
}
else if(n > 51000)
{
printf("I am > 51000");
return 0;
}
}
Trying to compile this gave me this error: "error: 'cin' was not declared in this scope" I am using g++ <filename>
to compile on mac osx 10.7 试图编译这个给了我这个错误:“错误:'cin'未在此范围内声明”我正在使用g++ <filename>
在mac osx 10.7上编译
by 通过
#include <iostream>
you are including symbols in namespace std
, so to access standard input stream you have to write: 您在命名空间std
中包含符号,因此要访问您必须编写的标准输入流:
std::cin
Either use C++ standard input/output streams or use C standard input/outpur streams. 使用C ++标准输入/输出流或使用C标准输入/输出流。 It is a bad idea to mix them. 混合它们是个坏主意。
All standard declarations with very rare exceptions are placed in the standard name space std
所有具有极少数例外的标准声明都放在标准名称空间std
So instead of 而不是
cin >> n;
you should write 你应该写
std::cin >> n;
Or place the following directive before using cin 或者在使用cin之前放置以下指令
using std::cin;
//,,,
cin >> n;
Also you should include header <cstdio>
where function printf
is declared. 您还应该包含头<cstdio>
,其中声明了函数printf
。
Take into account that this condition 考虑到这种情况
else if(n > 19000)
is invalid because it includes all numbers greater than 10000 including numbers greater than 51000 无效,因为它包含大于10000的所有数字,包括大于51000的数字
I would write the program the following way 我会按照以下方式编写程序
#include <iostream>
int main()
{
std::cout << "Please enter a #: ";
int block;
std::cin >> n;
if ( n <= 10 )
{
std::cout << "I am <= 10";
}
else if ( n <= 15000 )
{
std::cout << "I am <= 15000";
}
else if ( n <= 19000)
{
std::cout << "I am > 15000 and <= 19000";
}
else if ( n > 19000 && n <= 51000 )
{
std::cout << "I am > 19000";
}
else if ( n > 51000 )
{
std::cout << "I am > 51000";
}
std::cout << std::endl;
}
Maybe std::cin >> n
would help? 也许std::cin >> n
会有帮助吗? Seems like an issue of a namespace to me. 对我来说似乎是一个命名空间的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.