Wednesday, July 13, 2011

Loops,Selection, Simple File IO

#include <iostream>
#include <cmath>
#include <ctime>
#include <string>
#include <Windows.h>
#include <fstream>

/*
these are examples of loops
and branches
*/
using namespace std;

void SimpleForLoop();
void WhileLoop();
void DoLoops ();
void IfExamples();
void Menu();
void Pause();
void WriteFile();
void ReadFile();



int main()
{
//SimpleForLoop();
//WhileLoop();
//DoLoops();
//IfExamples();
Menu();
char c;
cin >> c;

}

void SimpleForLoop()
{
int myArray[5];

int i;
int total=0;
srand(time(0));
for(i =0; i<5; i++)
{

int number=rand();
myArray[i]=number;
}

for(int x=0; x<5; x++)
{
cout << myArray[x] <<endl;
total += myArray[x];
}

cout << "the total is " << total << " the Average is " << double(total) / i << endl;



}

void WhileLoop()
{
int i=1;
int number;
int total=0;
while( i != 0)
{
cout << "Enter an integer, 0 to exit" << endl;
cin >> i;
total += i;

}

cout << "Your total is " << total;
}

void DoLoops ()
{
int i = 0;
int total=0;
do
{
cout << "Enter an integer, 0 to exit" << endl;
cin >> i;
total += i;

}while(i !=0);

cout << "the total is " << total << " the Average is " << double(total) / i << endl;
}

void TwoDArray()
{
string myTwoDArray[3] [2] =
{
{"Twilight", "Meyers"},
{"Ulysses", "Joyce"},
{ "King Lear", "Shakespear"}
};

/*myTwoDArray[1,0]="Ulysses";
myTwoDArray[1,1]="Joyce";
myTwoDArray[2,0]="King Lear";
myTwoDArray[2,1]="Shakespear";*/


}

void IfExamples()
{
string message;

cout << "Enter the temperature" << endl;
int temp;
cin >> temp;
//or is ||
if (temp > -30 && temp < 0)
{
message="Way too cold";
}
else
if (temp >1 && temp < 20)
{
message ="Still too Cold";
}
else
if(temp >21 && temp < 50)
{
message="Cool";
}
else
if(temp >51 && temp < 80)
{
message="comfortable";
}
else
{
message="hot";
}

cout << message << endl;
}

void Menu()
{
int choice=999;

/* SimpleForLoop();
void WhileLoop();
void DoLoops ();
void IfExamples();
void Menu();*/
while (choice != 0)
{

system("cls");
cout << "Simple For Loop: 1" <<endl;
cout << "While Loops: 2" <<endl;
cout << "Do Loops: 3" << endl;
cout << "If Examples: 4" <<endl;
cout << "Write Example 5"<<endl;
cout << "Read File 6"<<endl;
cout <<"Exit: 0" << endl;

cout << "Enter your menu choice" << endl;
cin >> choice;

switch (choice)
{
case 1:
SimpleForLoop();
Pause();
break;
case 2:
WhileLoop();
Pause();
break;
case 3:
DoLoops();
Pause();
break;
case 4:
IfExamples();
Pause();
break;
case 5:
WriteFile();
Pause();
break;
case 6:
ReadFile();
Pause();
break;
case 0:
return;
default:
cout << "Enter a menu choice";
Pause();



}
}
}

void Pause()
{
cout<<"\n\nPress any key to continue";
char c;
cin >> c;
}

void WriteFile()
{
cin.get();
string message;
fstream outfile("C:\\Users\\sconger\\Desktop\\Test.txt",ios::out);
cout << "Enter your message" <<endl;
getline(cin, message);

outfile<<message;
outfile.close();

cout << "The file has been written" << endl;

}

void ReadFile()
{
cin.get();
string message;
fstream infile("C:\\Users\\sconger\\Desktop\\Test.txt",ios::in );
getline(infile,message);
cout << message << endl;

}

No comments:

Post a Comment