[英]cin throwing error message
我遇到了另一个程序的问题。 它是这样的:
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;
}
如果n高于51000,那么它仍将返回“我> 19000”。 我想我需要通过使用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;
}
}
试图编译这个给了我这个错误:“错误:'cin'未在此范围内声明”我正在使用g++ <filename>
在mac osx 10.7上编译
通过
#include <iostream>
您在命名空间std
中包含符号,因此要访问您必须编写的标准输入流:
std::cin
使用C ++标准输入/输出流或使用C标准输入/输出流。 混合它们是个坏主意。
所有具有极少数例外的标准声明都放在标准名称空间std
而不是
cin >> n;
你应该写
std::cin >> n;
或者在使用cin之前放置以下指令
using std::cin;
//,,,
cin >> n;
您还应该包含头<cstdio>
,其中声明了函数printf
。
考虑到这种情况
else if(n > 19000)
无效,因为它包含大于10000的所有数字,包括大于51000的数字
我会按照以下方式编写程序
#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;
}
也许std::cin >> n
会有帮助吗? 对我来说似乎是一个命名空间的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.