150 likes | 162 Views
Important information and tips for students in CSE 143 Section AD Quiz Section 3, including signing the sheet, upcoming quiz, getting help, homework issues, commenting, C++ strings, strtok function, and student statistics.
E N D
CSE 143 Section AD Quiz Section 3 Jeff West - Quiz Section 3
Administrative “Stuff” • Please sign the sheet that is coming around if: - You are not yet registered for 143. - You are registered in a different sec. • There will be a quiz this Thursday! Anything through tomorrow’s lecture is fair game! Jeff West - Quiz Section 3
Getting Help I want to help you guys as much as I can, but here are a few pointers on how to get as much help as possible: • Feel free to use any TA’s office hours. • Use the newsgroup! • If you come to my office hour, it would be very helpful if you could bring an idea of how your program will be structured with you – napkin scratchings are better than nothing • If your program is almost done with small errors, it will be most effective if you bring a copy of your source files with you to my office. Jeff West - Quiz Section 3
Homework 0 Issues • Specification asks for an infinite loop! Make it clear that you intended for the loop to be infinite, either by “while(true)” or by commenting that it is an infinite loop. • Comment -- especially at the top of each file and after each variable is declared! • Use C++ strings as much as possible! • Use bool types where it is natural – example, while(true) is more natural than while(1)! • Use meaningful names for variables – example, double fahrenheit, celsius not double x, y! Jeff West - Quiz Section 3
Commenting • At the top of each file you should include your name, id number, section, and instructor… for example: // main.cpp – CSE assignment 1, Summer 2001 // Y. Name, id #9999999, section AD, B. Tjaden • In long functions variables should have meaningful names! For example, use fahrenheit and celsius, not x and y! • The best resource for style guidelines is the style guidelines section of the webpage! You will be graded on style, so do pay attention to it! Jeff West - Quiz Section 3
C Strings vs. C++ Strings C++ strings operate in a very natural manner when you are used to other datatypes (ints, doubles, etc.) string color1, color2; color1 = “red”; color2 = color1; if(color1 == color2) { cout << “This is true”; } Jeff West - Quiz Section 3
C Strings May Still Come Up… In this week’s homework assignment, it was suggested that you may wish to use the “strtok” function. This function specifically brings in a C-String to break apart and a C-String of dilimiters, and returns a C-String! This function will not let you send in C++ strings! Jeff West - Quiz Section 3
strtok • Learn to use the Help section on the toolbar if you are using MSVC++! If you look up strtok, it says: char*strtok(char*strToken,constchar*strDelimit); On the handout you will see the example of using this function that the MSDN Library provides. Jeff West - Quiz Section 3
MSDN’s strtok Example #include <string.h> #include <stdio.h> char string[] = "A string\tof ,,tokens\nand some more tokens"; char seps[] = " ,\t\n"; char *token; void main( void ) { printf( "%s\n\nTokens:\n", string ); /* Establish string and get the first token: */ token = strtok( string, seps ); while( token != NULL ) { /* While there are tokens in "string" */ printf( " %s\n", token ); /* Get next token: */ token = strtok( NULL, seps ); } } Jeff West - Quiz Section 3
Output According to MSDN A string of ,,tokens and some more tokens Tokens: A string of tokens and some more tokens Jeff West - Quiz Section 3
Student Statistics! You are given last week’s student.h header file and told that the promptStudent function correctly prompts a student for their information and stores it in the student variable that is passed in. Make a program which will prompt student’s for their information until the height input is 0.0. The student who’s height is 0.0 does NOT count as a valid student. At this point, print to the screen: “There are w males and x females in the class.” “The oldest student in the class is y at age z.” You do not have to account for multiple students of the same age. Jeff West - Quiz Section 3
student.h // CSE143 Su01 Quiz Section 3 Example // student.h - interface to student data structure and functions // JW 6/01 #include <string> #include <iostream> using namespace std; enum Gender { MALE, FEMALE, NA }; // 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); More prototypes? Maybe, // Initializes a student so that its information is obviously void! void initializeStudent(Student &aStudent); Jeff West - Quiz Section 3
main.cpp (1) // CSE143 Su01 Quiz Section 3 Example // main.cpp - main program // JW 6/01 #include "student.h" // main program int main() { Student currentStudent; // the student that is inputting // their information Student oldestStudent; // oldest student in the class int males = 0, // the numbers of males and females = 0; // females in the class // initialize oldest student initializeStudent(oldestStudent); promptStudent(currentStudent); Jeff West - Quiz Section 3
main.cpp (2) while(currentStudent.height != 0.0) { // account for student's gender if(currentStudent.gender == MALE) males++; else if(currentStudent.gender == FEMALE) females++; // if student is the oldest student, set variables accordingly if(currentStudent.age > oldestStudent.age) oldestStudent = currentStudent; promptStudent(currentStudent); } cout << "There are " << males << " males and " << females << " females in the class." << endl; cout << "The oldest student in the class is " << oldestStudent.name << " at age " << oldestStudent.age << '.' << endl; return 0; } Jeff West - Quiz Section 3
student.cpp -- initializeStudent // Initializes a student so that its information is obviously void! void initializeStudent(Student &aStudent) { aStudent.name = "Nobody"; aStudent.gender = NA; aStudent.age = 0; aStudent.height = 0.0; } Jeff West - Quiz Section 3