PURPOSE: The purpose of this lab is to acquaint you with the C++

advertisement
CS1600 Lab Assignment 1 – Spring 2016
Due: Feb. 2, 2016
POINTS: 10
PURPOSE: The purpose of this lab is to acquaint you with the C++ programming
environment on storm.
PROCEDURES: You will use Unix/Linux environment to edit, compile and run your first C++
program. After you finish, print the result.
Practice Lab:
A. Your first C++ program
You will finish this task on a computer in JMH330
1. Log in a computer in JMH 330 as student.
2. On the left hand side the Graphic User Interface of Fedora, click Activies, then click Terminal. A
black terminal window should pop up on the desktop.
3. Let us log in your storm account now with your username (on your account ID sheet)
$ssh username@hostname
For example, my username is yli and our hostname is storm.cis.fordham.edu, I will log in
my storm account as $ssh yli@storm.cis.fordham.edu.When you are asked about the
authenticity of the host, say yes. Then type your password when you are asked for it.
If you want to change your password, you could use command passwd after you log in.
4. You should be in your home directory. To confirm it, type the following command after the command
prompt $, then hit Enter key.
$pwd
/home/staff/yli
$
5. Create a directory for CISC1600 in your home directory. And go to the directory you just created.
$mkdir CISC1600
$cd CISC1600
6. Now let's create your first C++ program helloCPlusPlus.cpp with the vi editor.
$vi helloCPlusPlus.cpp
7. A new file named as “helloCPlusPlus.cpp” is opened in front of you. vi has two modes,
INSERTION mode, and COMMAND mode. The editor begins in COMMAND mode. You can
press i or a key to get into INSERTION mood. You could press Esc key to return to COMMAND
mood at any time. Type the following program in the created file with your own information:
//
//
//
//
File name: helloCPlusPlus.cpp
Author: your name
Date:
Course: CISC1600
 Put your name here
 Put today’s date here
#include <iostream>
int main()
{
std::cout << "Hello World. \n This is my first C++ Program" << std::endl;
return 0;
}//end of program
8. After you finish typing, save the program:
a. First, switch from INSERT mode to COMMAND mode of vi by hitting the Esc key.
b. Then, type :x and Enter key to save the typed file.
c. If you do not want to save the typed file, type :q! Instead.
9. Check whether you created and saved your first C++ file successfully by doing
$ls
The file name “helloCPlusPlus.cpp” should be shown in the current directory
10. Compile the program by typing the following command after the command prompt:
$g++ helloCPlusPlus.cpp –o helloCPlusPlus.out
11. In case your program responds with some errors (compilation errors) you will need to compare your
program with the one given and make the necessary corrections. Every character and its case counts!
Usually the error message shows which line has a problem, such as
helloCPlusPlus.cpp:10:20: error: ‘Name’ was not … in this scope
12. If you need to correct some typos in the saved file helloCPlusPlus.cpp, re-open vi editor by
typing the following command, which open an existing file:
$vi helloCPlusPlus.cpp
13. If you know exactly which line has the error, modify that line directly. For example, line 10 has an
error, after opening the file, type :10 in COMMAND mode and Enter key, the cursor will be moved
to line 10; then you should switch to INSERT mood to modify your code by typing i or a key.
14. If you compile your program successfully, you can find that the executable
helloCPlusPlus.out is in directory CISC1600. Type the following command:
file
$ls
helloCPlusPlus.cpp helloCPlusPlus.out
15. In directory CISC1600, run the program by typing the following command after the command
prompt:
$./helloCPlusPlus.out
16. Let's edit this file by adding more lines before the end of the program.
int main()
{
std::cout << "Hello World. \n This is my first C++ Program" << std::endl;
std:cout<< “What is your age?”<<std::endl;
int age;
std::cin >> age;
std::cout<< “I know you are ”<< Age <<” years old! ”<<std::endl;
return 0;
}// end of program
Save the updated file by following Step 8. When you compile the updated code, do you get any
error message?
17.
18. If you get an error message, reopen your file with vi, go to the line with the error directly, modify your
code.
19. Then save your file and re-compile your file.
20. Then run your program by following Step 14 & Step 15.
---------------------Save your file on your USB device. ------------------------------------------------21. Plug in your USB device.
22. On the left hand side of the Window, click Activities, then click Files. A window will open with
Devices showing System and Your USB device on the left hand side.
23. Copy your first C++ file helloCPlusPlus.cpp into your USB device.
1. Transfer your file from your storm account by using software – FileZilla. Type the information
about hostname (storm.cis.fordham.edu), username (id), password, and choose port 22.
2. Left hand side window is your local computer, right hand side window is your storm account.
3. On the right hand side, go to click CISC1600 folder and find your first C++ program:
helloCPlusPlus.cpp.
4. On the left hand side, go to find your USB drive (depends on which USB port you use).
5. Drag your file from your right hand side window and drop it to your right hand side window.
------------------Or you can email yourself a copy of the code from the local computer --------------------
B. Your second C++ program
1. Create a file named LabOne.cpp with vi in your CISC1600 directory. Compile and run the
file by following instructions in the previous part.
//
//
//
//
File name: LabOne.cpp
Author: your name
Date:
Course: CISC1600
 Put your name here
 Put today’s date here
#include <iostream>
int main()
{
//variable declarations
int integerNumber;
double doubleNumber;
double sum;
std::cout << "Hello World. \n This is my first C++ Program" << std::endl;
//ask user give an integer number and show it to the user
std::cout << "Enter any integer (1 or 2 or ... up to 9999):" << std::endl;
std::cin >> integerNumber;
std::cout << "You entered the integer " << integerNumber << std::endl;
//ask user give a floating point number and show it to the user
std::cout << "Enter any floating point number \n"
<< "(any number with a decimal point): " << std::endl;
std::cin >> doubleNumber;
std::cout << "You entered the floating point number "
<< doubleNumber << std::endl;
sum = integerNumber + doubleNumber; //add two numbers
std::cout << "The sum of the integer and the floating point number is "
<< sum << std::endl;
return 0;
}//end of the program
2. The compiling command should be :
$g++ LabOne.cpp –o LabOne.out
3. In directory CISC1600, run the program by typing the following command after the
command prompt:
$./LabOne.out
Follow the prompts of the program, and enter an integer and a floating point number when
you are asked.
4. Scripting your file :
a. Type the following command after the command prompt (pick any file name you like
for your result)
$script your_result_file_name
b. Show your program LabOne.cpp on screen, the content will be saved in the script file.
$cat LabOne.cpp
c. Repeat Step 3, again the content on the screen will be saved in the script file
d. After you have finished, type the following command:
$exit
5. Again, you can check whether the result file is created by using ls command. You can use vi
to open the result file and check the content.
6. Move your C++ program and the result file to local disk/USB device by following steps in
previous part.
SUBMISSIONS: Email me the result file (yli@fordham.edu).
Download