prorathack1
Active Member
- Joined
- Mar 12, 2009
- Sythe Gold
- 0
- Messages
- 239
Feel free to experiment with the code or ask any questions:
Code:
#include <iostream>
#include <cstring>
#include <windows.h>
#include <fstream>
using namespace std;
int hrsEnt () //function
{
char hrs[10];
int min;
char conf[10];
int y = 0;
static bool exitTxt = false;
cout << "Enter hours:" << endl; // prints out a line that asks for the user to enter a number it the program
if (!exitTxt) //want it to show once so the user doesn't get annoyed by this line getting spammed
{
cout << "Please type exit to safely quit this program" << endl; //this is the line we don't want to to be spammed
exitTxt = true;
}
cin.getline ( hrs, 10 ); //user's input = hrs
if ( strcmp( hrs, "0" ) == 0) //if user writes the value 0
{
cout << "Obviously, there is 0 minutes in 0 hours NOOB\n" << "Try putting a number greater than 0" << endl; // will generate this line
hrsEnt (); //will restart this function..
}
else if ( strcmp( hrs, "exit" ) == 0) //if user inputs the word "exit"
{
cout << "Are you sure you want to exit? (yes or no)" << endl; //asks for confirmation
cin.getline ( conf, 10 ); //getting the user input into conf
if ( strcmp( conf, "yes" ) == 0 || strcmp( conf, "Yes" ) == 0 || strcmp( conf, "YES" ) == 0 || strcmp( conf, "exit")) //if user writes "yes"
{
int x = 5;
while ( x > 0) //displays "Program exiting in 5, 4 ,3 ,2 ,1"
{
cout << "Exiting program in " << x << endl;
x--;
Sleep(500);
}
ofstream a_file ( "Results.txt", ios::app ); //writes in the results.txt file "end of session"
a_file << "End of session\n" << "-----------------------------------\n" << "\n" << endl;
a_file.close();
return 0; //exit program with no erorrs
}
if ( strcmp( conf, "no" ) == 0 || strcmp( conf, "No" ) == 0 || strcmp( conf, "NO" ) == 0) //if user writes "no"
{
hrsEnt (); //restart program
}
}
else if ( strcmp( hrs, "1" ) == 0) //of user writes 1
{
cout << "There is 60 minutes in 1 hour" << endl; //generate this line only IF user writes 1
hrsEnt (); //restart program
}
else
{
int hrsInt = 0;
static bool shown = false;
static bool intro = false;
hrsInt = atoi(hrs); //converting the string into an integer so it could manipulated
min = (hrsInt * 60); //multiplies whatever the user inputed by 60 so it gives him the result
cout << "There is " << min << " minutes in " << hrs << " hours" << endl; // here it prints out the result
ofstream a_file ( "Results.txt", ios::app ); //create Results.txt file to write results into
if (!intro)
{
a_file << "Your results saved from Hours to Minutes Converter:" << endl; //telling the program to only write this line once in a session so it doesn't spam the output
intro = true;
}
a_file << "There is " << min << " minutes in " << hrs << " hours" << endl; //prints out the result in a text file
a_file.close(); //closes the file editing session
if (!shown) //will notify the user ONCE that the results will be all saved to Results.txt
{
cout << "All results has been saved to Results.txt" << endl; //this what the user will see as a confirmation
shown = true;
}
hrsEnt (); //restart the fucntion
}
}
int main () //main function
{
hrsEnt (); //exceute the function we coded
}


