





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Examples of using the setw function to manipulate the position and width of output for both integers and floating-point numbers in C++. It also covers the usage of fixed and setprecision to format floating-point numbers.
What you will learn
Typology: Schemes and Mind Maps
1 / 9
This page cannot be seen from the preview
Don't miss anything!
Electrical and Computer Engineering
1 of 52
Electrical and Computer Engineering
2 of 52
Electrical and Computer Engineering
3 of 52
UAH CPE 112
Statement (ans = 33, num = 7132) Output ( means blank) cout << setw(4) << ans
Electrical and Computer Engineering
4 of 52
UAH CPE 112
Statement (ans = 33, num = 7132) Output ( means blank)) cout << setw(4) << ans << setw(5) 33 << num
Electrical and Computer Engineering 5 of 52
UAH CPE 112
Statement (ans = 33, num = 7132) Output ( means blank) cout << setw(4) << ans << setw(5) 337132Hi << num << setw(4) << “Hi”;
Electrical and Computer Engineering 6 of 52
UAH CPE 112
Statement (ans = 33, num = 7132) Output ( means blank) cout << setw(4) << ans << setw(5) 337132Hi << num << setw(4) << “Hi”; cout << setw(2) << ans 33
Electrical and Computer Engineering
7 of 52
Statement (ans = 33, num = 7132) Output ( means blank) cout << setw(4) << ans << setw(5) 337132Hi << num << setw(4) << “Hi”; cout << setw(2) << ans << setw(4) 337132 << num
Electrical and Computer Engineering
8 of 52
Statement (ans = 33, num = 7132) Output ( means blank) cout << setw(4) << ans << setw(5) 337132Hi << num << setw(4) << “Hi”; cout << setw(2) << ans << setw(4) 337132Hi << num << setw(2) << “Hi”;
Electrical and Computer Engineering
9 of 52
UAH CPE 112
Statement (ans = 33, num = 7132) Output ( means blank) cout << setw(4) << ans << setw(5) 337132Hi << num << setw(4) << “Hi”; cout << setw(2) << ans << setw(4) 337132Hi << num << setw(2) << “Hi”; cout << setw(6) << ans
Electrical and Computer Engineering
10 of 52
UAH CPE 112
Statement (ans = 33, num = 7132) Output ( means blank) cout << setw(4) << ans << setw(5) 337132Hi << num << setw(4) << “Hi”; cout << setw(2) << ans << setw(4) 337132Hi << num << setw(2) << “Hi”; cout << setw(6) << ans << setw(3) 33Hi << “Hi”
Electrical and Computer Engineering 11 of 52
UAH CPE 112
Statement (ans = 33, num = 7132) Output ( means blank) cout << setw(4) << ans << setw(5) 337132Hi << num << setw(4) << “Hi”; cout << setw(2) << ans << setw(4) 337132Hi << num << setw(2) << “Hi”; cout << setw(6) << ans << setw(3) 33Hi << “Hi” << setw(5) << num;
Electrical and Computer Engineering 12 of 52
UAH CPE 112
Statement (ans = 33, num = 7132) Output ( means blank) cout << setw(4) << ans << setw(5) 337132Hi << num << setw(4) << “Hi”; cout << setw(2) << ans << setw(4) 337132Hi << num << setw(2) << “Hi”; cout << setw(6) << ans << setw(3) 33Hi << num << setw(5) << “Hi”; cout << setw(7) << “Hi” << setw(4) Hi << num; cout << setw(1) << ans 33
Electrical and Computer Engineering
19 of 52
ValueValue of xof x StatementStatement Output (Output ( mmeans bl ank)eans bl ank)
ValueValue cout << fixed; 310.0 cout << setw(10) << setprecision(2) << x; 310. 310.00 cout << setw(10) << setprecision(5) << x; 310. 310.0 cout << setw(7) << setprecision(5) << x; 310.
Electrical and Computer Engineering
21 of 52
of xof x StatementStatement Output (Output ( means blank)means blank)
UAH CPE 112
ValueValue cout << fixed; 310.0 cout << setw(10) << setprecision(2) << x; 310. 310.00 cout << setw(10) << setprecision(5) << x; 310. 310.0 cout << setw(7) << setprecision(5) << x; 310. 4.827 cout << setw(6) << setprecision(2) << x; 4. 4.827 cout << setw(6) << setprecision(1) << x; 4.
Electrical and Computer Engineering 23 of 52
Electrical and Computer Engineering 24 of 52
UAH CPE 112
const float PRICE = 150000.0; // Selling price less land int main(){ float grossFootage; // Total square footage float livingFootage; // Living area float costPerFoot; // Cost/foot of living area cout << fixed << showpoint; // Set up floating pt. // output format
grossFootage = LENGTH * WIDTH * STORIES;livingFootage = grossFootage - NON_LIVING_SPACE; costPerFoot = PRICE / livingFootage; cout << "Cost per square foot is “ << setw(6) << setprecision(2) << costPerFoot << endl; return 0; }
Electrical and Computer Engineering
of 52
UAH
// HouseCost // This program computes the cost per square foot of
// the house, the number of stories, the size of the
//************************************************************** #include iostream> include < > // For () and setprecision using std;
float WIDTH = 30.0; const LENGTH = 40.0; // Length of the house const float STORIES = 2.5; // Number of full stories const float NON_LIVING_SPACE = 825.0; // Garage, closets, etc. const float PRICE = 150000.0; // Selling price less land
26
UA H CP E 11 2
mai n() { f loa t ; // Tot al sq u are fo ota ge f loa t ; // Liv ing a r ea c os t Per Foo t;
c o ut << << sho wp o int // Set up fl oa t ing pt.
g r oss Foo tag e = gr oss Foo ta g e NO N_ L IVI NG_ SPA CE; c o stP erF oot = PRI CE / ;
<< " C ost pe r s qua re foo t i s " << (6 ) < < s et p rec isi on cos tP e rFo ot << ; r e tur n 0 ; }
El e ct r ic a l a nd Co m put er En g ine er i ng
of 52
CPE 112
s ubs tri ng is fou nd, i t r e tur ns th e p o si t io n wh er e t h em at c hb egi ns, i f n o t f oun d, it re tur ns as pe c ial nu mb e r.
UA H CP E 11 2
I f length looks li ke t hi s: .length()
Electrical and Computer Engineering 29 of 52
U AH CP E1 1 2
Exa mpl e:
Electrical and Computer Engineering 30 of 52
U AH CP E1 1 2
Electrical and Computer Engineering
37 of 52
Electrical and Computer Engineering
38 of 52
El ectrical and Computer Engineering
39 of 52
UAH CPE 112
Electrical and Computer Engineering
40 of 52
UAH CPE 112
myString.substr(5, 20)
Electrical and Computer Engineering 41 of 52
UAH CPE 112
myString.substr(0, 7) “Program”
Electrical and Computer Engineering 42 of 52
UAH CPE 112
myString.substr(0, 7) “Program” myString.substr(7, 8) “ming and”
Electrical and Computer Engineering
43 of 52
myString.substr(0, 7) “Program” myString.substr(7, 8) “ming and” myString.substr(10, 0) “”
Electrical and Computer Engineering
44 of 52
myString.substr(0, 7) “Program” myString.substr(7, 8) “ming and” myString.substr(10, 0) “” myString.substr(24, 40) “Solving”
Electrical and Computer Engineering
45 of 52
UAH CPE 112
myString.substr(0, 7) “Program” myString.substr(7, 8) “ming and” myString.substr(10, 0 “” myString.substr(24, 40) “Solving” myString.substr(40, 24) Error
Electrical and Computer Engineering
46 of 52
UAH CPE 112
// This program demonstrates several string operations //**************************************************************** #include
Electrical and Computer Engineering 47 of 52
UAH CPE 112
Electrical and Computer Engineering 48 of 52
UAH CPE 112
Cone Painting Program
Compute Surface Area of the Cone
Compute Cost for Each Color