Data Streams Input

Hi,

I'm having trouble with a input data stream, the data file is a .txt file with names, amount of coffee ordered and type of coffee, see below. There are 5 total orders with a whitespace in between each order.

Draco Malfoy
13
Plain Coffee

Ron Weasley
28
Latte

etc...

I"m trying to have this data feed into my program, but once it inputs it just repeats the first entry, Draco Malfoy for 5 times and does not pick up other orders. See code below.

ifstream inputFile;
inputFile.open("Project_3data.txt");
if (inputFile.fail())
{cout << "Error opening file.\n";
}
for ( int count = 0; count <= orders; count++)
{

inputFile >> users_name;
inputFile >> users_lname;
inputFile >> ounces_ordered;
inputFile >> users_order;
inputFile >> users_order2;


cout << users_name << " " << users_lname << "\n";
if (users_order = 'Plain')
{ users_order = 1;
}
else if (users_order = 'Latte')
{ users_order = 2;
}
else if (users_order = 'Macchiato')
{ users_order = 3;
}
else if (users_order = 'Frappuciino')
{ users_order = 4;
}
switch (users_order) //switch statement based on choice of coffee ordered.
{
case 1: cout << "Plain Coffee " << ounces_ordered << " oz\n";
cout << fixed << showpoint << setprecision(2);
cout << "Price of Coffee: $" << (ounces_ordered * plain_coffee) << "\n";
cout << "Sales Tax: $" << ((ounces_ordered * plain_coffee) * sales_tax) << "\n";
cout << "Total Amount Owed: $" << (((ounces_ordered * plain_coffee) * sales_tax) + (ounces_ordered * plain_coffee)) << "\n\n\n";
break;

case 2: cout << "Latte " << ounces_ordered << " oz\n";
cout << fixed << showpoint << setprecision(2);
cout << "Price of Coffee: $" << (ounces_ordered * latte) << "\n";
cout << "Sales Tax: $" << ((ounces_ordered * latte) * sales_tax) << "\n";
cout << "Total Amount Owed: $" << (((ounces_ordered * latte) * sales_tax) + (ounces_ordered * latte)) << "\n";
break;

case 3: cout << "Macchiato " << ounces_ordered << " oz\n";
cout << fixed << showpoint << setprecision(2);
cout << "Price of Coffee: $" << (ounces_ordered * macchiato) << "\n";
cout << "Sales Tax: $" << ((ounces_ordered * macchiato) * sales_tax) << "\n";
cout << "Total Amount Owed: $" << (((ounces_ordered * macchiato) * sales_tax) + (ounces_ordered * macchiato)) << "\n";
break;
case 4: cout << "Frappuccino " << ounces_ordered << " oz\n";
cout << fixed << showpoint << setprecision(2);
cout << "Price of Coffee: $" << (ounces_ordered * frappuccino) << "\n";
cout << "Sales Tax: $" << ((ounces_ordered * frappuccino) * sales_tax) << "\n";
cout << "Total Amount Owed: $" << (((ounces_ordered * frappuccino) * sales_tax) + (ounces_ordered * frappuccino)) << "\n";
break;
}
}
}

The results are as follows:

Draco Malfoy
Plain Coffee 13 oz
Price of Coffee: $1.69
Sales Tax: $0.12
Total Amount Owed: $1.81


Draco Malfoy
Plain Coffee 13.00 oz
Price of Coffee: $1.69
Sales Tax: $0.12
Total Amount Owed: $1.81


Draco Malfoy
Plain Coffee 13.00 oz
Price of Coffee: $1.69
Sales Tax: $0.12
Total Amount Owed: $1.81


Anyone see any error's i'm overlooking that's preventing it from picking up other orders? Thanks for the help!


Last edited on
@Panthers22

If you don't present complete code and you don't put it in code tags to make it readable it can be extremely difficult for people to assist. There is no way this would compile, so I'm surprised it produced any output at all.

However, here are some things that may be part of your problem.

if (users_order = 'Plain')
There are (at least) two things wrong here:
= is an assignment operator; == is the logical comparison for equality and is what you need here.
Single quotes ' go round a single character (char). You need double quotes " around a string.
The same is true of all the if statements in that block.

You are splitting up into two parts both names and orders. In the latter case this obviously wouldn't work because some of the orders - e.g. "Latte" have just a single word. For both name and order I would use getline rather than >> and use a single string to hold the name and a single string to hold the order. In fact, as long as you can guarantee the format of the input file I would use getline for all the input. You can stringstream any numerical values into their int or double variables.

for ( int count = 0; count <= orders; count++)
This will loop orders+1 times: is that what you intended? At a rough guess, as orders (=5?) is apparently known, you meant
for ( int count = 0; count < orders; count++)
Last edited on
Topic archived. No new replies allowed.