-2

When implementing this code

and the program runs when the black screen pops up it stays up nothing else waited a while and nothing happened didn't finish didn't give me process returned 0 to know it's done the output file is empty if someone can tell me what is wrong here

#include<iostream>
#include<fstream>
#include<iomanip>

using namespace std;

int main()
{
ifstream file;
ofstream out;

file.open("coinsCoint.txt");
out.open("1234567.txt");

int pennis,nickle,dime,quarter,sum=0;
float total;

while(!file.eof())
{
    file >> pennis >> nickle >> dime >> quarter;

    sum+=pennis+nickle*5+dime*10+quarter*25;

    total=sum/100.0;

}

out << "Total amount collected is: $" << fixed << showpoint << 
setprecision(2) << total;

file.close();
out.close();

return 0;

}
  • You don't even test if your files could be successfully opened. – πάντα ῥεῖ Feb 27 '19 at 21:18
  • did loads of codes without checking can you tell me how please to answer you back Thanks – Kenan Baira Feb 27 '19 at 21:36
  • `while(!file.eof())` is a very common error. [Explanation here](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). For one thing it ONLY checks that the end of the file has been reached. If anything else goes wrong the program misses it. If I'm not mistaken this includes the file not being open. – user4581301 Feb 27 '19 at 21:41
  • 1
    If the program is just sitting there doing nothing, put a print statement inside your loop or use a debugger to step through the code. You'll probably learn something. – Joseph Larson Feb 27 '19 at 21:49

1 Answers1

-1
#include<iostream>
#include<fstream>
#include<iomanip>

using namespace std;

int main()
{
    ifstream file;
    ofstream out;

    file.open("coinsCoint.txt");

    int pennis = 0, nickle = 0, dime = 0, quarter = 0, sum = 0;
    float total = 0.0;

    /* check if file is opened */
    if (file.is_open()){

        while (!file.eof())
        {
            file >> pennis >> nickle >> dime >> quarter;
            cout << "pennis " << pennis << endl;
            cout << "nickle " << nickle << endl;
            cout << "dime " << dime << endl;
            cout << "quarter " << quarter << endl;

            sum += pennis + nickle * 5 + dime * 10 + quarter * 25;
            total = sum / 100.0;
            cout << "total " << total;
        }
        file.close();
    }
    /* return if error in file open */
    else {
        cout<< "can not open given file";
        return 0;
    }
    out.open("1234567.txt");

    if (out.is_open()){
        out << "Total amount collected is: $" << fixed << showpoint <<
            setprecision(2) << total;
        out.close();
    }
    return 0;
}

A similar tutorial https://www.uow.edu.au/~lukes/TEXTBOOK/notes-cpp/io/readtextfile.html

You should always check of the file has been opened correctly by is_open()

You need to use cout << to print to console the 'Black screen'

the file looks like

1
2
3
4

the out put should be something like

pennis 1
nickle 2
dime 3
quarter 4
total 1.41Press <RETURN> to close this window...
  • I cannot support this answer. It wraps a bandaid over a and carries through a very simple mistake made by the asker. `while (!file >> pennis >> nickle >> dime >> quarter)` as recommended by [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) would have caught and exposed a missing file. – user4581301 Feb 27 '19 at 22:36