Hello justagreenie,
Looking at the code you have posetd you are going about the program in all the wrong ways.
First a struct should hold data not an entire program.
The "fstream" and the open should be in main or another function not inside a struct. Defining a file stream as a "fstream" is OK, but in the open statement you must tell the statement if the stream is for input or output. As in
file.open("HelloWorld.txt", std::ios::in);
or out. Following the open statement you need to check if the file stream is open. If the file did not open properly the program will continue, but you will not be able to read anything.
Next you have used a vector. A good approach, I like vectors over arrays any day, but, I would make it a vector of structs where the struct would likely hold each number in its own variable and the string in its one variable.
Inside the while loop you read part of the line and put it into the vector, but only print it out. Kind of a wast of time when you should be extracking the numbers and the string into say a struct.
If you are not use to using pointers, and it looks like you are not, I would avoid creating the variable "a", which you are doing wrong, and stick with a traditional array. First you are creating a 2D array the wrong way, only creating the first dimension, and then deleting this memory at the end of the while loop only to do it again on the next iteration of the loop.
The for loop looks OK, but I would suggest a better name for the vector other than "abc". A name that describes what the variable is used for works best. Right now this seems easy, but in the future when you look back at this program and waste time trying to figure out the variable names you will wish you had learned to use better names earlier. Now is the time to start.
Do not be afraid to use comments in your program to let your-self and others know what is going on.
I do have a couple of questions.
Do the []s have any special meaning or use?
In the strings is there a reason for the _? As a string you can use a space.
Are you stuck with the layout of the input file or can it be changed?
This looks like some fun for the day. I will work up some code and see what I can come up with. A first thought is:
1 2 3 4 5 6 7 8
|
struct Info
{
int num1{};
int num2{};
int num3{};
int num4{};
std::string sentence;
};
|
Not knowing what each number is used for I did not know what to call the ints for now. Inside main I would use
std::vector<Info> info;
.
I have found this code useful when first learning about files
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
std::string iFileName{ "" }; // <--- File name goes here.
std::ifstream inFile;
inFile.open(iFileName);
if (inFile.is_open())
{
std::cout << "\n File " << iFileName << " is open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2)); // <--- Needs header files chrono" and "thread".
}
else
{
std::cout << "\n File " << iFileName << " did not open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3)); // <--- Needs header files chrono" and "thread".
exit(1);
}
|
This is used with the header file "<fstream>" which includes "istream" and "ostream".
I realize that the above code is a bit much, but it can be shortened when you have a better understanding of using file streams..
If you have any questions let me know,
Hope that helps,
Andy