My group as a project that is due and we don't know where to start. We attempted it and we failed several times. This is the project. PLEASE HELP US.

Write a C++ program that will read data from a file, perform computation on the data, then print the result to another file. The data from the file will contian 100 lines where each line contains three items. The three items are an arithmetic operation followed by two integer values (non zero.) The program should perform the arithmetic operation specified on the two integers. Then the program should print out to another file the first number, the arithmetic operation, the second integer, an = sign, then the result of the operation just performed. This should repeat for the 100 lines. The program should compute the result by first using a switch statement to identify the operation (+, -, *, /) then call the appropriate function to perform the computation and print the result to the second file. A sample input file: + 4, 54 - 34 45 * 42 2 / 325 5 A sample output file: 4+45=50 34-45=-11 42*2=84 325/5=13 The general format of the program should be as follows: - The include section... - Declaring the file pointers ifstream infile; ofstream outfile; - The add function - The subtract function - The multiply function - The divide function - The main function

9 Years
Discussion Span
Last Post by Ancient Dragon

Sounds like a homework assignment, but I'm in a generous mood, so...

Basically what your trying to do is tokenise (split into arrays) two times. Firstly, split on newlines ('\n'), secondly split each line based on spaces.

The best part is you are given the sizes of each of these (100 and 3*100=300), so you don't need to use dynamic arrays to handle the data.

Because I think this is homework, and I'm still half a sleep I wont give a complete sample, but instead a few pointers.

Splitting lines is easy using fstream:

#include <ifstream>
#include <string>

using namespace std;

string lines[100]; //This is where we'll store lines

void SplitFile(char* file)
    ifstream inFile(file);

    string buffer;

    //I've split this up for clarification, you should be able to compress this into one loop if you want:
    for (int i=0; i < 99; ++i)
        while (!inFile.bad() && !inFile.eof())
            getline(inFile, buffer);
        lines[i] = buffer;
        buffer = "";

Splitting lines, if you wanted to can be done a similar way - you just need to read in characters until encountering a space 2 times, quickly followed by a newline character

That code is probably riddled with bugs, however like I said I'm tired and the keyboard isn't working. :P

In either case, samples of what your trying to do can be found on the cprogramming.com FAQ:

See tokenising strings, File I/O (C++), string manipulation, etc.

References for functions you might want (peek(), get(), the various getline()'s, etc) can probably be found on cplusplus.com (along with other tutorials):

Best of luck :)


>>That code is probably riddled with bugs
Yes it is.

>>and I'm still half a sleep
you should have went to sleep instead of posting that code. Attempting to program while half asleep is not good -- been there and done that (unsuccessfully I might add).

const int ARRAYSIZE = 100;
string buffer;
string lines[ARRAYSIZE];
int linecount = 0;
ifstream inFile(file);
if( !inFile.is_open()) // did open succeed
while( linecount < ARRAYSIZE && getline(inFile, buffer) )
    lines[linecount] = buffer;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.