Question

I'm having a problem getting the user input to be saved into a Txt file. Not sure what I am doing wrong; it is not writing to the file:

void Menu::nowShowingDecisions()
{
    switch (userInput)
    {
    case 1:
        system("cls");
        xyPosition(0, 0);
        CAtitleHeader();
        CAtitleMenu();
        getUserInput();
        CAtitleDecisions();

        break;
        return;

    case 2:
    {
        string userName;
        string password;
        {
            ofstream outFile;
            {
                outFile.open("C:\\Test\\Test.txt");
                if (!outFile.good())
                    cout << "File Could Not Be Opened." << endl;
                else
                {
                    cout << "Enter Username:";
                    cin >> userName;

                    cout << "Enter Password:";
                    cin >> password;
                    while (cin >> userName >> password)
                        outFile << userName << password << endl;

                    outFile.close();
                }
            }
        }
        return;
        {
            const int COL_SZ = 10;
            ifstream inFile;
            inFile.open("C:\\Test\\Test.txt");
            if (!inFile.good())
                cout << "File could not be opened" << endl;
            else
            {
                cout << left;
                cout << "Movie Ticket Accounts" << endl;
                cout << setw(COL_SZ) << "User Name" << setw(COL_SZ) << "Password" << endl;
                while (inFile >> userName >> password)
                {
                    cout << setw(COL_SZ) << userName << setw(COL_SZ) <<
                    password << setw(COL_SZ) << endl;
                }

                inFile.close();
                return;
            }
        }
    }
    break;
Was it helpful?

Solution

In this block of code

             cout << "Enter Username:";
              cin >> userName;             // BEING IGNORED

              cout << "Enter Password:";
              cin >> password;             // BEING IGNORED
              while (cin >> userName >> password) // THIS IS BEING SAVED>
                  outFile << userName << password << endl;

You are not writing the first userName and password to the output file.

It's not clear whether you wanted to really have the while loop in there. If you want to write out just the first username and password, you need to change it to:

              cout << "Enter Username:";
              cin >> userName;

              cout << "Enter Password:";
              cin >> password;
              if (cin) 
                  outFile << userName << password << endl;

OTHER TIPS

Reading or Writing to a txt using C++ can be done in a simple way like this.

// writing on a text file

 #include <iostream>
 #include <fstream>

 using namespace std;

 int main () 

 {
   ofstream myfile ("example.txt");

    if (myfile.is_open())
 {
    myfile << "This is a line.\n";
    myfile << "This is another line.\n";
   myfile.close();
 }
   else cout << "Unable to open file";
    return 0;
}

// reading a text file

 #include <iostream>
 #include <fstream>
 #include <string>
 using namespace std;

 int main () 

 {
   string line;
   ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
   {
      cout << line << '\n';
    }
  myfile.close();
 }

  else cout << "Unable to open file"; 

 return 0;
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top