I am trying to take data within a file and perform some operations on them
So far I can read the file and also write to a different file, as in the results.
I have two files
file_to_read.txt (contains data)
file_to_write_to.txt (file that I will write results to)
I have a for loop which walks the file_to_read file
like this
for (Index = 0; Index < 20; Index++)
{
}
I'm not sure how to go about the rest
The lines 0 to 6 within the file "file_to_read" are integers.
The rest (7-20) are strings.
So is it possible that I split the entire array?
As in, one array goes from 0 to 6 the other from 6 to 20 but 6 is set as 0 in the next for loop...
I'm also not sure about how to do the mathematical operations to something which I can't see...
I'm hung up on the fact that after I read the file, and once I get to walk the for loop correctly, how do I add these "data(s)"?
I was told that I could just do string01 + string02 in the case of the strings after the loop and perhaps storing the data in memory.
void main (void)
{
// local variables
ifstream inFile;
ofstream outFile;
string fileName("file_to_read.txt");
string fileName2("file_to_write_to.txt");
int random;
int Index = 0;
string array_from_file[20];
int results = 0;
inFile.open(fileName);
for (Index = 0; Index < 20 ; Index++)
{
for (Index = 0; Index < 6; Index ++)
{
array_from_file[1] + array_from_file[2] + array_from_file[3] = results;
cout << results;
}
}
outFile.open(fileName2);
outFile << "I'm writing this to test to a file" ;
cout << "Prompt to see screen" << endl;
cin >> random;
for (Index = 0; Index < 20 ; Index++) // <- Never breaks from this loop, Index always < 20
{
for (Index = 0; Index < 6; Index ++) // Index always reset to 0 here
{
}
}
// Should be
int outerIndex = 0;
int innerIndex = 0;
for(outerIndex = 0; outerIndex < 20; ++outerIndex)
{
for(innerIndex = 0; innerIndex < 6; ++innerIndex)
{
// Do it...err why were they nested in the first place? :-\
}
}
I think that the rest of your code needs some work as well. Can you post a sample input file?
Sorry
Could you elaborate more on the nested? Does that mean that one is within the other?
My class didn't really touch on nested, we had to read it on our own...
My professor was showing me a way to do it where I used two Indexes
Index1 and Index2
Here is an unfinished loop or two of them actually I just don't know what order.
Also how do you perform the operations once the data has been pulled up?
That's what I'm stuck on/can't get my head around.
for ( Index1 = 0; Index1 < 6; Index ++)
{
array_of_int[Index2}
Index2++
}
for (Index1; <20, Index1++)
{
string[Index2]
some_string[0] // Index 2 is 0 (inside the brackets)
}
I do apologize for the crappy order, I'm lost right now
My mistake I didn't see the last part of your post.
Here would be an input file. (at least I think this is what you mean)
int
int
int
int
int
int
word
word
word
word
.
word
word
word
word
.
I'm supposed to add, subtract, divide, multiply the integers (1-6)
Then I have to make two sentences from the words and the periods.
This is where that string01 + string02 was supposed to happen.
What is the [i] in your response? Is that just a short hand version of Index?
Also I haven't learned the use of "stream" yet, in the sense of how you used it
stream >> strings[i]
Also, could you describe in words what is going on in your latest post?
As in what does this:
stream >> strings[i]
mean?
I'm just stuck on how I add these things together from the loop.
We aren't allowed to hard code so that killed the first option in mind.
From my understanding (which could be very vague)
The array of the data will be walked by the for loop.
OK so we'd have
Index - Actual line of file
0 - 1
1 - 2
2 - 3 etc...
7 - Jane
8 - Macky
9 - is etc...
So when the loop goes through these 'cells' how do I add them together... do I need to store them first?
I mean I know it probably looks like I'm just trying to get the answer, believe me I want to learn this, however time is a problem right now as far as me entering random things found on the internet through my environment, I don't want just the straight up answer but rather a guide or something...
Anyways I appreciate your input.
I hope you can follow what I've written.
i is just a variable. This assignment requires the use of streams. If your class hasn't covered that yet, then I don't see how the professor can expect the students to use it. It's more likely that you just aren't used to the style that I wrote it in. Does this make more sense?
Yes that makes sense, that is how our Hangman and Address book programs were written.
Can I ask a dumb question, what is the purpose of the inFile >> int_array_from_file[Index]; ?
This is the storing part right?
After this point then how are the actual operations accomplished? How do I state an operation
Say I wanted to add 2 + 2
I could say
inta = 2
result = 0;
inta +inta = result;
I believe that's how I would express something literal like that...
Is the result part correct? What is the purpose of setting result = 0? (again something I picked up off the internet)