Name  ___________________


CPTR246  Spring '02 (100 total points)                                                 Exam 1


1.      One of your classmates notices that you have the following code in your program:


if (!infile){

   cerr << “Input file not found” << endl;

   exit (1);



He asks, “What is that cerr?  I always use cout when I want to display a message to the user.”  Explain to him what cerr is and why it is better to use it in this situation than cout.  (5 points)














2.      You and that same classmate are looking over the shoulder of an upperclassman who is testing a program called turtle.exe.  At the UNIX prompt you see that she types the following:


turtle.exe < file1.dat


Your classmate doesn’t understand what the “< file1.dat” is for.  Explain it to him.  (5 points)











3.      Consider the following class definition.  Using your knowledge of classes and the comments provided, complete the member function definitions. (5 points for the first 3, 20 points for the last)


class WalkAThonParticipant {


         WalkAThonParticipant(string walkersName, int dateOfWalk);

         void AddPledge(string pledgersName, double amountPerMile);

         void RecordMiles(int milesWalked);

         void DisplayCollectionSheet();


         string name;                  // name of the walker

         int walkDate;                 // date of the walk

         int numberOfPledges;          // number of pledges taken

         string pledgers[100];         // name of the pledgers

         double pledgePerMile[100];    // how much they pledged per mile

               // the previous two arrays are “parallel” – that is,

               // the amount that pledger[i] pledged is in pledgePerMile[i]

         int totalMiles;               // total miles walked



WalkAThonParticipant::WalkAThonParticipant(string walkersName, int dateOfWalk){

//  the constructor – we all know what a constructor does!
















void WalkAThonParticipant::AddPledge(string pledgersName, double amountPerMile){

// pre:  none

// post: new pleger’s name and their pledge amount are added to the arrays

// purpose: when the walker gets a pledge, they use this function to add

//             enter the pledger’s name and the amount per mile to the information















void WalkAThonParticipant::RecordMiles(int milesWalked){

// pre:  none

// post: the number of miles walked is recorded

// purpose: once the Walk-A-Thon is over, this function is used to update

//          the object with the number of miles he/she walked







void WalkAThonParticipant::DisplayCollectionSheet(){

// pre:  none

// post: a display of all of those who pledged, how much per mile they pledged

//       and how much they owe is displayed on the screen

// purpose: to give the walker the information that they need to go

//          around and collect all of the pledges



//       begin by clearing the screen

//       put a heading at the top so we know who the walker is and how many

//             miles they walked

//       for each pledger, print out

//             their name (left-justified in a column of width 30)

//             how much they pledged per mile(right-justified in a column

//                   of width 10)

//             how much they owe (right-justified in a column of width 10)

//             remember that some of these are dollar amounts






























4.      You decide to walk in this year’s Walk-A-Thon that will be held on November 22nd of this year.  Define a WalkAThonParticipant object (using the class in the previous question) for yourself. [Use literals for the name (like “Mary Smith”) and date (11222002).]  (4 points)





5.      I decide to pledge $5.00 per mile that you walk.  Write a C++ statement, calling the appropriate member function, to add my pledge to the object that you defined in the previous question. [Use literals for the name (“Eileen Peluso”) and the $5.00.]  (4 points)






6.      Imagine that the user is prompted to enter something at the keyboard.  And suppose that the following definitions have been made:


char str1[40];

int number;

char letter;


The following table contains an input statement in column 1 and what the user entered in column 2.  (Assume that the user hit the enter key after the last character that you see.)  Complete the remaining columns with what the variable contains, what is still in the buffer (write “empty” if there’s nothing in the buffer), and how the flags are set (enter T for true or F for false).  (1 points each)


input statement

user entered

variable contains

buffer contains


cin >> number;






cin >> number;






cin >> number;






cin >> number;






cin.getline(str1, 40);

Dr. Peluso





cin >> str1;

Dr. Peluso





cin.get(str1, 40);

Dr. Peluso





letter = cin.get();

Dr. Peluso






For questions 7 through 9, assume that the following code has been written.


struct studentSchedule{

      int socSecNbr;          // unique student ID number

      string name;            // name of the student

      int numberOfCourses;    // how many courses they have this semester

      string course[6];       // the course codes


studentSchedule Oscar = {123456789, “Oscar”, 4, “CPTR246”, “MATH128”,

                              “SOC110”, “HIST128”};  

studentSchedule allSchedules[1500];



7.      Which of the following statements displays Oscar’s social security number on the screen? (3 points)


a.       cout << Oscar << endl;

b.      cout << Oscar.socSec.Nbr << endl;

c.       cout << Oscar[socSecNbr] << endl;

d.      cout << “socSecNbr” << endl;




8.      Which of the following for loop displays the courses that Oscar is taking? (3 points)


a.       for (int i = 0; i < 6; i++)

cout << course[i] << endl;


b.      for (int i = 0; i < numberOfCourses; i++)

cout << studentSchedule.course[i] << endl;


c.       for (int i = 0; i < numberOfCourses; i++)

cout << Oscar.course[i] << endl;



9.      Which of the following refers to the first course on the schedule of the first student in the array defined above? (3 points)



a.       allSchedules[0].course[0]

b.      allSchedules[1].course[1]

c.       allSchedules[0].course

d.      allSchedules.course[0]



10.  Complete the following function that takes a string as input and returns true if the string contains a valid “product code.”  The rules for a valid product code are (1) it must be exactly 5 characters long and (2) the first 2 characters are letters and the next 3 are digits. (6 points)



bool isValidCode(string code){