-2

My program prompts the user for a file name and I use getline to put it into my string variable. getline is not storing the user's input into filename. Why?

int main() {
string fileName;
cout << "==============================================================\n"
<< "|        Welcome to the Automatic Maze Path Finder!      |\n"
<< "=============================================================\n"
<< "\nEnter the name of the Maze configuration file: ";
getline(cin, fileName);

Also when using cout statements to test it puts the output onto the current command line. Ex: user@computer:~$ //program output goes here for some reason? Why is it doing this??

helpme
  • 11
  • 4

1 Answers1

0

std::cin is representation of standard user input and std::cout is representation of standard output. If you want the input/output to be read from/written into something different, you have to use that system (e.g. std:fstream).

Your code seems fine to me.

Zereges
  • 5,139
  • 1
  • 25
  • 49
  • Did you use any special command to compile yours? Would compiler version/c++ version make a difference? Mine is not working!! – helpme Nov 07 '15 at 22:51
  • What are you using. I compiled it using online compiler (gcc-5.1), but Microsoft CL (distributed with visual studio) works too. – Zereges Nov 07 '15 at 22:56
  • I used g++ to compile in Linux – helpme Nov 07 '15 at 22:59
  • The program will not output anything for fileName. If I i try to output another string "temp" after calling getline it does not output either. getline is ending my program? Could this be caused by a different part of my program? – helpme Nov 07 '15 at 23:05
  • Just try my program alone and check if it works. If it does, you may have some errors in your program. – Zereges Nov 07 '15 at 23:08
  • @jelly it would help if you posted the *full* program that you are running. Also, try to copy/paste it into an online compiler, like the one that Zereges has used, and see if it works there. – Fabio says Reinstate Monica Nov 07 '15 at 23:18