-
2. What is a preprocessor directive?
2. An example of a preprocessor directive is #include The preprocessor copies code found in the header file iostream into the program.
-
3. What does a compiler do?
3. The compiler translates C++ into machine code.
-
4. At what statement does execution begin?
4. Execution begins at main.
-
3. Why don’t comments increase the program size when the program runs?
3. comments are removed by the compiler
-
3. Indicate if the following are valid variable names a) char
no: is a key word
-
3. Indicate if the following are valid variable names b) 4thTest
no: can't start with a number
-
3. Indicate if the following are valid variable names c) test#1
no: can't use special characters
-
3. Indicate if the following are valid variable names d)overDueAmountThisMonthToDate
yes
-
3. Indicate if the following are valid variable namese) test one
no: can not use spaces
-
5. What values are initially found in the variables when declare?
garbage
-
How many bytes is an int?
4
-
how many bytes is a double?
8
-
how many bytes is a string?
32
-
how many bytes is a char?
1
-
1. Write the statement that will declare a constant called PI initializing it to 3.414
const double PI=3.14;
-
char code[25]={‘a’,’b’,’c’}; What value is stored in the last element?
a space
-
Given int numbers[6]={4,3}; a) Draw the array.
[4,3,0,0,0,0]
-
Given int numbers[6]={4,3}; b) What is the length of the array?
2
-
Given int numbers[6]={4,3}; c) What is the size of the array?
6
-
Given int numbers[6]={4,3}; d) What is the value of numbers[1]?
3
-
Given int numbers[6]={4,3}; e) What is wrong with the statement cin>>numbers[6];
outside array
-
Given int numbers[6]={4,3}; f) Write the statement that will add 2 to the first element in the array.
numbers[0]+=2
-
int grades[8];
what is grade[0]=
garbage
-
int numb[3];
numb[0]=5;
what is numb[1]=
garbage
-
int numb[3]={5};
what is numb[1]=
0
-
how to end a loop with Ctrl+z?
- how do you clear the input buffer for a string entry after an int or double entry?
- cin.ignore(80,'n');
-
2. show the values in the following arrays: a) int numb[5];
garbage, size=5
-
2. show the values in the following arrays: b) int value[5]={40,30,30};
{40,30,30,0,0} size=5
-
2. show the values in the following arrays:c) int numb[]={1,3,4,5};
{1,3,4,5} size =4(auto)
-
2. show the values in the following arrays: d) string names[5]={ "J. Doe", "P. Smith" };
{ "J. Doe", "P. Smith",,, }
-
2. show the values in the following arrays:e) char code[3]={ '1'};
{ '1',' ',' '}
-
2. show the values in the following arrays:f) double width[]={1.9,3.9,4.9,5.0};
{1.9,3.9,4.9,5.0}
-
what happens with for(;;)
infinite loop
-
for (int ctr = 0; ctr<5; ctr++) //if declare ctr in for loop it can’t be used after loop T/F
T
-
for (int ctr = 0; ctr<5; ctr++)
cout << ctr << endl;
cout << ctr << endl; //is okay? T/F
F
-
for (int ctr = 0; ctr<5; ctr++)
cout << ctr << endl; //is okay? T/F
T, as long as ctr is not used outside the loop
-
cout<<"Enter a number between 1 and 100 code:";
- cin>>numb;
- while (cin.fail()||numb<0||numb>1000)
- {
- cin.clear();
- cin.ignore(80,'n');
- cout<<"re-enter";
- cin >> num
- }
- cin.clear(80,'n') //in cin for text later
-
Validate a product code.
- string productCode;
- cout << "enter a product code";
- while (productCode.length() == 0)
- {
- cout << "You did not enter a product code." << endl;
- cout << "Enter a product code: ";
- getline(cin, productCode);
- }
-
how to open a file.
- ofstream fout("products.dat");
- if (!fout.is_open())
- {
- cout << "The file products.dat could not open - need to fix" << endl;
- system("pause");
- exit(666);
- }
- how to send file to console
- system("type products.dat");
-
beginning of file:
- #include <iostream>
- #include <string>
- #include <iomanip>
- #include <fstream>
- using namespace std
-
code for decimals:
fout << fixed << setprecision(2); // or cout
-
cout << "Enter a product code (or ctrl Z to end): ";
- getline(cin, productCode);
- while (!cin.eof())
- {...}
|
|