Uncategorized

CIT-120 Week 2 Notes – Writing Your First Program

Added by on September 14th, 2008, filed under Uncategorized

This week was slightly more challenging with the introduction of some logic challenges in the form of two games called “Mastermind” and “Cross the River”. The classwork portion consisted of a quiz over last weeks content, the cin operator, writing your first program, and cin.ignore().

Visual C++ Student Edition

We also received a copy of the student edition of Visual C++. In case you lose your copy and/or need to reinstall, you can download it here.

Class Schedule

Professor Delta teaches the same class at various times throughout the week so if you miss a class, feel free to attend one of these to get back up to speed.

  • M,W – 11:30 – 12:45
  • M – 1 – 2:15
  • T,TH – 11:30 – 12:45
  • M,W – 11:30 – 12:45

Notes

Class started off with a few character examples. If you want to cout a character, use single quotes – pretty simple.

cout << 'a'; // Displays character 'a'
cout << a; // Displays var a
cout << "a"; //Displays string a

A few more examples ...

cout << 'ab'; // Won't compile
cout << 'a' << 'b'; // Displays a and then b
cout << 'n'; //Will work and display newline

Remember: newline characters may be made up of two characters, but they're viewed as one character by the compiler - thus the single quotes work fine. Basically, anything with an ascii value is a character.

Writing Your First Program

At the top of every program we turn in, Professor Delta has requested the following 4 comments at the top:

// What the program does
// Name of the assignment
// Author Name
// Last Date Modified

For each assignment, we should create a new project and then a new c++ source file.

To create a new project:

  • Open Visual C++
  • Go to File
  • New
  • In the projects tab, choose Win32 Console workspace

Once created, go to File->New->Files Tab->C++ Source File. Make sure the source file is located within the source folder of the workspace.

Remember: to reopen your workspace, click on the .dsw file in the save folder. Also, don't forget to change the build type to "release" as that is what the Professor prefers for turning in assignments.

To auto-format code:

  • CTRL+A to copy all text
  • Go to Edit
  • Advanced
  • Format Selection

To compile and execute:

  • CTRL+F5

cin.ignore();
cin is a stream object and the .ignore() is a method of that objects which ignores the previously input characters in memory. If you want your program to pause at the end, you need to use cin.ignore(); twice - one to ignore the newline characters that was submitted in the last input, and one to wait for the next command to receive (that's what causes the pause).

Pulling it all together
So the first program was pretty simply - it was to output our names. The second program should ask for two integers and then output their sum. Notice the commenting at the beginning, the use of cin.ignore(), and lastly how the sum of a and b was outputted.

// Asks for two integers and sums them up
// Lab 2
// Joshua McGinnis
// 9/13/08

#include ;

void main() {
	int a, b;

	cout << "Please give me an integer: ";
	cin >> a;

	cout << "nnPlease give me another integer: ";
	cin >> b;

	cout << "nnThe sum of " << a << " and " << b << " is " << a + b << ". ";
	cout << "nPlease press ANY KEY to exit the program";
	cin.ignore();
	cin.ignore();
}

Make sure to hit OK when it comes to creating the file for the first time or overwriting existing files.

Homework

This weeks homework consisted of doing two logic-based games, "Cross the River" and "Mastermind".

Cross the River Game

The object of the game is to get everyone across the river in as few moves as possible while obeying the rules. If you need to start over the game, you have to refresh.

Click on the person you want to step on or off the boat. To move the boat click on the red lever.

This was a pre-employment test for a Japanese company. Theoretically a "normal" person should be able to solve it in 15 mins.

Here are the rules:

  1. Only one or two people in the raft at a time
  2. Dad can not be with a girl without the presence of the mother
  3. Mom can not stay with a boy without the presence of the dad
  4. The thief can not be with any other person without the presence of the police officer
  5. The only ones who can drive the raft are the parents and the police officer

Looking for the solution?

Enjoy this Post?

Spread the word by promoting this post on FaceBook and Twitter.

What do you think?