#include "stdio.h" #include "stdlib.h" //including a new library today, for rand function #include //another new one, for file input and output operations #include #include "math.h" #include "string.h" //another new one using namespace std; int main ( int argc, char** argv ) { //today's lesson #1: for and while loops for ( int i = 0; i < 100; i++ ) { //create variable integer i and initialize to 0 cout << i << endl; //can do many other things; can have multiple lines } srand(atoi(argv[1])); //initialize the random number seed long randomNumber = rand(); //long is a bigger type of integer than int is while ( randomNumber > long(1e8) ) { randomNumber = rand(); //keep on overwriting the initial value } cout << randomNumber << endl; //lesson #2: how to write output to a text file ofstream myfile; //much easier than in C myfile.open("example_output.dat",ios::app); //must open and name it (myfile is var name) myfile << "Writing this to a file." << "\t" << randomNumber << endl; //overwrites myfile.close();//always close all of your files before exiting, when done //lesson #3: how you can read from an existing file string line; ifstream file("error.txt"); if ( file.is_open() ) { while ( getline ( file, line ) ) { cout << line << endl; } file.close(); } else cout << "Unable to open file!" << endl; //lesson #4: strings, characters, and operations with them string name = "Hello world,"; string continuation; continuation = " watch out for me! I'm coming."; name.append(continuation); //much easier than equivalent in C cout << name << endl; char letter = 'A'; //the grade you want to get :) printf("I am lonely, just the letter %c.\n",letter); //Next: debug with cout statements, string/file append, Gauss/exp rand's return 1; }