473,651 Members | 2,630 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with program using structs & arrays

3 New Member
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:


Expand|Select|Wrap|Line Numbers
  1. //Breakfast Billing System
  2.  
  3. //Marlen LaBianco
  4.  
  5. //May 20, 2007
  6.  
  7. //Program to calculate a local restaurant's breakfast billing system
  8.  
  9. #include<iostream>
  10.  
  11. #include<fstream>
  12.  
  13. #include<iomanip>
  14.  
  15. #include<string>
  16.  
  17. using namespace std;
  18.  
  19. const int Breakfast_Items = 8;
  20.  
  21. struct menuItemType
  22.  
  23. {
  24.  
  25. string menuItem;
  26.  
  27. double menuPrice;
  28.  
  29. };
  30.  
  31. menuItemType menuList[Breakfast_Items];
  32.  
  33. ifstream inMenu1;
  34.  
  35. ofstream outMenu;
  36.  
  37. void getData ();
  38.  
  39. void showMenu ();
  40.  
  41.  
  42.  
  43. int main()
  44.  
  45. {
  46.  
  47.  
  48. string inputMenu;
  49.  
  50. string outputMenu;
  51.  
  52. inMenu1.open("itemNo.txt", ios::in);
  53.  
  54. if (!inMenu1)
  55.  
  56. {
  57.  
  58. cout << "Cannot open the input file." << endl;
  59.  
  60. return 1;
  61.  
  62. }
  63.  
  64. getData ();
  65.  
  66. showMenu ();
  67.  
  68. inMenu1.close();
  69.  
  70. outMenu.close();
  71.  
  72.  
  73. return 0;
  74.  
  75. }
  76.  
  77. void getData ()
  78.  
  79. {
  80.  
  81. int index;
  82.  
  83. string menuItem;
  84.  
  85. string item;
  86.  
  87. double price;
  88.  
  89. for (index = 0; index < 8; index++)
  90.  
  91. {
  92.  
  93. getline(inMenu1, menuList[index].menuItem);
  94.  
  95. item = menuList[index].menuItem;
  96.  
  97. inMenu1 >> menuList[index].menuPrice;
  98.  
  99. price = menuList[index].menuPrice;
  100.  
  101.  
  102.  
  103. cout << item << setw(15) << setprecision(2) 
  104.  
  105. << setiosflags(ios::fixed) << right << "$" 
  106.  
  107. << price << endl;
  108.  
  109.  
  110. }
  111.  
  112. }
  113.  
  114. void showMenu ()
  115.  
  116. {
  117.  
  118. int index;
  119.  
  120. int maxIndex = 0;
  121.  
  122.  
  123. cout << "Restaurant Breakfast Menu" << endl;
  124.  
  125.  
  126. for (index = 0; index < 8; index++)
  127.  
  128. {
  129.  
  130. cout << menuList[index].menuItem << setw(15) << setprecision(2) 
  131.  
  132. << setiosflags(ios::fixed) << right << "$" 
  133.  
  134. << menuList[index].menuPrice << endl;
  135.  
  136.  
  137. }
  138.  
  139. }
  140.  
  141.  
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
Jun 4 '07 #1
7 5231
Silent1Mezzo
208 New Member
Can you please put your code in [code] tags? It maakes it a lot easier to read it.

