Hi so this program is supposed to read from a txt file three quantities for 10 customers. It is supposed to add the price of these goods (a random amount for each customer) apply a discount and/or service charge and give the total fee for each customer. The problem is we're supposed to use zeros to end each customer's transaction. Depending on the condition I use for the while loop it will only run once and then go through the for loop 10 times without going through the while loop again. Or the opposite it only goes through while loop and then goes through the for loop 9 times after the while loop is done.
Apologies for being a super noob
I've tried for 2 days to figure this out on my own, any help would be appreciated.
You're biting off things in bigger chunks that you should. Start smaller and build up to what you want to do. You should also write out in English what your algorithm is before you start coding. Presenting that description with your code will make it easier to see what you're having trouble with.
I would start with a simple program that just lists the values read from the file for each customer:
#include <fstream>
#include <iostream>
int main()
{
constchar* in_file_name = "crooked.txt";
std::ifstream in(in_file_name);
std::ostream& out = std::cout;
// std::ofstream out("homework 2.txt");
if (!in.is_open())
{
std::cerr << "ERROR: Unable to open file \"" << in_file_name << "\"\n";
return 0;
}
int item_id, item_count;
double price;
while (in >> item_id >> item_count >> price)
{
out << item_id << '\t' << item_count << '\t' << price << '\n';
if (item_id == 0 && item_count == 0 && price == 0.0)
out << '\n';
}
}
Notice that we loop on the success of extracting input from the stream. In this way we don't have to worry about the number of actual records in the file.
Having verified that was correct, I would move on to providing the line item discount, probably via a function.