To gain experience in
#include <iostream>
using namespace std;
int main()
{
int junk_food;
cout << "Do you want ding-dongs or ho-hos:"
<< "(1 for ding-dongs, 0 for ho-hos)"
<< endl;
cin >> junk_food;
if ( junk_food == 1)
{
cout << "Have some ding-dongs!" << endl;
}
if ( junk_food == 0)
{
cout << "Have some ho-ho's!" << endl;
}
return 0;
}
In this case the computer actually made a decision as to what to print based on the user input.
Notice that the structure of an if statement is fairly close to English. However take particular notice of the == operator. It's different from the = operator which assigns values. The == operator simply checks whether two values are equal. Sometimes it's read "is equal". Compile and run this program.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string password;
cout << "What's your password?" << endl;
cin >> password;
//the password can't have spaces since we are not using getline()
if (password == "blood_sucker")
{
cout << "You typed the correct password. "
<< "Welcome to Super Evil Enterprizes!" << endl;
}
else
{
cout << "You are human. We will destroy you!!" << endl;
}
return 0;
}
Change the above to some other kind of conditions and show the TA. Note that using if-else statements can simplify the code since you don't have to have two if statements.
Write a program that asks the user to choose where they want to go. Let there be two choices, the moon and mars. If they chose mars, ask them for their weight and then output their weight times (377/1000). If they chose the moon ask them for their weight and output their weight times (1/6). Test this with a weight of 100. Remember to be careful about integer vs. floating-point division. Have the TA check the results and your code.
Write a graphics program that asks the user to click on 2 points within the window. Then draw a rectangle with these points being opposite corners of each other. If the user chooses points that have either the same x coordinate or the same y coordinate (this would cause a horizontal or vertical line instead of a rectangle), output an error message and exit the program, otherwise, draw the rectangle.
For example, if the user clicks on points with the coordinates (1,2) and (4,0), then the 4 corners (Points) of the rectangle would be (1,2), (1,0), (4,0), and (4,2).
While statements are another way to teach the computer to think. In this case, the computer must decide whether to repeat certain statements or not. The following program demonstrates one use for the while statement.
This first example does not use a while statement:
/* PURPOSE: Count number of digits needed to express an integer in base 10
using multiple if statements
*/
#include <iostream>
using namespace std;
int main()
{
int input;
cout << "Input an integer between 1 and 9999: ";
cin >> input;
int temp = input;
int number_of_digits = 1;
if (temp > 9)
{
temp = temp / 10;
number_of_digits++;
}
if (temp > 9)
{
temp = temp / 10;
number_of_digits++;
}
if (temp > 9)
{
temp = temp / 10;
number_of_digits++;
}
if (temp > 9)
{
temp = temp / 10;
number_of_digits++;
}
cout << input << " can be expressed in "
<< number_of_digits << " digits" << "\n";
return 0;
}
But having to write
if (temp > 9)
{
temp = temp / 10;
number_of_digits++;
}
four times, even using copy/paste, is clearly repetitive! It also only works for input <= 9999. One would like to have a way of testing that the input is still greater than 9, and executing the succeeding control block if it is. Replacing if with while does it.
/* PURPOSE: Count number of digits needed to express an integer in base 10
using a while loop
*/
#include <iostream>
using namespace std;
int main()
{
int input;
cout << "Input an integer: ";
cin >> input;
int number_of_digits = 1;
int temp = input;
while (temp > 9)
{
temp = temp / 10;
number_of_digits++;
}
cout << input << " can be expressed in "
<< number_of_digits << " digits" << "\n";
}
Write a program that asks the user for a positive integer. The program should then output all the integers between and including the number input by the user and -5.
For example, if the user enters the value 3, the output would be:
3, 2, 1, 0, -1, -2, -3, -4, -5
Write a program that asks the user to enter as many positive integers as they want. When they are done, they should type the sentinal value, -1. Your program should then output the average of these numbers, not including the -1.
For example, if the user enters the values:
100 25 75 200 -1
the output should be:
Avg: 100