Thanks.
Jun 4 '07 #2
ilikepython
844 Recognized Expert Contributor
Here is the better formatted code (I think):
Expand|Select|Wrap|Line Numbers
  1.  
  2. //Breakfast Billing System
  3. //Marlen LaBianco
  4. //May 20, 2007
  5.  
  6. //Program to calculate a local restaurant's breakfast billing system
  7.  
  8. #include<iostream>
  9. #include<fstream>
  10. #include<iomanip>
  11. #include<string>
  12.  
  13. using namespace std;
  14.  
  15. const int Breakfast_Items = 8;
  16.  
  17. struct menuItemType
  18. {
  19.     string menuItem;
  20.     double menuPrice;
  21. };
  22.  
  23. menuItemType menuList[Breakfast_Items];
  24.  
  25. ifstream inMenu1;
  26. ofstream outMenu;           // not used
  27.  
  28. void getData ();
  29. void showMenu ();
  30.  
  31. int main()
  32. {
  33.     string inputMenu;
  34.     string outputMenu;
  35.  
  36.     inMenu1.open("itemNo.txt", ios::in);
  37.  
  38.     if (!inMenu1)     // I would prefer "if (!inMenu1.is_open())"
  39.     {
  40.         cout << "Cannot open the input file." << endl;
  41.         return 1;
  42.     }
  43.  
  44.     getData ();
  45.     showMenu ();
  46.  
  47.     inMenu1.close();
  48.     outMenu.close();
  49.  
  50.     return 0;
  51. }
  52.  
  53.  
  54. void getData ()
  55. {
  56.     int index;
  57.  
  58.     string menuItem;     //not used
  59.     string item;
  60.  
  61.     double price;
  62.  
  63.     for (index = 0; index < 8; index++)
  64.     {
  65.         getline(inMenu1, menuList[index].menuItem);
  66.         item = menuList[index].menuItem;
  67.  
  68.         inMenu1 >> menuList[index].menuPrice;
  69.         price = menuList[index].menuPrice;
  70.  
  71.         cout << item << setw(15) << setprecision(2) 
  72.  
  73.         << setiosflags(ios::fixed) << right << "$" 
  74.  
  75.         << price << endl;
  76.     }
  77. }
  78.  
  79. void showMenu ()
  80. {
  81.     int index;
  82.  
  83.     int maxIndex = 0;         // not used
  84.  
  85.     cout << "Restaurant Breakfast Menu" << endl;
  86.  
  87.     for (index = 0; index < 8; index++)
  88.     {
  89.         cout << menuList[index].menuItem << setw(15) << setprecision(2) 
  90.  
  91.         << setiosflags(ios::fixed) << right << "$" 
  92.  
  93.         << menuList[index].menuPrice << endl;
  94.     }
  95.  
  96. }
Jun 4 '07 #3
Savage
1,764 Recognized Expert Top Contributor
[

I'm quite sure that problem is among this lines:

Expand|Select|Wrap|Line Numbers
  1. for (index = 0; index < 8; index++)
  2.     {
  3.         getline(inMenu1, menuList[index].menuItem);
  4.         item = menuList[index].menuItem;
  5.  
  6.         inMenu1 >> menuList[index].menuPrice;
  7.         price = menuList[index].menuPrice;
  8.  
  9.         cout << item << setw(15) << setprecision(2) 
  10.  
  11.         << setiosflags(ios::fixed) << right << "$" 
  12.  
  13.         << price << endl;
  14.     }
Now let us take a closer look.

Savage
Jun 4 '07 #4
echo26
6 New Member
Is there any particular reason that you are outputting the menu twice (i.e in getData () and showMenu ())?

Expand|Select|Wrap|Line Numbers
  1. cout << item << setw(15) << setprecision(2)
  2.  
  3. << setiosflags(ios::fixed) << right << "$"
  4.  
  5. << price << endl;
Jun 5 '07 #5
echo26
6 New Member
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) =).
Jun 5 '07 #6
weaknessforcats
9,208 Recognized Expert Moderator Expert
Your problem is here:

Expand|Select|Wrap|Line Numbers
  1. inMenu1 >> menuList[index].menuPrice;
  2.  
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:

Expand|Select|Wrap|Line Numbers
  1. inMenu1 >> menuList[index].menuPrice;
  2. inMenu1.ignore();
  3.  
and your world will become happy again.
Jun 5 '07 #7
Woody714
3 New Member
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
Jun 6 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

8
4886
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. ...
2
2866
by: aj902 | last post by:
Hello , I am trying to create a program where all detail, http://www.albany.edu/~csi333/projects.htm
7
1874
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;
5
3123
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;
8
1597
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).
0
2252
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...
61
3737
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.
1
2147
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>
7
5302
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;
0
8349
marktang
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...
0
8275
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,...
0
8795
Oralloy
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...
1
8460
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,...
0
8576
tracyyun
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...
0
7296
agi2029
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...
1
6157
isladogs
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...
0
4143
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...
1
1906
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.