0

I came across a problem in which I need to load data from a text file, and than save it into an array of string type. My approach is to consider the array as a 2D array, but of char type.

This is my code:

string *rollno;
rollno=new string[2];
string line;

ifstream in("file.txt",ios::app);

int i=0;
char single;
in.get(single);

while (single != '.') {
    for (int j=0; single!=',' || single!='.'; j++) {
        rollno[i][j]=single;\\saving in array character wise
    }

    in.get(single);\\getting the next line 
    i++;
}

cout<<rollno[0]<<endl<<rollno[1];\\checking

Could anyone help me figure out what I'm doing wrong?

Will
  • 24,082
  • 14
  • 97
  • 108
  • What's your question? – R Sahu Jan 23 '16 at 21:48
  • What is the contents of the file? What if you increase `i` beyond `1` (which is the top index of `rollno`)? Or what if you use any indexing into an ***empty*** string? – Some programmer dude Jan 23 '16 at 21:51
  • @RSahu I need to make a school management system for which I am required to input from a file. – Firzok Nadeem Jan 23 '16 at 21:55
  • @joachimPileborg The file has the details of student in following format: Roll No,First Name,Last Name,age,City. currently I am taking size 2 just to test. And the file I am using has data for two students only. – Firzok Nadeem Jan 23 '16 at 21:59
  • You kind of missed the important part of my comment, that indexing an empty string will index out of bounds. If a string if empty then *any* index will be out of bounds. – Some programmer dude Jan 23 '16 at 22:05
  • You still haven't asked a question. You explained (a tiny bit) what your objective is but not what you are struggling with. – Dietmar Kühl Jan 23 '16 at 22:07
  • I am struggling with storing data( character by character) in an array of string type. Pointing towards line 15 @DietmarKühl – Firzok Nadeem Jan 23 '16 at 22:14
  • I still couldn't get what you are trying to say. Are you trying to say that I can not use string arrays unless I initialize them where they are declared? @joachimpileborg – Firzok Nadeem Jan 23 '16 at 22:17
  • A `std::string` object *isn't* an array, and it doesn't matter, you can't index out of bounds. If the size of *real* array is zero, what do you think would be a valid index? – Some programmer dude Jan 23 '16 at 22:19
  • I read somewhere that strings are character arrays. So you can access them like you access an array. I am not sure about inputting. @joachimpileborg – Firzok Nadeem Jan 23 '16 at 22:24
  • You can use a `std::string` object similar to an array, but it doesn't mean you can use an index that is out of bounds, neither for `std::stirng`, `std::vector`, and actual array, or anything else that allows indexing. If you want to ***append*** a character to a `std::string` object, use e.g. the `+=` operator, like `rollno[i] += single;`. After making sure `i` is within bounds. Just think about it logically, if something has the size zero, how could you be able to access element `x`? – Some programmer dude Jan 23 '16 at 22:31
  • And also, if you want a dynamic array use [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) instead. You might also want to read e.g. [this `std::string` reference](http://en.cppreference.com/w/cpp/string/basic_string). As well as [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to find a good beginners book. – Some programmer dude Jan 23 '16 at 22:32
  • Sure - you string has zero size. You'll need extend the string to an appropriate size before you can access a character. You can, for example, use `push_back()` to add each character. You also need to check whether you successfully read a character, BTW. – Dietmar Kühl Jan 23 '16 at 22:33
  • So I can not use rollno[i][j] in line no 15? Then what would you suggest? @joachimpileborgh – Firzok Nadeem Jan 23 '16 at 22:36
  • That means storing one character at a time is not possible. I'll have to temporarily save the complete roll number to a string variable and then save it to my array later. @dietmarkühl – Firzok Nadeem Jan 23 '16 at 22:39

1 Answers1

0

Hmm... Not entirely sure what you're trying to do, but from the looks of it, wouldn't this be what you're looking for?

(Assuming i is the array index, and j is for std::string.operator[], as appears to be the case.)

// ...

while (single != '.') {
    while (single != ',' && single != '.') {
        rollno[i] += single; // appending to string.
    }

    in.get(single); //getting the next line 
    i++;
}

You can append a char to a std::string with operator+=, as described here.

Also, correct me if I'm wrong, but (single != ',' || single != '.') will always evaluate to true; if single == ',', then single != '.', and vice versa. If you want it to stop processing once it encounters either delimiter, then you use a logical AND to make sure it stops if either check fails.

(Apologies for any typoes or mistakes I may have missed.)