By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,412 Members | 1,009 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,412 IT Pros & Developers. It's quick & easy.

Need help with program using structs & arrays

P: 3
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
Share this Question
Share on Google+
7 Replies


100+
P: 208
Can you please put your code in [code] tags? It maakes it a lot easier to read it.

Thanks.
Jun 4 '07 #2

ilikepython
Expert 100+
P: 844
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
Expert 100+
P: 1,764
[

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

P: 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

P: 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
Expert Mod 5K+
P: 9,197
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

P: 3
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

Post your reply

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