I'm trying to make a program for my computer science class and it requires that I run output to the console as well as to a file.
I'm looking for a way to intialize a string at the beginning of the program and pass the variable by reference to my functions.
The functions will then append strings to the intialized variable. I need to be able to also set precision and justification for floating point variables that will be appended to the string.
I am working with stringstream right now but every time I return the output string that I intialized and appended to I get a mix an 8 character alphanumeric string instead of my output. I included my main function in the post.
The program works fine otherwise but setting the output to a single variable for the console and the file is my stumbling block.

int main() {
    //String variable for filename input in case the user has the file located elsewhere
    string filename = "C:/Users/thall1/Google Drive/Computer Science/Homework 2-1/Homework 2-1/input.dat";
    //String variable for appending output data to be written to output file
    stringstream output;
    //Totals for print_summary() to be called at end of function. Zeroed out for safety
    double totalGross = 0.00;
    double totalNet = 0.00;
    double totalFed = 0.00;
    double totalState = 0.00;
    double totalSoc = 0.00;
    //File variable for employee data file
    ifstream input(filename.c_str());
    //cout << "Enter the path to the input file (use forward slashes instead of backslashes): ";
    //cin >> filename;
    //Test to see if file can be accessed
    //Exit with error if file can't be accessed
    if(!input.is_open()) {
        cout << "The file could not be accessed." << endl;
    //Otherwise proceed to process employee info
    } else {
        //Process payroll
        process_payroll(input, output, totalGross, totalNet, totalFed, totalState, totalSoc);
        //Process Summary
        print_summary(output, totalGross, totalNet, totalFed, totalState, totalSoc);
        //Send output to screen
        cout << output << endl;
    }
    system("pause");
    return 0;
}
int process_employee(ifstream& input, int &id, double &wage, double &hours) {
    if(input) {
        input >> id >> wage >> hours;
        return 1;
    } else {
        return 0;
    }
}

The << operator for the cout object does not take the character data held in a stringstream and output it to screen. No special handling has been written for using the << operator on cout with a stringstream, so it does the default; outputs the memory location of the object.

I suggest you push the stringstream data into a string object, and then output that, as the << operator for the cout object does what you want with a string.

stringstream s;
s << "eggs";
cout << s << endl; // outputs memory location of stringstream object
string j; 
s >> j;
cout << j; // outputs contents of string
commented: Excellent :D +0

Thank you this was very helpful! Worked like a charm!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.