> Why isn't my program running?

Why isn't my program running?

Posted at: 2014-12-18 
http://www.cplusplus.com/doc/tutorial/fi...

At a very minimum check to make sure the file is getting opened...

#include

#include

using namespace std;

float sellingprice (float);

int totalWeight (int,int);

int main()

{

ifstream file;

file.open("file.txt");

if (file.is_open()){

int itemcount;

file >> itemcount;

int itemsprocessed = 0;

int itemnumber;

int itemweight;

int quantity;

float itemprice;

int sellprice;

int tweight;

int avgunitweight = 0;

int tenpercent = 0;

int twentypercent = 0;

for(int numb = 0; numb <= itemcount; numb++)

{

file >> itemnumber;

file >> itemweight;

file >> quantity;

file >> itemprice;

itemsprocessed++;

avgunitweight += itemweight;

if(itemprice <= 10)

tenpercent++;

else tweight++;

sellprice = sellingprice(itemprice);

tweight = totalWeight(itemweight, quantity);

cout << itemnumber << " " << itemweight << " " << quantity << " " << itemprice << " " << sellprice << " " << tweight << endl;

}

file.close();

cout << "Total number of items processed: " << itemsprocessed << endl;

cout << "Average Unit weight: " << (avgunitweight/itemsprocessed) << endl;

cout << "Items with Selling Prices Calculated with 10%: " << tenpercent << endl;

cout << "Items with Selling Prices Calculated with 20%: " << twentypercent << endl;

}

else{

cout << "Cannot open file\n";

}

return 0;

}

float sellingprice(float price)

{

if(price <= 10)

price *= 1.1;

else

price *= 1.2;

return price;

}

int totalWeight(int weight, int qty)

{

int total = 0;

total = weight * qty;

return total;

}

Where is file.txt located? If it is NOT in the same directory as your .exe file you MUST include the path to the file.

file.open("C:\\path\\file.txt"); //for example.

I know it has something to do with my file because it works when I put my own numbers in

#include

#include

using namespace std;

float sellingprice (float);

int totalWeight (int,int);

int main(){

ifstream file;

file.open("file.txt");

int itemcount;

file >> itemcount;

int itemsprocessed = 0;

int itemnumber;

int itemweight;

int quantity;

float itemprice;

int sellprice;

int tweight;

int avgunitweight = 0;

int tenpercent = 0;

int twentypercent = 0;

for(int numb = 0; numb <= itemcount; numb++){

file >> itemnumber;

file >> itemweight;

file >> quantity;

file >> itemprice;

itemsprocessed++;

avgunitweight += itemweight;

if(itemprice <= 10)

tenpercent++;

else tweight++;

sellprice = sellingprice(itemprice);

tweight = totalWeight(itemweight, quantity);

cout << itemnumber << " " << itemweight << " " << quantity << " " << itemprice << " " << sellprice << " " << tweight << endl;

}

file.close();

cout << "Total number of items processed: " << itemsprocessed << endl;

cout << "Average Unit weight: " << (avgunitweight/itemsprocessed) << endl;

cout << "Items with Selling Prices Calculated with 10%: " << tenpercent << endl;

cout << "Items with Selling Prices Calculated with 20%: " << twentypercent << endl;

return 0;

}

float sellingprice(float price){

if(price <= 10)

price *= 1.1;

else

price *= 1.2;

return price;

}

int totalWeight(int weight, int qty){

int total = 0;

total = weight * qty;

return total;

}