I'm fairly new to C++ and have an assignment that is knocking me out. The text file is fairly simple:

Plain Egg
1.45
Bacon and Egg
2.45
Muffin
0.99..... etc....

And the code is:

//Breakfast Billing System
//Marlen LaBianco
//May 20, 2007
//Program to calculate a local restaurant's breakfast billing system
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;
const int Breakfast_Items = 8;
struct menuItemType
{
string menuItem;
double menuPrice;
};
menuItemType menuList[Breakfast_Items];
ifstream inMenu1;
ofstream outMenu;
void getData ();
void showMenu ();
 
int main()
{

string inputMenu;
string outputMenu;
inMenu1.open("itemNo.txt", ios::in);
if (!inMenu1)
{
cout << "Cannot open the input file." << endl;
return 1;
}
getData ();
showMenu ();
inMenu1.close();
outMenu.close();

return 0;
}
void getData ()
{
int index;
string menuItem;
string item;
double price;
for (index = 0; index < 8; index++)
{
getline(inMenu1, menuList[index].menuItem);
item = menuList[index].menuItem;
inMenu1 >> menuList[index].menuPrice;
price = menuList[index].menuPrice;


cout << item << setw(15) << setprecision(2) 
<< setiosflags(ios::fixed) << right << "$" 
<< price << endl;

}
}
void showMenu ()
{
int index;
int maxIndex = 0;

cout << "Restaurant Breakfast Menu" << endl;

for (index = 0; index < 8; index++)
{
cout << menuList[index].menuItem << setw(15) << setprecision(2) 
<< setiosflags(ios::fixed) << right << "$" 
<< menuList[index].menuPrice << endl;

}
}

And this is what the program is giving out:
Plain Egg $1.45
$0.00
$0.00
$0.00
$0.00
$0.00
$0.00
$0.00
Restaurant Breakfast Menu
Plain Egg $1.45
$0.00
$0.00
$0.00
$0.00
$0.00
$0.00
$0.00

Please help!


Woody

I kind of missed the part where you described what the output was supposed to look like. Can you post it again for me?

I'm fairly new to C++ and have an assignment that is knocking me out. The text file is fairly simple:

Plain Egg
1.45
Bacon and Egg
2.45
Muffin
0.99..... etc....

And the code is:

c Syntax (Toggle Plain Text)

[LIST=1]<LI class=li1>//Breakfast Billing System
<LI class=li1>//Marlen LaBianco
<LI class=li1>//May 20, 2007
<LI class=li1>//Program to calculate a local restaurant's breakfast billing system
<LI class=li2>#include<iostream>
<LI class=li1>#include<fstream>
<LI class=li1>#include<iomanip>
<LI class=li1>#include<string>
<LI class=li1>using namespace std;
<LI class=li2>const int Breakfast_Items = 8;
<LI class=li1>struct menuItemType
<LI class=li1>{
<LI class=li1>string menuItem;
<LI class=li1>double menuPrice;
<LI class=li2>};
<LI class=li1>menuItemType menuList[Breakfast_Items];
<LI class=li1>ifstream inMenu1;
<LI class=li1>ofstream outMenu;
<LI class=li1>void getData ();
<LI class=li2>void showMenu ();
<LI class=li1> 
<LI class=li1>int main()
<LI class=li1>{
<LI class=li1> 
<LI class=li2>string inputMenu;
<LI class=li1>string outputMenu;
<LI class=li1>inMenu1.open("itemNo.txt", ios::in);
<LI class=li1>if (!inMenu1)
<LI class=li1>{
<LI class=li2>cout << "Cannot open the input file." << endl;
<LI class=li1>return 1;
<LI class=li1>}
<LI class=li1>getData ();
<LI class=li1>showMenu ();
<LI class=li2>inMenu1.close();
<LI class=li1>outMenu.close();
<LI class=li1> 
<LI class=li1>return 0;
<LI class=li1>}
<LI class=li2>void getData ()
<LI class=li1>{
<LI class=li1>int index;
<LI class=li1>string menuItem;
<LI class=li1>string item;
<LI class=li2>double price;
<LI class=li1>for (index = 0; index < 8; index++)
<LI class=li1>{
<LI class=li1>getline(inMenu1, menuList[index].menuItem);
<LI class=li1>item = menuList[index].menuItem;
<LI class=li2>inMenu1 >> menuList[index].menuPrice;
<LI class=li1>price = menuList[index].menuPrice;
<LI class=li1> 
<LI class=li1> 
<LI class=li1>cout << item << setw(15) << setprecision(2) 
<LI class=li2><< setiosflags(ios::fixed) << right << "$" 
<LI class=li1><< price << endl;
<LI class=li1> 
<LI class=li1>}
<LI class=li1>}
<LI class=li2>void showMenu ()
<LI class=li1>{
<LI class=li1>int index;
<LI class=li1>int maxIndex = 0;
<LI class=li1> 
<LI class=li2>cout << "Restaurant Breakfast Menu" << endl;
<LI class=li1> 
<LI class=li1>for (index = 0; index < 8; index++)
<LI class=li1>{
<LI class=li1>cout << menuList[index].menuItem << setw(15) << setprecision(2) 
<LI class=li2><< setiosflags(ios::fixed) << right << "$" 
<LI class=li1><< menuList[index].menuPrice << endl;
<LI class=li1> 
<LI class=li1>}
[*]}
[/LIST]

And this is what the program is giving out:
Plain Egg $1.45
$0.00
$0.00
$0.00
$0.00
$0.00
$0.00
$0.00
Restaurant Breakfast Menu
Plain Egg $1.45
$0.00
$0.00
$0.00
$0.00
$0.00
$0.00
$0.00

It's supposed to display the following:

Plain Egg $1.49
Bacon and Egg $2.45
Muffin $0.99
Restaurant Breakfast Menu
Plain Egg $1.49
Bacon and Egg $2.45
Muffin $0.99

Thanks!

Woody

> getline(inMenu1, menuList[index].menuItem); > item = menuList[index].menuItem; > inMenu1 >> menuList[index].menuPrice; The operation inMenu1 >> leaves behind a newline. When getline() is called again, the only thing it picks up is the stray newline, and then of course the menu name is going to be attempted to be extracted into the price. It doesn't work of course, the stream gets an error, and all subsequent reads fail.

You're going to have to remove the newline after you extract the price.

cin.ignore(numeric_limits<streamsize>::max(), '\n');
This article has been dead for over six months. Start a new discussion instead.