-8

Please see following source code written in C++. Can you make the code print “Hello World!” by providing it with an input string? You are NOT allowed to modify the source code. Compile the code with g++. Submit your input string and the screenshot proving that the code prints “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;
}

Note:- How to get Hello World! out put without editing this code Please help me

  • 2
    Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please read [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Aug 31 '18 at 06:47
  • 2
    Also, the person who gave you this assignment needs to read [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) Not that the `cin.eof()` really have anything to do with your problem. – Some programmer dude Aug 31 '18 at 06:48
  • 3
    As for your problem, it's all about *buffer overflow* and the security problems those can have. There are many books to read, as well as many articles on the Internet about this and how to do it. The person or book that gave you this assignment should have taught you some about it, or otherwise this exercise is meaningless. – Some programmer dude Aug 31 '18 at 06:50
  • I don't think this is the intention of the assignment but piping the string "Hello World!" into the program would presumably make it print "Hello World!"? – Alan Birtles Aug 31 '18 at 06:53
  • To me this seems like an exercise you are meant to fail. Then the professor will show you the surprising answer to demonstrate how clever he is. Only an evil genius could work this out for themselves. – john Aug 31 '18 at 06:56

1 Answers1

0

Suppose you are using UNIX

Create a file 'hello.txt' which contains:

Hello World!

compile your piece of code (say test.cpp)

g++ test.cpp -o test

Then execute

test < hello.txt

And the output will be

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

I have not edited your code and you have a Hello World! printed out.

PilouPili
  • 2,372
  • 2
  • 13
  • 27