Вы находитесь на странице: 1из 8

COMP 218 - Fundamentals of Programming

Tutorial 3 – Built in Data Types II


Question 1: Type conversion
What is the float a = 10.5;
output generated int b = 10, d;
by the following char c = 'b'; //ASCII of b=98
statements?
a += c;
cout <<"a = "<< a << endl;
d = c / b;
cout << "d = " << d << endl;
a = c / b;
cout << "a = " << a << endl;
a = float(c) / b;
cout << "a = " << a << endl;
Question 2: Character input using cin
The following statements read in character variables using the
cin operator and then outputs them to screen.

char char1, char2, char3, char4;


cout << "Input four characters."
<< " Press Return." << endl;
cin >> char1 >> char2 >> char3 >> char4;
cout << char1 << char2 << char3 << char4;

What will the output be for each input data?


abcd
a b c d
1b2c
31 45
Question 3: Character input using cin.get
The following statements read in character variables using the
cin.get operator and then outputs them to screen.

char char1, char2, char3, char4;


cout << "Input four characters."
abcd
<< " Press Return." << endl;
a b c d
cin.get(char1);
cin.get(char2); 1b2c
cin.get(char3); 31 45
cin.get(char4);
cout << char1 << char2 << char3 << char4;

Again what will the output be for each input data?


Question 4: Output statements format:
Record what you think is written by each of the output statements
const int INT_NUMBER = 106;
const float FLT_NUMBER = 3.14;
float fltValue;
int intValue;
intValue = INT_NUMBER + FLT_NUMBER;
fltValue = float(INT_NUMBER) + FLT_NUMBER;
cout << INT_NUMBER << endl;
cout << intValue << endl;
Solution
cout << setw(7) << intValue;
cout << setw(7) << intValue << intValue /10 << endl;
cout << setw(7) << fltValue << endl;
//cout << fixed << showpoint;
cout << setprecision(7) << fltValue << endl;
cout << setw(7) << setprecision(3) << fltValue << endl;
cout << fltValue << endl;
cout << intValue << setw(3) << intValue << setw(7) << intValue
<< endl;
Question 5: String operators
Use the following program shell to answer the questions
below.

// String program
#include <iostream>
#include <string>
using namespace std;
int main()
{
return 0;
}

- Write a named string constant made up of your first and


last name with a blank in between. Write the statements to
print out the result of applying length and size to your
named constant object. Compile and run your program.
Question 5: String operators (continued)

- Write the statements necessary to print your name, last


name first, followed by a comma and your first name. Use
function substr to accomplish this task. Compile and run
your program.

- Write the statements necessary to print your last name,


followed by a comma and your first initial. Compile and run
your program.

Вам также может понравиться