How to get out of this while-loop

Nov 19, 2009 at 3:04am
Hi guys, I just started practicing C++.
Right now I'm trying to put lines of text to a file one by one manually and then stop puting in text by typing end. The thing is that I don't really know how to put the whole line of text one by one into a file and then stop the input by typing a command like "end"...


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

int main()
{
char filename[30];
string sentence;
int i = 2;

cout << "Name your file: ";
cin >> filename;

ofstream outputfile(filename, ios::out);
string text;

cout << endl << "Row 1: ";

while (sentence != "end") // <- Lol! :(
{
cin >> text;
getline(cin, text);
outputfile << sentence << endl;
cout << "Row " << i << ": ";
++i;
}

... more code..

return 0;
}
Nov 19, 2009 at 3:11am
You're getting user input and putting it into text, not sentence. You never set sentence to anything.

Also... you're using cin >> and getline to get input. Pick just one of them. You can't do both like that. At best you'll skip every other sentence.
Nov 19, 2009 at 3:36am
Yes ofc, thank you! I changed my code to this and now the input and the break out of the loop works. But I have a new problem, I output the "Row number" one time too much. It seems like the program is going through the loop one time at the beginning to check ( text != "end" ) and this gives me one "Row number" too much at the beginning.

1
2
3
4
5
6
7
while (text != "end") 
{
   cout << "Row " << i << ": "; 
   getline(cin, text);
   outputfile << text << endl;
   ++i;
} 


EDIT;

fixed the little probelm by doing this

1
2
3
4
5
6
7
8
9
int i = 0;

while (text != "end") 
{
   ++i;
   getline(cin, text);
   outputfile << text << endl;
   cout << "Row " << i << ": "; 
} 


But now it outputs one "Row number" too much when i jump out from the loop.
Last edited on Nov 19, 2009 at 3:45am
Nov 19, 2009 at 3:47am
Look just before that while loop. Did you forget to delete the cout << endl << "Row 1: " line you have in your original post?

EDIT: I'm too slow for the edit. Although I don't think your fix helped. Try switching back to what you had before and get rid of the preceeding cout line.
Last edited on Nov 19, 2009 at 3:48am
Nov 19, 2009 at 4:00am
Oh, I forgot to mention that i did get rid of the one preceeding cout line.

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
#include<iostream>
#include<fstream>

using namespace std;

int main()
{

  char filename[30];
  string  sentence;

  int i = 0;

  cout << "Name your file: ";

  cin >> filename;
  
  ofstream outputfile(filename, ios::out);
  
  cout << endl;

  while (sentence != "end")
    {
      ++i;
      getline(cin, sentence);
      outputfile << sentence << '\n';       
      cout << "Rad " << i << ": "; 
    }
    
  
  return 0;
  
}



Example of the output would look like this:

Name your file: text.txt

Row 1: Hi what a nice day
Row 2: How are you
Row 3: end
Row 4:

..end of example, but as you see there is a row 4 too much.
Nov 19, 2009 at 4:17am
change your while loop to how it was in your previous post.
Nov 19, 2009 at 3:56pm
Hi, thanks for your reply. But it seems like the problem persist.

The output turn to look like this instead:

Name your file: text.txt

Row 1: Row 2: Hi what a nice day
Row 3: end

But I did this and it seemed to work
1
2
3
4
5
6
7
8
9
10
  while (sentence != "end")
    { 
      getline(cin, sentence);
      outputfile << sentence << '\n';       
      if(sentence != "end")    
	{
	  cout << "Rad " << i << ": "; 
	  ++i;
	}
    }


thx for your help.
Topic archived. No new replies allowed.