944,052 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3352
  • C++ RSS
Jun 4th, 2007
0

Help with C++ code using structs and arrays

Expand Post »
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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Woody714 is offline Offline
2 posts
since Jun 2007
Jun 4th, 2007
0

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

I kind of missed the part where you described what the output was supposed to look like. Can you post it again for me?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 4th, 2007
0

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

And format your code so we can read and understand it. Be sure to use code tags, too.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Jun 4th, 2007
0

Re: Help with C++ code using structs and 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:

c Syntax (Toggle Plain Text)
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Woody714 is offline Offline
2 posts
since Jun 2007
Jun 4th, 2007
0

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

>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.
C++ Syntax (Toggle Plain Text)
  1. cin.ignore(numeric_limits<streamsize>::max(), '\n');
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Doesn't open for file input successfully.... why?
Next Thread in C++ Forum Timeline: Multimedia Commands (SCSI protocol) suggestions and verifications.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC