90 likes | 107 Views
Learn about essential types like int, char, bool, and more in C++. Explore arrays, strings, and file I/O concepts with practical exercises.
E N D
Built-In and Simple User Defined Types in C++ • int, long, short, char (signed, integer division) • unsigned versions too unsigned int, unsigned long, etc. • C++ guarantees a char is one byte in size • Sizes of other types are platform dependent • Can determine using sizeof() , <climits> INT_MAX • float, double (floating point division) • More expensive in space and time • Useful when you need to describe continuous quantities • bool • Logic type, takes on values true, false • enumerations enum primary_colors {red, blue, yellow}; • Exercise: enumerate truth, character, string, decimal
C-style Strings • Arrays of characters “hello, world!” // 14 positions w/ ‘\0’ char greeting[6] = “hello”; char *audience = “world”; • Can reference specific positions as characters • Useful for some input checking tasks isalnum (greeting[i]) // array syntax isdigit (audience + i) // pointer syntax • Can also reference as an entire string • Using functions found in the <cstring> library if (strcmp (s, “hello”) == 0){...} if (strlen (s) == 1){...} • Exercise: check argv[1] for “true” or “false” • Exercise: check argv[1] for single character input
Program Argument Checking • Character based checking • There are also useful functions in the <cctype> library • See page 249 of Prata C++ Primer Plus, 5th Ed. isdigit() // in ‘0’to ‘9’(decimal digit) isxdigit() // in ‘0’to ‘9’, ‘A’ to ‘F’, // or ‘a’ to ‘f’ (hexadecimal digit) isalpha() // in ‘A’ to ‘Z’ or ‘a’ to ‘z’ (letter) islower() // in ‘a’ to ‘z’ (lowercase letter) isupper() // in ‘A’ to ‘Z’ (uppercase letter) isalnum() // alphanumeric (letter or decimal digit) ispunct() // punctuation character isblank() // blank (space or horizontal tab) • Exercise: check whether any of the strings passed by argv represent unsigned decimal integers • we’ll allow leading zeroes but not a + or – • if so, print them on separate lines using cout
#include <iostream> #include <string> using namespace std; int main (int, char*[]) { string s; // empty s = “”; // empty s = “hello”; s += “, ”; s = s + “world!”; cout << s << endl; return 0; } <string> header file Various constructors Assignment operator Overloaded operators += + < >= == [] The last one is really useful: indexes string if (s[0] == ‘h’) … C++ string Class
#include <string> #include <cstring> // strcmp ... using namespace std; int main (int, char*[]) { char * w = “world”; string sw = “world”; char * h = “hello, ”; string sh = “hello, ”; cout << h << endl; cout << sh << endl; sh += sw; // cannot say h += w; cout << sh << endl; return 0; } C-style strings are contiguous arrays of char Often accessed through pointers to char (char *) C++ string class (template) provides a rich set of overloaded operators Often C++ strings do “what you would expect” as a programmer Often C-style strings do “what you would expect” as a machine designer In this course we’ll focus on the programmer role Using C++ vs. C-style Strings
#include <iostream> using namespace std; int main (int, char*[]) { int i; // cout == std ostream cout << “how many?” << endl; // cin == std istream cin >> i; cout << “You said ” << i << “.” << endl; return 0; } <iostream> header file Use istream for input Use ostream for output Overloaded operators << ostream insertion operator >> istream extraction operator Other methods ostream: write, put istream: get, eof, good, clear Stream manipulators ostream: flush, endl, setwidth, setprecision, hex, boolalpha C++ Input/Output Stream Classes
#include <fstream> using namespace std; int main () { ifstream ifs; ifs.open (“in.txt”); ofstream ofs (“out.txt”); if (ifs.is_open () && ofs.is_open ()) { int i; ifs >> i; ofs << i; } ifs.close (); ofs.close (); return 0; } <fstream> header file Use ifstream for input Use ofstream for output Other methods open, is_open, close getline seekg, seekp File modes in, out, ate, app, trunc, binary C++ File I/O Stream Classes
#include <iostream> #include <fstream> #include <sstream> using namespace std; int main () { ifstream ifs (“in.txt”); if (ifs.is_open ()) { string line_1, word_1; getline (ifs, line_1); istringstream iss (line_1); iss >> word_1; cout << word_1 << endl; } return 0; } <sstream> header file Use istringstream for input Use ostringstream for output Useful for scanning input Get a line from file into string Wrap string in a stream Pull words off the stream Useful for formatting output Use string as format buffer Wrap string in a stream Push formatted values into stream Output formatted string to file C++ String Stream Classes
#include <string> #include <cstring> #include <sstream> using namespace std; int main (int argc, char *argv[]) { if (argc < 3) return 1; ostringstream argsout; argsout << argv[1] << “ ” << argv[2]; istringstream argsin (argsout.str()); float f,g; argsin >> f; argsin >> g; cout << f << “ / ” << g << “ is ” << f/g << endl; return 0; } Program gets arguments as C-style strings But let’s say we wanted to input floating point values from the command line Formatting is tedious and error-prone in C-style strings (sprintf etc.) iostream formatting is friendly Exercise: check whether any of the strings passed by argv are unsigned decimal integers (leading zeroes still ok) print their sum if there are any otherwise print the value 0 Using C++ String Stream Classes