User input to a txt file

Hello, this is my first post here at cplusplus.
I started learning c++ yesterday and I can't seem to figure out how to write user input to a text file. The program I wrote will write one word, but if the user types any spaces, it ignores the space and everything after it. I tried to write "Here is some text" and it wrote "Here" to the file. However, I also want to be able to read the text and find a word from the file. So say I wrote "Here is some text." I want to ask the user for a word and if the word they type is in the .txt file, it should output a message that it was successful. Kinda like a user name ID thing. Here is what I have right now...

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
26
27
28
29
30
31
32
33
34
35
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main()
{
  string sentence;
  string mysent;

  //Creates an instance of ofstream, and opens example.txt
  ofstream a_file ( "example.txt", ios::app );
  cin >>mysent;
  // Outputs to example.txt through a_file
  a_file << mysent << "\n";
  // Close the file stream explicitly
  a_file.close();
  //Opens for reading the file
  ifstream b_file ( "example.txt" );
  //Reads one string from the file
  b_file>> mysent;
  cout<< "Please Write the sentence:" <<"\n";
  cin >> sentence;
  if (sentence == mysent)
  {
     cout << "you did it right!" << "\n";
  }
  else
  {
      cout << "No!" << endl;
  }
  system("pause");    // wait for a keypress
  // b_file is closed here
}


Any help is greatly appreciated :)
~Mashed Pwntato
Last edited on
Please use [code][/code] tags

to get more then a word use getline http://www.cplusplus.com/reference/string/getline/
to find the word in the file you should read words from the file until you find it or you reach the end of the file
Thank you, but I'm not quite getting it. I did this for line 16:
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
26
27
28
29
30
31
32
33
34
35
36
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main()
{
  string sentence;
  string mysent;

  //Creates an instance of ofstream, and opens example.txt
  ofstream a_file ( "example.txt", ios::app );
  cin >>mysent;
  // Outputs to example.txt through a_file
  a_file << getline (cin, mysent);
  a_file << "\n";
  // Close the file stream explicitly
  a_file.close();
  //Opens for reading the file
  ifstream b_file ( "example.txt" );
  //Reads one string from the file
  b_file>> mysent;
  cout<< "Please Write the sentence:" <<"\n";
  cin >> sentence;
  if (sentence == mysent)
  {
     cout << "you did it right!" << "\n";
  }
  else
  {
      cout << "No!" << endl;
  }
  system("pause");    // wait for a keypress
  // b_file is closed here
}


and it wrote this hexidecimal to the file: 0x443468
How do I search for a word in the file by the way? Sorry if these are stupid questions. Again, I started yesterday :/
Thank you!
Last edited on
You just started learning C++ YESTERDAY and you're trying to learn hexidecimals? Slow the fuck down, man. That's why you're not getting it. Begin with the basics.
I understand hexidecimals. I just need help with writing it as a string to the txt file instead of hexidecimals. I think I know the basics. So, how do I use getline to write it as a string rather than a hexidecimal?
You should use getline this way:
1
2
getline (cin, mysent);//instead of  cin >>mysent;
a_file << mysent;


To read words, example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
while ( !b_file.eof() ) //continue until the end of file is reached
{
    b_file >> sentence;//read a word (to read a line use geltine(b_file,sentence) instead)
    if (sentence == mysent)
    {
        cout << "you did it right!\n";
        break; // exit from the while loop as you found the word
    }

}

//the loop has terminated

if ( sentence != mysent )
    cout << "No!";

Well that break its not good way of getting out , just limit to use them when you use switch/Case in C++, so why don't you just do this, it will validate sentence, if not true, it will ask again and also when its true it will get out because of the condition being true:
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
26
27
28
29

cout<< "Please Write the sentence:" <<"\n";
cin >> sentence;

while ( sentence == mysent) // If sentence is not mysent, it will continue.
{

 while ( !b_file.eof() && sentence == mysent ) // Continue until the end of file is reached and //sentence its equal to mysent.

  {
     geltine(b_file,sentence); // Read  words.

    if (sentence == mysent)
    {
        cout << "you did it right!\n";
    }
  
  }

    if ( sentence != mysent ) 
  {
    cout << "No!";

    cout<< "Please Write the sentence:" <<"\n"; // Ask again for the first while to test again.
    cin >> sentence;
    
  }

}
Last edited on
Thanks Bazzy! that's nearly perfect!
It would be if he didn't use those break to get out... With Control Statements its faster and good programming though.
Last edited on
well, I tried that but it has a problem with getline(b_file,sentence); The writing part is perfect though. Right now I'm using the way Bazzy said (with b_file >> sentence; instead of geltine(b_file,sentence);) but when I type in a word that's in the file it outputs "No!" 3 times and then says "You did it right!". However, it does the same thing if I type a word that is not in the file :/
I don't know if you need this but here's the whole thing now:
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main()
{
  string sentence;
  string mysent;

  //Creates an instance of ofstream, and opens example.txt
  ofstream a_file ( "example.txt", ios::app );
  getline (cin, mysent);
  // Outputs to example.txt through a_file
  a_file << mysent << "\n";
  // Close the file stream explicitly
  a_file.close();
  //Opens for reading the file
  ifstream b_file ( "example.txt" );
  //Reads one string from the file
  b_file>> mysent;
  
  cout<< "Please Write the sentence:" <<"\n";
  cin >> sentence;
  
  while (!b_file.eof()) //continue until the end of file is reached
{
    b_file >> sentence;//read a word (to read a line use geltine(b_file,sentence) instead)
    if (sentence == mysent)
    {
        cout << "you did it right!\n";
        break; // exit from the while loop as you found the word
    }

    if ( sentence != mysent )
    {
    cout << "No!";
    }
}
  system("pause");    // wait for a keypress
  // b_file is closed here
}

Thank you for the help!
~Mashed Pwntato
Last edited on
Why to use the break if you could use the while(sentence == mysent) and the if (sentence == mysent) to validate... Like I did in your code in my last post, so that and the b_file >> sentence; and you will be just great.

I'm telling you break its most used when you use switch/case, using it out of there its considered bad programming.

Okay, thanks alot!
Topic archived. No new replies allowed.