LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-18-2009, 08:37 PM   #1
nunchuckie
LQ Newbie
 
Registered: Nov 2009
Posts: 1

Rep: Reputation: 0
c++, need help returning from while-loop


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:ut);
string text;

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

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

... more newb code..

return 0;
}


Any sugestions how i can make this possible?
/nunchuckie
 
Old 11-18-2009, 09:12 PM   #2
MidSpeck
LQ Newbie
 
Registered: Jul 2009
Posts: 9

Rep: Reputation: 6
Quote:
Originally Posted by nunchuckie View Post
Code:
while (sentence != "end")  // <- Lol! :(
    {
      cin >> text;
      getline(cin, text);
      outputfile << sentence << endl;
      cout << "Row " << i << ": ";
      ++i;   
    }
It's been a /long/ time since I've programmed, but it looks like your while statement is looking at the variable "sentence" to decide whether or not it should exit. The thing is, where do you type stuff _into_ "sentence"? I see a: cin >> text; and a getline(cin, text); but I never see you doing anything with the "sentence" variable.

Mess around with that and see if that helps.
 
Old 11-19-2009, 07:25 PM   #3
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
You are almost there. If you use character arrays instead of STL strings, then you would need to do the string comparison using the standard C library (eg strcmp).

The following example would grab lines and print them until it reaches the line "end". Note that it checks the getline first in case it reaches the end of file (which can be entered using <ctrl-D> from the terminal input):

Code:
#include <iostream>
int main()
{
	std::string text;
	while (std::getline(std::cin, text) && text != "end")
		std::cout << text;
}

Last edited by neonsignal; 11-19-2009 at 07:32 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM
converting shell while loop to for loop farkus888 Programming 8 09-12-2007 02:30 AM
ld returning 1 angelo_maci Linux - Software 1 04-10-2006 01:41 PM
for loop only works properly on first loop symo0009 Programming 1 12-25-2005 05:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration