ofstream is creating a file but writting only starting word not full string

229 views Asked by At

This is My code. It makes a file but only prints he first word i input. Please help.

std::string userinput;
int a =rand() % 4+1;
std::stringstream ss;
ss << a;
std::string line = ss.str();
std::string mfile="C:\\Users\\Venom\\Documents\\atom\\c++\\projects   \\pyramid\\info files\\infernotxt\\help\\greeting\\" +line;
//std::cout << mfile << '\n';
std::ifstream infile(mfile.c_str());
getline(infile, line);
std::cout << infernoname<<line << '\n';
infile.close();


std::cout << username;
std::ofstream output(guestfolder.c_str());
std::getline(std::cin, userinput);
output<<userinput;
output.close();
2

There are 2 answers

0
Dan2004 On

cin >> var; only reads until first space. In order to prevent this use getline(cin, var);

7
Jeremy Law On

The problem with the code is that you're using std::cin which only gets the first word of input. What you want to use is getline(string).

The problem: Code. Input/Output


getline usage, assuming userinput is a std::string

std::getline (std::cin,userinput);