Ch2_6 - Next Previous Section 2.6: While Loops Section 2.6:...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Next Previous Section 2.6: While Loops Section 2.6: While Loops 11 Section 2.6: While Loops While loops counting sentinel input failure Next Previous Section 2.6: While Loops Section 2.6: While Loops 22 An example Read sales data from file Calculate total sales 6 10095 15 85 1275 10099 12 125 1500 10152 18 16 288 10158 10 78 780 10160 6 90 540 10166 8 101 808 number of lines of sales data part number Number of parts sold price of part price*number While loops: counting Next Previous Section 2.6: While Loops Section 2.6: While Loops 33 using namespace std; #include<iostream> #include<fstream> #include<iomanip> int main() { Header functions iomanip used for format int counter, count, item_number; double amount, unit_price, total_cost, total_sales; ifstream datain ( "salesdata1.txt" ); datain >> count; cout << "count = " << count << endl <<endl; total_sales = 0; counter = 0; cout << " item amount price cost " << endl; define variables read count (6) echo print count initialize variables print table headings open sales data file salesdata1.txt Counting example code Next Previous Section 2.6: While Loops Section 2.6: While Loops 44 cout << endl << "total sales = " << total_sales << endl << endl; while (counter<count) { datain >> item_number >> amount >> unit_price >> total_cost; cout <<setw( 10 )<< item_number<<setw( 10 )<<amount<< setw( 10 )<<unit_price<<setw( 10 )<<total_cost<<endl; total_sales = total_sales + total_cost; counter++; } while loop criteria read a line of data print data to screen keep running total of...
View Full Document

Page1 / 15

Ch2_6 - Next Previous Section 2.6: While Loops Section 2.6:...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online