473,385 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Need help with program using structs & arrays

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 5215
Silent1Mezzo
208 100+
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 Expert 512MB
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 Expert 1GB
[

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
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
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 Expert Mod 8TB
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
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
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...
2
by: aj902 | last post by:
Hello , I am trying to create a program where all detail, http://www.albany.edu/~csi333/projects.htm
7
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...
5
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
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...
0
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...
61
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...
1
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...
7
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:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.