Previous Lecture Lecture 3 Next Lecture

Lecture 3, Tue 07/03

User Input, Loops

User Input

Example of interacting with the console using the cin function

#include <iostream>
#include <string>

using namespace std;

int main() {	
// Example receiving a string from the user
	string name;
	cout << "What is your name? ";
	cin >> name;
	cout << "Hello " << name << endl;

	// Example receiving a number from the user
	int i;
	cout << "Enter a number: ";
	cin >> i;
	cout << "The number entered is " << i << endl;

	cout << i / 2 << endl; // what value / type is this if i == 11? 
}

Example of using command line arguments

int main(int argc, char *argv[]) {
#include <iostream>
#include <cstdlib>

using namespace std;

int main(int argc, char *argv[]) {

	cout << "Number of arguments: " << argc << endl;

	cout << argv[0] << endl;
	cout << argv[1] << endl;
	cout << argv[2] << endl;

	// how to use these arguments as numbers?
	// We can convert them using the atoi function
	// in the cstdlib standard library

	int x = atoi(argv[1]) + atoi(argv[2]);
	cout << x << endl;
	return 0;
}

Control Structures: Loops

While Loops

General syntax of a while loop:

while (BOOLEAN_EXPRESSION) {
	// code
	// ...
}
  1. Check if the BOOLEAN_EXPRESSION is true.
    • If true, the statements in loop will execute.
      • at the end of the loop, go back to 1.
    • If false, the statements in the loop will not execute.
      • the program execution after the loop continues.

Example

int i = 0;
while (i < 10)
	cout << "i = " << i << endl;
	// add i++ afterwards to eliminate an infinite loop.
	// i++ → i = i + 1;
	// Remember to enclose this statement with { }

Do-while Loops

do {
	// code
	// ...
} while (BOOLEAN_EXPRESSION);
  1. Execute the code in the loop
  2. Check if BOOLEAN_EXPRESSION is true.
    • If true, then go back to 1.
    • If false, then exit the loop and resume program execution.

Example

int i = 0;
do {
	cout << "i = " << i << endl;
	i++;
} while (i < 0);

For Loop

General syntax of a for loop:

for (INITIALIZATION; BOOLEAN_EXPRESSION; UPDATE) {
	// code
	// ...
}
  1. Execute the INITIALIZATION statement.
  2. Check if BOOLEAN_EXPRESSION is true.
    • if true, execute code in the loop.
      • execute UPDATE statement.
      • Go back to 2.
    • if false, do not execute code in the loop.
      • exit the loop and resume program execution.

Example

for (int i = 0; i < 10; i++) {
	cout << “i = “ << i << endl;
}

Nested Loops

Other loops within a loop can be defined.

Example

for (int i = 0; i < 5; i++) {
	cout << “—“ << endl;
	cout << “i = “ << i << endl;
	for (int j = 0; j < 5; j++) {
		cout << “j = “ << j << endl;
	}
}

Continue and break statements

Example

for (int i = 0; i < 10; i++) {
	if (i == 4)
		continue;
	if (i == 7)
		break;
	cout << “i = “ << i << endl;
}

Formatting output to the terminal

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2); // prints two decimal spaces for floating point values.

Example: A number guessing game

#include <iostream>

using namespace std;

const int ANSWER = 42; // const values cannot be modified

int main(int argc, char *argv[]) {

	int input = 0;

	do {
		cout << "Guess a number between [0 - 100]: ";
		cin >> input;
		if (input == -1)
			break;
		if (input < ANSWER) {
			cout << "Too small" << endl;
			continue;
		}
		if (input > ANSWER) {
			cout << "Too big" << endl;
			continue;
		}
		if (input == ANSWER) {
			cout << "WINNER! ANSWER = " << ANSWER << endl;
			break;
		}
	} while (true);

	cout << "Thanks for playing!" << endl;
}