C ++ cin.eof函数问题

时间:2018-08-31 06:41:44

标签: c++

请参阅以下用C ++编写的源代码。您是否可以通过提供输入字符串来使代码打印“ Hello World!”?您无权修改源代码。用g ++编译代码。提交您的输入字符串和屏幕截图,以证明该代码显示“ Hello World!”。

#include <iostream>
#include <cstdio>

using namespace std;
char *p;

void f1() {
  char str[8];
  p = str;

  cout << "Please enter a string:";

  while (!cin.eof()) {
    cin.get(*p);
    p++;
  }
  cout << "The string you entered is:" << str << endl;
}

void f2()
{
  cout << "Hello World!\n";
}

int main(){ 
  cout << sizeof(char*) << endl; 
 cout << (void*) f2 << endl; 
 f1();
  return 0;
}

注意:-如何获取Hello World!无需编辑此代码即可输出 请帮助我

1 个答案:

答案 0 :(得分:0)

假设您正在使用UNIX

创建一个文件“ hello.txt”,其中包含:

Hello World!

编译您的代码(例如test.cpp)

g++ test.cpp -o test

然后执行

test < hello.txt

输出将是

8
0x400a2c
Please enter a string:The string you entered is:Hello World!

我尚未编辑您的代码,并且您已打印出Hello World!