Click here to Skip to main content
15,120,078 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I've been given an assignment to take values from an Ifstream file, do some calculations, then take the calculations and put them all in an output file. The problem I'm having is that the output file is only printing out the last number in the input file (40) and is skipping the rest. It might be because the other values aren't being stored but this is a fairly new topic to me and I am completely confused. I need the input values to be read one by one, calculated, and then put into output file

What I have tried:

<pre>#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>

using namespace std;
void input_file(int& total_cents);
int compute_coins(int coin_value, int& total, int& amount_left);
void output_file(int& total_cents);

int main()
{
    int total_cents, coin_value, amount_left, total, sum;

    input_file(total_cents);
    output_file(total_cents);


    return 0;
}

void input_file(int& total_cents)
{
    int coin_value, amount_left;

    ifstream inputnums;
    inputnums.open("Inputs.txt");

    if (inputnums.fail( ) )
    {
        cout << "File couldn't open";
        exit(1);
    }
        while (inputnums >> total_cents)
        {
    
        }

    inputnums.close();
}

int compute_coins(int coin_value, int& total, int& amount_left)
{
    total = amount_left / coin_value;
    amount_left = amount_left % coin_value;

    return(total);
}

void output_file(int& total_cents)
{
    int quarters = 0, dimes = 0, nickels = 0, pennies = 0;

    ofstream outputnums;
    outputnums.open ("Outputs.txt", ios::app);

    outputnums << total_cents << " cents can be given as"<< endl;
    compute_coins(25, quarters, total_cents);
    outputnums << quarters << " quarter(s), ";
    compute_coins(10, dimes, total_cents);
    outputnums << dimes << " dime(s), ";
    compute_coins(5, nickels, total_cents);
    outputnums << nickels << " nickel(s)  and " ;
    compute_coins(1, pennies, total_cents);
    outputnums << pennies << " penny(pennies) " << endl;

    outputnums.close();

}


Output:
40 cents can be given as
1 quarter(s), 1 dime(s), 1 nickel(s)  and 0 penny(pennies) 
Posted
Updated 1-Mar-21 12:31pm
Comments
jeron1 1-Mar-21 17:25pm
   
while (inputnums >> total_cents)
{

}

Looks like you keep overwriting the total_cents variable and only the last write will be seen.
blingmcqueen 1-Mar-21 18:05pm
   
How can this be fixed? The statements print fine when using cout but only print 40 or 0 when using the output file function
Richard MacCutchan 2-Mar-21 4:06am
   
Your design can be improved by thinking of the logical steps which should be:
In main function:
    open the input and output files.
    while input.eof is false
        read the next value from the input file
        call the calculate function with that value
        write the results of the calculation to the output file
    end while
    close both files

1 solution

Inside your input_file() function you have the following:
C++
while(inputnums >> total_cents)
{
}

This reads all the numbers from the input file, and does not return until you get to the end-of-file.

What you want to do is more like:
C++
while(inputnums >> total_cents)
{
    output_file(total_cents);
}


However, I recommend that you think about what is going on with your program, as written. If you only make the change suggested above, for every input value you are opening and closing the output-file, which is a major inefficiency. Better would be to do somethink like (pseudo code)
open input-file
open output-file
while (read input-file returns a number)
{
  write results to output-file
}
close input-file
close output-file
   
Comments
blingmcqueen 1-Mar-21 18:03pm
   
I've tried your way and its still not working. I've been testing some stuff and it seems like the output displays perfectly when I copy the output statements alone from the output function and replace the file name with "cout <<", it prints fine. But when I try to link it it still doesn't work

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900