---+ For for Control Structures ---+++ Fuel Warning <sticky> %CODE{"c++"}% //Illustrates the importance of using braces in if-else statements. #include <iostream> using namespace std; int main( ) { double fuel_gauge_reading; cout << "Enter fuel gauge reading: "; cin >> fuel_gauge_reading; cout << "First with braces:\n"; if (fuel_gauge_reading < 0.75) { if (fuel_gauge_reading < 0.25) cout << "Fuel very low. Caution!\n"; } else { cout << "Fuel over 3/4. Dont stop now!\n"; } cout << "Now without braces:\n"; if (fuel_gauge_reading < 0.75) if (fuel_gauge_reading < 0.25) cout << "Fuel very low. Caution!\n"; else cout << "Fuel over 3/4. Don't stop now!\n"; return 0; } %ENDCODE%</sticky> ---+++ Multiway if-else Statement - Program to compute state income tax. <sticky> %CODE{"c++"}% //DISPLAY 3.5 Multiway if-else Statement //Program to compute state income tax. #include <iostream> using namespace std; //This program outputs the amount of state income tax due computed //as follows: no tax on income up to $15,000; 5% on income between //$15,001 and $25,000; 10% on income over $25,000. int main( ) { int net_income; double tax_bill; double five_percent_tax, ten_percent_tax; cout << "Enter net income (rounded to whole dollars) $"; cin >> net_income; if (net_income <= 15000) tax_bill = 0; else if ((net_income > 15000) && (net_income <= 25000)) //5% of amount over $15,000 taxc_bill = (0.05*(net_income - 15000)); else //net_income > $25,000 { //five_percent_tax = 5% of income from $15,000 to $25,000. five_percent_tax = 0.05*10000; //ten_percent_tax = 10% of income over $25,000. ten_percent_tax = 0.10*(net_income - 25000); tax_bill = (five_percent_tax + ten_percent_tax); } cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << "Net income = $" << net_income << endl << "Tax bill = $" << tax_bill << endl; return 0; } %ENDCODE%</sticky> ---+++ A switch Statement <sticky> %CODE{"c++"}% DISPLAY 3.6 A switch Statement //Program to illustrate the switch statement. #include <iostream> using namespace std; int main( ) { char grade; cout << "Enter your midterm grade and press Return: "; cin >> grade; switch (grade) { case 'A': cout << "Excellent. " << "You need not take the final.\n"; break; case 'B': cout << "Very good. "; grade = 'A'; cout << "Your midterm grade is now " << grade << endl; break; case 'C': cout << "Passing.\n"; break; case 'D': case 'F': cout << "Not good. " << "Go study.\n"; break; default: cout << "That is not a possible grade.\n"; } cout << "End of program.\n"; return 0; } %ENDCODE%</sticky> ---+++ A Menu <sticky> %CODE{"c++"}% //DISPLAY 3.7 A Menu //Program to give out homework assignment information. #include <iostream> using namespace std; int main( ) { int choice; do { cout << endl << "Choose 1 to see the next homework assignment.\n" << "Choose 2 for your grade on the last assignment.\n" << "Choose 3 for assignment hints.\n" << "Choose 4 to exit this program.\n" << "Enter your choice and press Return: "; cin >> choice; switch (choice) { case 1: //code to display the next assignment on screen would go here. break; case 2: //code to ask for a student number and give the corresponding //grade would go here. break; case 3: //code to display a hint for the current assignment would go //here. break; case 4: cout << "End of Program.\n"; break; default: cout << "Not a valid choice.\n" << "Choose again.\n"; } }while (choice != 4); return 0; } %ENDCODE%</sticky> ---+++ Block with a Local Variable <sticky> %CODE{"c++"}% //DISPLAY 3.8 Block with a Local Variable //Program to compute bill for either a wholesale or a retail purchase. #include <iostream> using namespace std; const double TAX_RATE = 0.05; //5% sales tax. int main( ) { char sale_type; int number; double price, total; cout << "Enter price $"; cin >> price; cout << "Enter number purchased: "; cin >> number; cout << "Type W if this is a wholesale purchase.\n" << "Type R if this is a retail purchase.\n" << "Then press Return.\n"; cin >> sale_type; if ((sale_type = = 'W') || (sale_type = = 'w')) { total = price * number; } else if ((sale_type = = 'R') || (sale_type = = 'r')) { double subtotal; subtotal = price * number; total = subtotal + subtotal * TAX_RATE; } else { cout << "Error in input.\n"; } cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << number << " items at $" << price << endl; cout << "Total Bill = $" << total; if ((sale_type = = 'R') || (sale_type = = 'r')) cout << " including sales tax.\n"; return 0; } %ENDCODE%</sticky> ---+++ The Increment Operator as an Expression <sticky> %CODE{"c++"}% //DISPLAY 3.10 The Increment Operator as an Expression //Calorie-counting program. #include <iostream> using namespace std; int main( ) { int number_of_items, count, calories_for_item, total_calories; cout << "How many items did you eat today? "; cin >> number_of_items; total_calories = 0; count = 1; cout << "Enter the number of calories in each of the\n" << number_of_items << " items eaten:\n"; while (count++ <= number_of_items) { cin >> calories_for_item; total_calories = total_calories + calories_for_item; } cout << "Total calories eaten today = " << total_calories << endl; return 0; } %ENDCODE%</sticky> ---+++ A break Statement in a Loop <sticky> %CODE{"c++"}% //DISPLAY 3.14 A break Statement in a Loop //Sums a list of ten negative numbers. #include <iostream> using namespace std; int main( ) { int number, sum = 0, count = 0; cout << "Enter 10 negative numbers:\n"; while (++count <= 10) { cin >> number; if (number >= 0) { cout << "ERROR: positive number" << " or zero was entered as the\n" << count << "th number! Input ends " << "with the " << count << "th number.\n" << count << "th number was not added in.\n"; break; } sum = sum + number; } cout << sum << " is the sum of the first " << (count - 1) << " numbers.\n"; return 0; } %ENDCODE%</sticky> ---+++ Explicitly Nested Loops <sticky> %CODE{"c++"}% //DISPLAY 3.15 Explicitly Nested Loops //Determines the total number of green-necked vulture eggs //counted by all conservationists in the conservation district. #include <iostream> using namespace std; void instructions (); int main() { cout << "This program tallies conservationist reports\n" << "on the green-necked vulture.\n" << "Each conservationist's report consists of\n" << "a list of numbers. Each number is the count of\n" << "the eggs observed in one๎ << "green-necked vulture nest.\n" << "This program then tallies << "the total number of eggs.\n"; int number_of_reports; cout << "How many conservationist reports are there? "; cin >> number_of_reports; int grand_total = 0, subtotal, count; for (count = 1; count <= number_of_reports; count++) { cout << endl << "Enter the report of " << "conservationist number " << count << endl; cout << "Enter the number of eggs in each nest.\n" << "Place a negative integer at the end of your list.\n๎;๎ subtotal = 0; int next; cin >> next; while (next >=0) { subtotal = subtotal + next; cin >> next; } cout << "Total egg count for conservationist " << " number " << count << " is " << subtotal << endl; grand_total = grand_total + subtotal; } cout << endl << "Total egg count for all reports = " << grand_total << endl; return 0; } %ENDCODE%</sticky> ---+++ [[SampleCode][ ]]
E
dit
|
A
ttach
|
Watch
|
P
rint version
|
H
istory
: r2
<
r1
|
B
acklinks
|
V
iew topic
|
Ra
w
edit
|
M
ore topic actions
Topic revision: r2 - 2017-09-20
-
JimSkon
Home
Site map
KatiWeb web
KenyonCpp web
MSSC web
Main web
SCMP118 web
Fall2016 web
SCMP391 web
Sandbox web
TWiki web
Fall2016 Web
Create New Topic
Index
Search
Changes
Notifications
RSS Feed
Statistics
Preferences
View
Raw View
Print version
Find backlinks
History
More topic actions
Edit
Raw edit
Attach file or image
Edit topic preference settings
Set new parent
More topic actions
Account
Log In
Register User
E
dit
A
ttach
Copyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki?
Send feedback