160 likes | 292 Views
CSE 143 Section AD. Quiz Section 2. Jeff’s Office Hours. Office: Sieg 226a Office Hours: Tuesday 12:00-1:00pm Wednesday 9:30-10:30am I am COMPLETELY open to meeting with any of you at just about any other time, just email me to set up an appointment!. Lab Cons. Hours (MG 131).
E N D
CSE 143 Section AD Quiz Section 2 Jeff West - Quiz Section 2
Jeff’s Office Hours Office: Sieg 226a Office Hours: Tuesday 12:00-1:00pm Wednesday 9:30-10:30am I am COMPLETELY open to meeting with any of you at just about any other time, just email me to set up an appointment! Jeff West - Quiz Section 2
Lab Cons. Hours (MG 131) Ryan: Mon, Wed, Thu – 12:30-5:30pm Frans: Tue, Thu – 1:00-5:00pm Frances: Mon, Wed, Fri – 9:00-10:40am Tue – 11:00am-12:30pm Fri – 9:00am-12:30pm Andrew: Sat, Sun – 12:30-4:30pm Sat, Sun Hours in OUGL Jeff West - Quiz Section 2
Homework 0 All of the homework 0’s I’ve seen so far were pretty good. The main problems people seemed to have were: • Why isn’t Jeff’s name on the turn-in receipt? • Including multiple files • Looping infinitely • Style Jeff West - Quiz Section 2
Including Multiple Files • In your project, click on Project – Add To Project – New • Click on C++ Source File and write main.cpp in the Filename section. • Repeat for fToC.cpp • Will automatically include both of these files in the project when it compiles. Jeff West - Quiz Section 2
Looping Infinitely Preferred Methods: • while(true) { … } • do { … } while(true); Not as Preferred: • while(1) { … } • int a = 1 while(a) { // a doesn’t change in here … } Jeff West - Quiz Section 2
Style Your best resource for style guidelines will be on the web at: http://www.cs.washington.edu/ education/courses/cse143/CurrentQtr/ homework/style_guide.html I’ll talk more about style & comments on Tuesday after I’ve graded HW0! Jeff West - Quiz Section 2
This Week’s Topics • Bool Type • String Library • Enumerated Types • Structs • Pass by Value vs. Pass by Reference • Streams • File Streams Jeff West - Quiz Section 2
An Incomplete C++ Program Given the “skeleton” for two C++ source files and one C++ header file, let’s design a program which will prompt a user for their name, age, gender, and height (in feet). Use an enumerated type gender to store a user’s gender and a struct Student to keep each user’s information packaged in one place. As soon as the user has input all of their information output it to a file “students.dat” and end the program. Jeff West - Quiz Section 2
student.h // CSE143 Su01 Quiz Section 2 Example // student.h - interface to student data structure and functions // JW 6/01 #include <string> using namespace std; // creates a new datatype “Gender" struct Student { // description of a single student: // name // gender // age // height }; // Prompt a student for their information void promptStudent(Student &aStudent); // Output a student's information to the given file void outputToFile(Student &aStudent, ofstream &outFile); Jeff West - Quiz Section 2
student.h Implemented // CSE143 Su01 Quiz Section 2 Example // student.h - interface to student data structure and functions // JW 6/01 #include <string> using namespace std; enum Gender { MALE, FEMALE }; // creates a new datatype “Gender" struct Student { // description of a single student: string name; // name Gender gender; // gender int age; // age double height; // height }; // Prompt a student for their information void promptStudent(Student &aStudent); // Output a student's information to the given file void outputToFile(Student &aStudent, ofstream &outFile); Jeff West - Quiz Section 2
main.cpp // CSE143 Su01 Quiz Section 2 Example // main.cpp - main program // JW 6/01 #include < > #include < > #include < > using ; #include "student.h" // main program int main() { } Jeff West - Quiz Section 2
main.cpp Implemented // CSE143 Su01 Quiz Section 2 Example // main.cpp - main program // JW 6/01 #include <iostream> #include <fstream> #include <string> using namespace std; #include "student.h" // main program int main() { Student currentStudent; // the student that is inputting their information ofstream outFile("students.dat"); // open the output file and assign it to a stream promptStudent(currentStudent); outputToFile(currentStudent, outFile); return 0; } Jeff West - Quiz Section 2
student.cpp // CSE143 Su01 Quiz Section 2 Example // student.cpp - implementation of Student struct and functions // JW 6/01 #include < > #include < > #include < > using ; #include "student.h" // Prompt a student for their information void promptStudent(Student &aStudent) { … } // Output a student's information to the given file void outputToFile(Student &aStudent, ofstream &outFile) { … } Jeff West - Quiz Section 2
student.cpp - promptStudent // Prompt a student for their information void promptStudent(Student &aStudent) { char maleOrFemale; // temporarily stores student's gender cout << "What is your name? "; cin >> aStudent.name; cout << "What is your gender (M/F)? "; cin >> maleOrFemale; if(maleOrFemale == 'm' || maleOrFemale == 'M') aStudent.gender = MALE; else if(maleOrFemale == 'f' || maleOrFemale == 'F') aStudent.gender = FEMALE; cout << "What is your age? "; cin >> aStudent.age; cout << "What is your height (e.g. 5.75 for 5'9\")? "; cin >> aStudent.height; cout << "Thank you for your cooperation!"; } Jeff West - Quiz Section 2
student.cpp - outputToFile // Output a student's information to the given file void outputToFile(Student &aStudent, ofstream &outFile) { string maleOrFemale; // temporarily stores student's gender switch (aStudent.gender) { case MALE: maleOrFemale = "Male"; break; case FEMALE: maleOrFemale = "Female"; break; } outFile << "Name: " << aStudent.name << endl; outFile << “Gender: " << maleOrFemale << endl; outFile << "Age: " << aStudent.age << endl; outFile << "Height: " << aStudent.height << endl; } Jeff West - Quiz Section 2