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>
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
usingnamespace std;
int main ()
{
constchar filename[] = "YourFileName";
constint 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.
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
usingnamespace std;
int main ()
{
constchar filename[] = "YourFilename";
constint 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.
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.
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.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace 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;
}
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.
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.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace 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;
}