no input to file

does anybody know why I'm not going any input into my .txt file even when the conditions are true? the txt file for some reason says blank


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  #include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
   string name;
   string password;

   ofstream file("new.txt");
   ifstream theFile("new.txt");
   cout << "enter name" << endl;
   cin >> name;
   cout << "enter password" << endl;
   cin >> password;

   if(name == "adam" && password == "123"){

        theFile >> name;
        theFile >> password;
   }
}
ofstream means your program is outputting to that file. ifstream means your program is reading things from that file. In lines 21 and 22 you are telling the program to read the file. If the file is blank, it will stay blank no matter how many times you read it.
I assume you want to write the name and password into the file !?!?
In this case you need to do it like this:
1
2
3
4
5
if(name == "adam" && password == "123")
{
   file << name << ' ';
   file << password;
}

You also should check that the files are opened correctly.
1
2
3
4
5
ofstream file("new.txt");
if(file)
// ok use file
else
// handle error 

thanks guys I got inputting and outputting mixed up
Last edited on
Topic archived. No new replies allowed.