// Demonstrate more various high-level syntax for the C language // ECE2036 // George F. Riley, Georgia Tech, Fall 2012 // Include the C++ output library "iostream" #include // Illustrate the definition of a C/C++ "enumeration type" // This simply defines several symbols that can be referred to by // name in the program, and the symbols are given unique integer // identifiers. Not this DOES NOT create a variable, but rather // defines a type. We will use this type later in the "switch" // example. typedef enum { Yugo, Ford, Chevrolet, Chrysler, Toyota, Honda, Mercedes, Ferrari } CarModels_t; // Define the MyRec structure (from handout 1) typedef struct { // myStruct has three subvariables (or components). // Note the components can be different types, but don't have to be int a; char b; double c; } myStruct; // Implement a PriceModel function that uses a switch statement // to select one of several alternatives basd on the enumeration // type argument passed in as an argument, and return the approximate // cost of the model double PriceModel(CarModels_t model) { // The switch statement selects one of a list of alternatives double cost; switch (model) { case Yugo: cost = 2000.00; // Cost for the Yugo break; // break is needed, otherwise "falls through" to Ford case Ford: cost = 18000.00; // Cost for the Ford break; case Chevrolet: cost = 19000.00; break; case Chrysler: cost = 19500.00; break; case Toyota: cost = 20000.00; break; case Honda: cost = 20000.00; break; case Mercedes: cost = 50000.00; break; case Ferrari: cost = 150000.00; break; } return cost; } // Implement a "void" function called "Print1". The "void" return type // simply says the function doe not return any value. // In this example, we pass a pointer to a character array // that is terminated with the '\0' character. // This also demonstrates the use of the de-referencing operator // and the pointer increment (++) // // pStart is a pointer variable (see the * after the "char" type // that points to a string of characters. This functions prints // each character in turn. void Print1 (char* pStart) { // Loop until the end of string character is found while(*pStart != '\0') { // If not the end of string, print the character AND increment // the pointer variable. std::cout << *pStart++; } } int main(int argc, char** argv) { // argc is the count of the number of command line arguments // and argv is the pointer to the array of arguments // Use Print1 to print each argument for (int i = 0; i < argc; i++) { Print1(argv[i]); // Note the function call with no return value std::cout << '\n'; // and print the end of line } // Call the PriceModel function for several models std::cout << "Cost of Ford is " << PriceModel(Ford) << std::endl; std::cout << "Cost of Honda is " << PriceModel(Honda) << std::endl; std::cout << "Cost of Mercedes is " << PriceModel(Mercedes) << std::endl; std::cout << "Cost of Ferrari is " << PriceModel(Ferrari) << std::endl; // Illustrate the if/else construct double hondaCost = PriceModel(Honda); if (hondaCost > 15000) { // Cost is more than $15000 std::cout << "Oops, the honda is too expensive" << std::endl; } else { // Buy it std::cout<< "Ok, I'll take it" << std::endl; } // In the above, the "else" is optional. Often we have an "if" statement // that takes some action if something is true, and no action if not. // Illustrate the "sizeof" operator std::cout << "sizeof int is " << sizeof(int) << std::endl; std::cout << "sizeof char is " << sizeof(char) << std::endl; std::cout << "sizeof float is " << sizeof(float) << std::endl; std::cout << "sizeof double is " << sizeof(double) << std::endl; std::cout << "sizeof myStruct is " << sizeof(myStruct) << std::endl; }