|
ex1-1.cpp - Example 1-1 First example |
// File: Ex1-1.cpp
// This example illustrates some of the basic differences of C and C++:
// Comments
// cin and cout for input and output
// declarations of variables almost anywhere
// use of type bool
#include <iostream> // instead of <stdio.h> or <iostream.h>
using namespace std;
/* You can still use the old comment, */
/* but you must be // very careful
about mixing them */
// Your best bet is to use this style for 1 line or partial line comments
/* And use this style when your comment
consists of multiple lines */
int main (void)
{
cout << "hey"; // display a message, Why won't printf or puts work here?
//printf("hey");
//puts("hey"); // Can you use printf or puts in a C++ program?
for (int k = 1; k < 5; k++) // declare a variable when you need it
{
cout << k;
}
//cout << k;
cout << endl; // print a carriage return (newline)
cout << "Please enter your name => ";
char name[10]; // I feel like declaring a variable
cin >> name;
cout << "Hey " << name << ", nice name." << endl;
cout << endl; // blank line
cout << "Hey " << name << ", how old are you? ";
int age; // Declare another variable
cin >> age;
bool IsOld = age > 35;
bool IsYoung = !IsOld;
cout << IsOld << ' ' << IsYoung << endl;
if (IsOld) cout << name << ", you don't really look that old!\n";
char dogs_name[10];
int cats;
cout << "What's your dog's name and how many cats do you have? "
<< endl;
cin >> dogs_name >> cats;
cout << "I'll bet " << dogs_name << " is a good dog and your "
<< cats << " cat" << (cats>1?"s are":" is") << " nice too\n";
{
// This is a block
int x = 5; // x is local to this block
cout << x;
}
// cout << x; What would happen if you tried to print x now?
return 0;
}
CIS27: Programming in C++ Instructor: Joe Bentley