473,396 Members | 1,866 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,396 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 1935
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: 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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.