为什么程序不会创建输出文件? (C ++)

时间:2015-03-15 22:03:33

标签: c++ io output

//Program is meant to read input file and print in output file  
//The output is suppose to print out a RECEIPT for a customer  
//The input consists of all the information regarding name, address, etc.  
#include<iostream>  
#include<fstream>  
#include<string>  
using namespace std;  
int main()  
{  
    //Variables being used  
    string name,address,bknum1,bknum2,bknum3,  bkname1,bkname2,bkname3,auth1,auth2,auth3;  
    int aisle1,aisle2,aisle3;  
    double price1,price2,price3,   disc1,disc2,disc3,subtotal,totdisc,subafter,tax,total;  

    //Input file  
    ifstream fin;  
    fin.open("Project3_Input.txt");  
    fin.ignore();  
    getline(fin, name);  
    getline(fin, address);  
    fin >> bknum1 >> bknum2 >> bknum3;  
    fin.ignore();  
    getline(fin, bkname1);  
    getline(fin, bkname2);  
    getline(fin, bkname3);  
    fin.ignore();  
    getline(fin, auth1);  
    getline(fin, auth2);  
    getline(fin, auth3);  
    fin >> aisle1 >> aisle2 >> aisle3;  
    fin >> price1 >> price2 >> price3;  
    fin >> disc1 >> disc2 >> disc3;  
    fin.close();  

    //5% discount is applied when book price exceeds $90  
    if(price1 >= 90.00 || price2 >= 90 || price3 >= 90)  
    {  
        price1 *= 0.05;  
        price2 *= 0.05;  
        price3 *= 0.05;  
    }  
    else  
        return 0;  

    //Calculations for subtotal, total discount, subtotal after, tax, and total  
    subtotal = price1 + price2 + price3;  
    totdisc = disc1 + disc2 + disc3;  
    subafter = subtotal - totdisc;  
    tax = 0.08 * subafter;  
    total = subafter + tax;  
    //Output file is created
    //This is where I THINK the problem is, but you might find otherwise..
    ofstream fout;  
    fout.open("Project3_Output.txt");  
    //Receipt header  
    fout << "       The University Bookstore, San Marcos, Texas 78666 " << endl;  
    fout << "           RECEIPT #25, March 3, 2015, 3:40 PM" << endl << endl;  

    //Customers name and address  
    fout << "Customer Name: "<< name << endl;  
    fout << "Customer Address: " << address << endl << endl;  

    //First book information  
    fout << "Book Number: " << bknum1 << endl;  
    fout << "Book Title: " << bkname1 << endl;  
    fout << "Book Author: " << auth1 << endl;  
    fout << "Aisle Number: " << aisle1 << endl;  
    fout << "Price of the Book: $" << price1 << endl;  
    fout << "Cost Discount: $" << disc1 << endl << endl;  

    //...There are two other books after this..

    //Prints information  
    fout << "Sub Total of Three Books: $" << subtotal << endl;  
    fout << "Total Discount: $" << totdisc << endl;  
    fout << "Subtotal after Discount: $" << subafter << endl << endl;  
    fout << "Sales Tax Amount: $" << tax << endl << endl;  
    fout << "Total Amount Paid: $" << total << endl << endl;  
    fout << "   Thank you for shopping at The University Book Store";  
    fout.close();  
    return 0;  
 }  

我不太确定问题出在哪里。我一直在寻找一个不停的答案,但似乎已经走到了尽头。请赐教!

块引用

1 个答案:

答案 0 :(得分:2)

一个关键部分可能如下:

if(price1 >= 90.00 || price2 >= 90 || price3 >= 90)  
{  
    price1 *= 0.05;  
    price2 *= 0.05;  
    price3 *= 0.05;  
}  
else  
    return 0;

您确定要输入if吗?否则程序将退出并且永远不会创建输出文件。