Creating an array of string objects from a textfile

This has been making me really frustrated for the past week, but i'm completely stuck on this and cannot proceed anywhere. Can anyone help, please? I have no idea how to progress with this, and it doesn't help that i'm very new to C++ Programming. Thank you.

Here's the question: Write a program that creates an array of 100 string objects. Fill the array by having your program open a (text) file and read one line of the file into each string until you have filled the array. Display the array using the format “line #: <string>,” where # is the actual line number (you can use the array counter for this value) and <string> is the stored string.

Here's what little code I have:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

using std::string;

int main()
{
int size = 100;
string values[size];

for (int i=0; i<size; i++)
{


}
return 0;
}
Last edited on
Hi,

please use code tags
http://www.cplusplus.com/articles/jEywvCM9/

And
1
2
3
4
5
for (int i=0; i<size; i++)
{


}

is not a good approach to your problem, you have to open the fine first and then read it and input it to your string

PS:Welcome to cplusplus.com :)
Last edited on
Here is a skeleton to get you started:
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
34
35
36
37
38
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>

using namespace std;

int main ()
{
  const char filename[] = "YourFileName";
  const int MAX_NAMES = 100;

  string names[MAX_NAMES];

  /*****************
   * open the file
   *****************/
  ifstream src (filename);
  if (!src)
  {
    perror ("File error: ");
    system ("pause"); // remove if you don't use Visual Studio
    exit (EXIT_FAILURE);
  }

  /****************
   * read the file
   ***************/
  // put your code here

  /*********************
   * display the names
   *********************/
  // put your code here

  system ("pause"); // remove if you don't use Visual Studio
  return 0;
}
Thanks for the skeleton, but like I said, i'm still stuck. I've tried and it's still getting no where. I either get compiler errors or stuck on what to write.
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
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>

using namespace std;

int main ()
{
  const char filename[] = "YourFilename";
  const int MAX_NAMES = 100;

  string names[MAX_NAMES];

  /*****************
  * open the file
  *****************/
  ifstream src (filename);
  if (!src)
  {
    perror ("File error: ");
    system ("pause"); // remove if you don't use Visual Studio
    exit (EXIT_FAILURE);
  }

  /****************
  * read the file
  ***************/
  string line; // stores a single line from the file
  int number = 0; //the number of lines we have read
  while (getline (src, line) && number < MAX_NAMES)
  {
    names[number] = line;
    number++
  }

  /*********************
  * display the names
  *********************/
  // put your code here

  system ("pause"); // remove if you don't use Visual Studio
  return 0;
}
Maybe you could post the code that you are getting the compiler errors with (and preferably the errors that the compiler gave you) so we can help you understand and solve them.

Kind regards, Nico
Alright, so this is what I got to so far. Would I have to do a for loop after the while loop with a cout to display all of the arrays?

Here's my current code:
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>
#include <string>

using namespace std;

using std::string;

int main()
{
    int text = 100;
    int number = 0;
    string sentence[text];
    string line;

    ifstream f("TMA1Q2.txt");

    cout << "Press 'enter' to display the text by line:" << endl;
    cin.get();

    while(getline(f, line) && number < text)
    {
        sentence[number] = line;
        number++;
    }

    for()
    {
        
    }
    }
    return 0;
}

Last edited on
It is probably a good idea to comment out the parts of a skeleton that you have not completed yet. You are getting compiler errors because you did not implement the for loop yet, but you did already copy the first part in.
Also, your for() has two closing brackets, the compiler will complain about that too.

So to solve it you will have to implement the for() loop or remove it and remove the extra closing bracket. Doing a std::cout in each time you loop sounds like a good approach to me.

Kind regards, Nico
Hey Nico, thank you for being so patient. I can display each line now individually, but how do I go about displaying the arrays? Or am I missing something here, because I feel like i'm missing a piece of information about what is really being displayed, and if my strings are being implemented/displayed as an array.

Here's what I did updated.

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

using namespace std;

using std::string;

int main()
{
    int text = 100;
    int number = 0;
    string sentence[text];
    string line;

    ifstream f("TMA1Q4.txt");

    cout << "Press 'enter' to display a line:" << endl;
    cin << number;

    while(getline(f, line) && number < text)
    {
        sentence[number] = line;
        number++;
    }

    for(int x=0; x<text;x++)
    {
        cout << sentence[x];
        cin.get();
    }
    //So now when I run it, I can load each line of text one at a time.
    //The main question now is, how do I display the array of 100? Did I do something wrong or miss it? everything is compiling, but I can't display the array based on it's line number.
    return 0;
}
Last edited on
You are currently displaying your array (you can't print all data in an array at once, you have to loop over it).
To make things faster, I would recommend removing line 30.

You just have to modify the format of your output.
Change line 29 to:
 
cout << x << endl;       // the << endl ends this line and makes it go to the next line 

and run your code.

Subsequently change it to
 
cout << “line #: <string>, where # is the actual line number where " << sentence[x] << " was in the file." << endl; 

and run it again.

Now you should know how to get the numbers you need and how to put them in the correct place in your output.

Kind regards, Nico
Last edited on
Hi Nico, thank you for the help. I believe I have the program completely finished now, or am close. Does everything check out? I made some changes accordingly like you mentioned in the previous reply.

Below is what I have:

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

using namespace std;

using std::string;

int main()
{
    int text = 100;
    int number = 0;
    string sentence[text];
    string line;

    ifstream f("TMA1Q4.txt");

    cout << "Press 'enter' to display a line:" << endl;
    cin.get();

    while(getline(f, line) && number < text)
    {
        sentence[number] = line;
        number++;
    }

    for(int x=0; x<text; x++)
    {
        cout << "line " << x << ": " << sentence[x];
        cin.get();
    }
    //So now when I run it, it displays as "Line #: <string>" as the question asks (or I believe so). Does everything seem correct now?
    return 0;
}

Line 7: You've already done using namespace std; which brought in std::string. This line is unnecessary.

Line 27: This is going to print unused entries in your array. You want x<number as your termination condition.
Great. Thank you so much AbstractionAnon! Cheers!
Topic archived. No new replies allowed.