Help with C++ code using structs and arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2007
Posts: 2
Reputation: Woody714 is an unknown quantity at this point 
Solved Threads: 0
Woody714 Woody714 is offline Offline
Newbie Poster

Help with C++ code using structs and arrays

 
0
  #1
Jun 4th, 2007
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:
  1. //Breakfast Billing System
  2. //Marlen LaBianco
  3. //May 20, 2007
  4. //Program to calculate a local restaurant's breakfast billing system
  5. #include<iostream>
  6. #include<fstream>
  7. #include<iomanip>
  8. #include<string>
  9. using namespace std;
  10. const int Breakfast_Items = 8;
  11. struct menuItemType
  12. {
  13. string menuItem;
  14. double menuPrice;
  15. };
  16. menuItemType menuList[Breakfast_Items];
  17. ifstream inMenu1;
  18. ofstream outMenu;
  19. void getData ();
  20. void showMenu ();
  21.  
  22. int main()
  23. {
  24.  
  25. string inputMenu;
  26. string outputMenu;
  27. inMenu1.open("itemNo.txt", ios::in);
  28. if (!inMenu1)
  29. {
  30. cout << "Cannot open the input file." << endl;
  31. return 1;
  32. }
  33. getData ();
  34. showMenu ();
  35. inMenu1.close();
  36. outMenu.close();
  37.  
  38. return 0;
  39. }
  40. void getData ()
  41. {
  42. int index;
  43. string menuItem;
  44. string item;
  45. double price;
  46. for (index = 0; index < 8; index++)
  47. {
  48. getline(inMenu1, menuList[index].menuItem);
  49. item = menuList[index].menuItem;
  50. inMenu1 >> menuList[index].menuPrice;
  51. price = menuList[index].menuPrice;
  52.  
  53.  
  54. cout << item << setw(15) << setprecision(2)
  55. << setiosflags(ios::fixed) << right << "$"
  56. << price << endl;
  57.  
  58. }
  59. }
  60. void showMenu ()
  61. {
  62. int index;
  63. int maxIndex = 0;
  64.  
  65. cout << "Restaurant Breakfast Menu" << endl;
  66.  
  67. for (index = 0; index < 8; index++)
  68. {
  69. cout << menuList[index].menuItem << setw(15) << setprecision(2)
  70. << setiosflags(ios::fixed) << right << "$"
  71. << menuList[index].menuPrice << endl;
  72.  
  73. }
  74. }

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
Last edited by WaltP; Jun 4th, 2007 at 6:25 pm. Reason: Added CODE tags -- you actually typed right over how to use them when you entered this post...
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,855
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: Help with C++ code using structs and arrays

 
0
  #2
Jun 4th, 2007
I kind of missed the part where you described what the output was supposed to look like. Can you post it again for me?
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,127
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Help with C++ code using structs and arrays

 
0
  #3
Jun 4th, 2007
And format your code so we can read and understand it. Be sure to use code tags, too.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2
Reputation: Woody714 is an unknown quantity at this point 
Solved Threads: 0
Woody714 Woody714 is offline Offline
Newbie Poster

Re: Help with C++ code using structs and arrays

 
0
  #4
Jun 4th, 2007
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)
  1. <ol style="list-style-type: decimal"><li><LI class=li1>//Breakfast Billing System
  2. <LI class=li1>//Marlen LaBianco
  3. <LI class=li1>//May 20, 2007
  4. <LI class=li1>//Program to calculate a local restaurant's breakfast billing system
  5. <LI class=li2>#include<iostream>
  6. <LI class=li1>#include<fstream>
  7. <LI class=li1>#include<iomanip>
  8. <LI class=li1>#include<string>
  9. <LI class=li1>using namespace std;
  10. <LI class=li2>const int Breakfast_Items = 8;
  11. <LI class=li1>struct menuItemType
  12. <LI class=li1>{
  13. <LI class=li1>string menuItem;
  14. <LI class=li1>double menuPrice;
  15. <LI class=li2>};
  16. <LI class=li1>menuItemType menuList[Breakfast_Items];
  17. <LI class=li1>ifstream inMenu1;
  18. <LI class=li1>ofstream outMenu;
  19. <LI class=li1>void getData ();
  20. <LI class=li2>void showMenu ();
  21. <LI class=li1>
  22. <LI class=li1>int main()
  23. <LI class=li1>{
  24. <LI class=li1>
  25. <LI class=li2>string inputMenu;
  26. <LI class=li1>string outputMenu;
  27. <LI class=li1>inMenu1.open("itemNo.txt", ios::in);
  28. <LI class=li1>if (!inMenu1)
  29. <LI class=li1>{
  30. <LI class=li2>cout << "Cannot open the input file." << endl;
  31. <LI class=li1>return 1;
  32. <LI class=li1>}
  33. <LI class=li1>getData ();
  34. <LI class=li1>showMenu ();
  35. <LI class=li2>inMenu1.close();
  36. <LI class=li1>outMenu.close();
  37. <LI class=li1>
  38. <LI class=li1>return 0;
  39. <LI class=li1>}
  40. <LI class=li2>void getData ()
  41. <LI class=li1>{
  42. <LI class=li1>int index;
  43. <LI class=li1>string menuItem;
  44. <LI class=li1>string item;
  45. <LI class=li2>double price;
  46. <LI class=li1>for (index = 0; index < 8; index++)
  47. <LI class=li1>{
  48. <LI class=li1>getline(inMenu1, menuList[index].menuItem);
  49. <LI class=li1>item = menuList[index].menuItem;
  50. <LI class=li2>inMenu1 >> menuList[index].menuPrice;
  51. <LI class=li1>price = menuList[index].menuPrice;
  52. <LI class=li1>
  53. <LI class=li1>
  54. <LI class=li1>cout << item << setw(15) << setprecision(2)
  55. <LI class=li2><< setiosflags(ios::fixed) << right << "$"
  56. <LI class=li1><< price << endl;
  57. <LI class=li1>
  58. <LI class=li1>}
  59. <LI class=li1>}
  60. <LI class=li2>void showMenu ()
  61. <LI class=li1>{
  62. <LI class=li1>int index;
  63. <LI class=li1>int maxIndex = 0;
  64. <LI class=li1>
  65. <LI class=li2>cout << "Restaurant Breakfast Menu" << endl;
  66. <LI class=li1>
  67. <LI class=li1>for (index = 0; index < 8; index++)
  68. <LI class=li1>{
  69. <LI class=li1>cout << menuList[index].menuItem << setw(15) << setprecision(2)
  70. <LI class=li2><< setiosflags(ios::fixed) << right << "$"
  71. <LI class=li1><< menuList[index].menuPrice << endl;
  72. <LI class=li1>
  73. <LI class=li1>}</li>
  74. <li>}</li>
  75. </ol>

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
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Help with C++ code using structs and arrays

 
0
  #5
Jun 4th, 2007
>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.
  1. cin.ignore(numeric_limits<streamsize>::max(), '\n');
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2391 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC