//=========================================================== // // Simple example program, illustrating how to read integers from // the input and from the command line. // // Margaret Fleck // 21 Jan 98 // // to compile // g++ -Wall -o simple simple.C // // to run, give it an integer argument, e.g. // ./simple 5 // ./simple 13 // etc // // When program runs, it asks you to input a single character. // (Type ENTER after the character.) It will then print // a block of characters whose width and height are determined // by the command-line parameter, surrounded by an ASCII box. // //=========================================================== #include // for I/O #include // for atoi #include // for assert // prints multiple copies of a character // does not return any value (void declaration) void print_N(char output_character, int number_of_repeats) { int i; for (i = 0; i < number_of_repeats; i++){ cout << output_character;} } main(int argc,char *argv[]) // argc is the number of items on the command line. // argv is an array of strings, one for each item on the command line { int input_width,i; // integer variables char input_character; // a character variable // argc is the number of items on the command line. // The first (argv[0]) is the command name (simple) // and then there is one real argument. So argc // should be 2. assert(argc == 2); // the first command-line input (argv[1]) is a ASCII string // containing the digits of an integer. atoi translates // this string into an integer. input_width = atoi(argv[1]); // check that input number is valid assert (input_width > 0); // get character to put into box cout << "Type input character:"; cin >> input_character; // print top row of box print_N('-',2+input_width); cout << endl; // print interior characters and sides of box for (i = 0; i < input_width; i++){ cout << '|'; print_N(input_character,input_width); cout << '|'; cout << endl;} // print bottom row of box print_N('-',2+input_width); cout << endl; }