473,383 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

how to do a simple loop to store strings ??

is there any way to make a loop to store strings??

e.g. abc.txt contains 3 lines i.e.
this is line 1.
this is line 2.
this is line 3.

i want to create a loop that can make the three strings contains the
corresponding lines,

string line1; //contains "this is line1."
string line2; // contains "this is line2."
string line3; // contains "this is line3."
int no_of_lines = 3;

can a simple for loop do this ??
thanks a lot
Jul 22 '05 #1
7 1933
Read "Dynamic memory allocation" in any standard C++ book.
To be more specific , new - delete should do it for you.

If you donno in advance the number of lines, STL(Standard Template
Library) should prove useful - vector , list should do it for you.

HTH

news.hku.hk wrote:
is there any way to make a loop to store strings??

e.g. abc.txt contains 3 lines i.e.
this is line 1.
this is line 2.
this is line 3.

i want to create a loop that can make the three strings contains the
corresponding lines,

string line1; //contains "this is line1."
string line2; // contains "this is line2."
string line3; // contains "this is line3."
int no_of_lines = 3;

can a simple for loop do this ??
thanks a lot


--
Karthik

------

Humans please 'removeme' for my email.
Jul 22 '05 #2
"news.hku.hk" <bi******@hkusua.hku.hk> wrote in message news:<40********@newsgate.hku.hk>...
is there any way to make a loop to store strings??

e.g. abc.txt contains 3 lines i.e.
this is line 1.
this is line 2.
this is line 3.

i want to create a loop that can make the three strings contains the
corresponding lines,

string line1; //contains "this is line1."
string line2; // contains "this is line2."
string line3; // contains "this is line3."
int no_of_lines = 3;

can a simple for loop do this ??
thanks a lot


Individually named string variables might not be the easiest way of
processing a file. The usual solution for that would be to have a
vector of strings. If you want to use a for loop and named variables,
you could use an array of pointers to the strings (now I wonder if you
can have an array of references, and how you bind them!). But that is
having the tail wagging the dog - it might be more usual to have an
array of strings, and then have named references bound to the elements
of the array.
Jul 22 '05 #3
news.hku.hk wrote:
is there any way to make a loop to store strings??

e.g. abc.txt contains 3 lines i.e.
this is line 1.
this is line 2.
this is line 3.

i want to create a loop that can make the three strings contains the
corresponding lines,

string line1; //contains "this is line1."
string line2; // contains "this is line2."
string line3; // contains "this is line3."
int no_of_lines = 3;

can a simple for loop do this ??


Yes. Look up the std::getline function.
Jul 22 '05 #4
In article <40********@newsgate.hku.hk>,
news.hku.hk <bi******@hkusua.hku.hk> wrote:
is there any way to make a loop to store strings??

e.g. abc.txt contains 3 lines i.e.
this is line 1.
this is line 2.
this is line 3.

i want to create a loop that can make the three strings contains the
corresponding lines,

string line1; //contains "this is line1."
string line2; // contains "this is line2."
string line3; // contains "this is line3."
int no_of_lines = 3;


You can't do this with individually named string variables, but you can
do it with a vector of strings:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main ()
{
ifstream inputFile ("abc.txt");
vector<string> linevec;

string line;
while (getline (inputFile, line))
{
linevec.push_back (line);
}

for (int k = 0; k < linevec.size(); ++k)
{
cout << linevec[k] << endl;
}

return 0;
}

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #5

news.hku.hk wrote:
is there any way to make a loop to store strings??

e.g. abc.txt contains 3 lines i.e.
this is line 1.
this is line 2.
this is line 3.

i want to create a loop that can make the three strings contains the
corresponding lines,

string line1; //contains "this is line1."
string line2; // contains "this is line2."
string line3; // contains "this is line3."
int no_of_lines = 3;

can a simple for loop do this ??


Sure!

#include <sstream>
#include <string>
using namespace std;

string int_to_str(int i) {
ostringstream o;
o << i;
return o.str();
}

int main() {
const int no_of_lines=3;
string lines[no_of_lines];
for(unsigned int i=0;i<no_of_lines;++i)
lines[i]="this is line"+int_to_str(i);
}
.... except that you now have lines[0],lines[1],lines[2]
instead of line1, line2,line3.

HTH,
- J.

PS. What you really need is a textbook.
Jul 22 '05 #6
Jacek Dziedzic wrote:
e.g. abc.txt contains 3 lines i.e.


Oops, missed that line and never noticed you wanted
these from a file, sorry.

- J.
Jul 22 '05 #7

"news.hku.hk" <bi******@hkusua.hku.hk> wrote in message
news:40********@newsgate.hku.hk...
is there any way to make a loop to store strings??

e.g. abc.txt contains 3 lines i.e.
this is line 1.
this is line 2.
this is line 3.

i want to create a loop that can make the three strings contains the
corresponding lines,

string line1; //contains "this is line1."
string line2; // contains "this is line2."
string line3; // contains "this is line3."
int no_of_lines = 3;

can a simple for loop do this ??
thanks a lot


You can't do it in a loop, but why would you want to? Like this

getline(file, line1);
getline(file, line2);
getline(file, line3);

john
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: drs | last post by:
Hi, I am trying to find all lists of length x with elements a, b, and c. To this end, I have created the class below, but it is not quite working and I am having trouble figuring out what to...
4
by: Seth | last post by:
I want to create a simple hash function that can hash strings. Currently I'm storing passwords as strings in a DB but want to store them as a hash. I don't need any proper standardised hashing e.g....
3
by: Shane | last post by:
So it's been a while since I've done anything in C++, but I thought I could grab cin >> input; and it would go into string input, even if what's coming in happens to be 3 words separated by...
10
by: leaf | last post by:
How do i make a simple parser that parse strings ex. "string1 string2 string3" and store in a vector? how can it be done using BOOST.Spirit? --- leaf
11
by: juvenuts | last post by:
Hi, I'm a complete newbie to C, but I wanted to get started by writing a few simple programs that print out strings. 1. The first thing I wanted to do was write a program that uses getchar to...
2
by: martin paul | last post by:
Sir please consider the following... char a="copy mart1.c mart2.c"; Sir I would like to parse the above string using strtok() function...I need to store the individual parsed...
24
by: oliv29 | last post by:
Hello, Can anyone tell me how do i fill a structure's fields in a for loop ? #define MAX_FIELD_LEN 10 typedef struct { char field1; /*will store first string read from keyboard*/
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
2
by: dgrissen | last post by:
Hi, I am an unabashed noob. I am trying to build a list to store values that are generated through a loop. However, every time I append the variable to the list, I'd like to reset the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.