| | |
Help with C++ code using structs and arrays
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2007
Posts: 2
Reputation:
Solved Threads: 0
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:
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
Plain Egg
1.45
Bacon and Egg
2.45
Muffin
0.99..... etc....
And the code is:
c Syntax (Toggle Plain Text)
//Breakfast Billing System //Marlen LaBianco //May 20, 2007 //Program to calculate a local restaurant's breakfast billing system #include<iostream> #include<fstream> #include<iomanip> #include<string> using namespace std; const int Breakfast_Items = 8; struct menuItemType { string menuItem; double menuPrice; }; menuItemType menuList[Breakfast_Items]; ifstream inMenu1; ofstream outMenu; void getData (); void showMenu (); int main() { string inputMenu; string outputMenu; inMenu1.open("itemNo.txt", ios::in); if (!inMenu1) { cout << "Cannot open the input file." << endl; return 1; } getData (); showMenu (); inMenu1.close(); outMenu.close(); return 0; } void getData () { int index; string menuItem; string item; double price; for (index = 0; index < 8; index++) { getline(inMenu1, menuList[index].menuItem); item = menuList[index].menuItem; inMenu1 >> menuList[index].menuPrice; price = menuList[index].menuPrice; cout << item << setw(15) << setprecision(2) << setiosflags(ios::fixed) << right << "$" << price << endl; } } void showMenu () { int index; int maxIndex = 0; cout << "Restaurant Breakfast Menu" << endl; for (index = 0; index < 8; index++) { cout << menuList[index].menuItem << setw(15) << setprecision(2) << setiosflags(ios::fixed) << right << "$" << menuList[index].menuPrice << endl; } }
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...
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Jun 2007
Posts: 2
Reputation:
Solved Threads: 0
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)
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
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)
<ol style="list-style-type: decimal"><li><LI class=li1>//Breakfast Billing System <LI class=li1>//Marlen LaBianco <LI class=li1>//May 20, 2007 <LI class=li1>//Program to calculate a local restaurant's breakfast billing system <LI class=li2>#include<iostream> <LI class=li1>#include<fstream> <LI class=li1>#include<iomanip> <LI class=li1>#include<string> <LI class=li1>using namespace std; <LI class=li2>const int Breakfast_Items = 8; <LI class=li1>struct menuItemType <LI class=li1>{ <LI class=li1>string menuItem; <LI class=li1>double menuPrice; <LI class=li2>}; <LI class=li1>menuItemType menuList[Breakfast_Items]; <LI class=li1>ifstream inMenu1; <LI class=li1>ofstream outMenu; <LI class=li1>void getData (); <LI class=li2>void showMenu (); <LI class=li1> <LI class=li1>int main() <LI class=li1>{ <LI class=li1> <LI class=li2>string inputMenu; <LI class=li1>string outputMenu; <LI class=li1>inMenu1.open("itemNo.txt", ios::in); <LI class=li1>if (!inMenu1) <LI class=li1>{ <LI class=li2>cout << "Cannot open the input file." << endl; <LI class=li1>return 1; <LI class=li1>} <LI class=li1>getData (); <LI class=li1>showMenu (); <LI class=li2>inMenu1.close(); <LI class=li1>outMenu.close(); <LI class=li1> <LI class=li1>return 0; <LI class=li1>} <LI class=li2>void getData () <LI class=li1>{ <LI class=li1>int index; <LI class=li1>string menuItem; <LI class=li1>string item; <LI class=li2>double price; <LI class=li1>for (index = 0; index < 8; index++) <LI class=li1>{ <LI class=li1>getline(inMenu1, menuList[index].menuItem); <LI class=li1>item = menuList[index].menuItem; <LI class=li2>inMenu1 >> menuList[index].menuPrice; <LI class=li1>price = menuList[index].menuPrice; <LI class=li1> <LI class=li1> <LI class=li1>cout << item << setw(15) << setprecision(2) <LI class=li2><< setiosflags(ios::fixed) << right << "$" <LI class=li1><< price << endl; <LI class=li1> <LI class=li1>} <LI class=li1>} <LI class=li2>void showMenu () <LI class=li1>{ <LI class=li1>int index; <LI class=li1>int maxIndex = 0; <LI class=li1> <LI class=li2>cout << "Restaurant Breakfast Menu" << endl; <LI class=li1> <LI class=li1>for (index = 0; index < 8; index++) <LI class=li1>{ <LI class=li1>cout << menuList[index].menuItem << setw(15) << setprecision(2) <LI class=li2><< setiosflags(ios::fixed) << right << "$" <LI class=li1><< menuList[index].menuPrice << endl; <LI class=li1> <LI class=li1>}</li> <li>}</li> </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
>
>
>
The operation
You're going to have to remove the newline after you extract the price.
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)
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.
All my posts may be freely redistributed under the terms of the MIT license.
![]() |
Similar Threads
- making a inventory(rpg based)... problems[code] [/code] (C++)
- help with creating and calling a function (C++)
- Modify arrays passed by reference (C++)
- Help: Passing arrays between functions (C)
- GTK code uses pointer for structs and not normal instances why? (C)
- A couple Qs (arrays, c++, and reading in text files) (C++)
- Huge Dynamic arrays in QB45, BC7 (Legacy and Other Languages)
Other Threads in the C++ Forum
- Previous Thread: Doesn't open for file input successfully.... why?
- Next Thread: Multimedia Commands (SCSI protocol) suggestions and verifications.
Views: 2391 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






