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:
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
7 5231
Can you please put your code in [code] tags? It maakes it a lot easier to read it.
Thanks.
Here is the better formatted code (I think): -
-
//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; // not used
-
-
void getData ();
-
void showMenu ();
-
-
int main()
-
{
-
string inputMenu;
-
string outputMenu;
-
-
inMenu1.open("itemNo.txt", ios::in);
-
-
if (!inMenu1) // I would prefer "if (!inMenu1.is_open())"
-
{
-
cout << "Cannot open the input file." << endl;
-
return 1;
-
}
-
-
getData ();
-
showMenu ();
-
-
inMenu1.close();
-
outMenu.close();
-
-
return 0;
-
}
-
-
-
void getData ()
-
{
-
int index;
-
-
string menuItem; //not used
-
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; // not used
-
-
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;
-
}
-
-
}
Savage 1,764
Recognized Expert Top Contributor
[
I'm quite sure that problem is among this lines: - 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;
-
}
Now let us take a closer look.
Savage
Is there any particular reason that you are outputting the menu twice (i.e in getData () and showMenu ())? - cout << item << setw(15) << setprecision(2)
-
-
<< setiosflags(ios::fixed) << right << "$"
-
-
<< price << endl;
Okay, ignore the previous post.
The question that I should have really asked is what exactly is this program supposed to do or output etc. (Correct me if I'm wrong, but...) I (and possibly others) would find a brief description useful to finding the solution to your problem (that way we know what needs correcting) =).
Your problem is here: -
inMenu1 >> menuList[index].menuPrice;
-
True, menuList[index].menuPrice is a double and the price of 1.45 does go in there.
BUT! The \n on that line of data is still in the input stream. You need to eat that enter key.
Change your code to: -
inMenu1 >> menuList[index].menuPrice;
-
inMenu1.ignore();
-
and your world will become happy again.
HEY! Thank you! It worked!!
Thanks for your patience with this newbie...if I post again, I will be sure to remember the rules.
Thanks again,
Woody
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: cpptutor2000 |
last post by:
I am using an STL list to store data packets in a network simulator.
The data packets are really C structs, which have other C structs
inside them. These structs contain
unsigned char arrays of various sizes, and some unsigned int values.
There are no pointers values stored in these structs.
I have a function, from inside of which I have a loop as follows:
Please note that dataPacketStore is the name of the struct being
stored.
...
|
by: aj902 |
last post by:
Hello ,
I am trying to create a program
where all detail,
http://www.albany.edu/~csi333/projects.htm
|
by: Bleakcabal |
last post by:
Keep in mind my program is written in C ( not C++ ).
I have a function which takes two structs as parameters is supposed to
put the values of the source struct in the destination struct. Only
the first character is put into the destination struct for each field.
void AssignValues(info *source, info *destination)
{
*destination->name = *source->name;
|
by: Paminu |
last post by:
Why make an array of pointers to structs, when it is possible to just make
an array of structs?
I have this struct:
struct test {
int a;
int b;
|
by: hothead098 |
last post by:
ASSIGNMENT (4) USING AND MANIPUPATING ARRAYS (Chapter 10 material)
For this assignment you are to:
1) Create and manage arrays
a) One of type integers (containing 10 elements).
b) One of type strings (containing 20 elements).
c) One of type char (containing 30 elements).
| |
by: ward |
last post by:
Greetings.
Ok, I admit it, I bit off a bit more than I can chew.
I need to complete this "Generate Report" page for my employer and I'm
a little over my head. I could use some additional assistance. I say
additional because I've already had help which is greatly appreciated.
I do try to take the time and understand the provided script in hopes
on not having to trouble others on those. But here it goes...
|
by: Marty |
last post by:
I am new to C# and to structs so this could be easy or just not
possible.
I have a struct defined called Branch
If I use Branch myBranch = new Branch(i); // everything works
If I use Branch (myBranch + x) = new Branch(i); // it doesn't
x is a loop iterator, i is an int for the constructor to define an
array.
What am I doing wrong here.
|
by: Ray Z |
last post by:
I would like to have someone to help me out with my program.
Users are able to add items if they wish for the Monthly Claims Charges System.
I did not manage to do the calculation part of the program.
Could someone help me? :) Here is the program:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Monthly claims Charges</title>
|
by: Francois Grieu |
last post by:
Hello,
is the size of a type, defined using typedef as a collection of
arrays of unsigned char, the sum of the size of the arrays ?
After simplifying my question, it probably is equivalent to:
does the following program terminates ?
int main(void) {
typedef struct t { unsigned char f; } t;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |