Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 当试图从c++;它给了我一个很大的随机数_C++ - Fatal编程技术网

C++ 当试图从c++;它给了我一个很大的随机数

C++ 当试图从c++;它给了我一个很大的随机数,c++,C++,我正试图制作一个程序,让你知道你的名字并用它向你致意。这是我的密码: #include <iostream> #include <fstream> #include <string> #include <cstdlib> using namespace std; int main() { ifstream infile; infile.open("Info.txt"); int

我正试图制作一个程序,让你知道你的名字并用它向你致意。这是我的密码:

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstdlib>

using namespace std;

int main()
{


       ifstream infile;
       infile.open("Info.txt");

int x;

infile >> x;

cout << "Hello " << x << endl;



return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
河流充填;
infle.open(“Info.txt”);
int x;
infle>>x;

cout您需要将名称作为字符串而不是int读入:

ifstream infile;
infile.open("Info.txt");

string x;

infile >> x;

cout << "Hello" << x << endl;
ifstream-infle;
infle.open(“Info.txt”);
字符串x;
infle>>x;
coutBob不是int,

将其更改为string。

为什么(尝试)读取
int
Bob
不是
int
。当您尝试读取
Bob时,会发生什么情况
转换成
int
?这不是问题,但是你真的需要
std::endl
所做的额外工作吗?
'\n'
结束一行。你假设
infle>>x
有效;它没有,但你不知道,因为你从未检查过。假设是一切之母……如果你检查你的IO操作是否成功,
if(infle>>x)
您将看到该语句失败,并且在这样做的过程中,将
x
留下一个不确定的值,您将在死后向stdout报告。