 |

09-14-2009, 03:54 AM
|
|
Active Member
|
|
Join Date: Mar 2009
Posts: 238
|
|
[NEWB FRIENDLY] Simple Hours to Minutes Converter.
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
}
Last edited by prorathack1 : 09-14-2009 at 03:54 AM.
|

09-14-2009, 08:46 AM
|
 |
When They Cry...
|
|
Join Date: Jan 2007
Location: Japan
Posts: 4,404
|
|
Re: [NEWB FRIENDLY] Simple Hours to Minutes Converter.
You're making it way more complex than it needs to be, and it is very messy. In terms of standard code, this is a lot tidier, and hence a lot easier to read. Code that is easier to read is what I'd call "newb friendly".
Code:
#include "stdio.h"
#include "string.h"
#include "ctype.h"
// Declare strtolower(char* input) function.
// For those who are unfamiliar with C++ function declarations,
// the actual function is below the main function.
void strtolower(char* input);
int main(int argc, char** argv)
{
// While this remains true ...
bool running = true;
// ... The program will continue looping.
while(running)
{
printf("Enter conversion:\n\t %s \n\t %s \n\t %s \n\t %s \n\t %s \n\t %s \n---\n\t %s \nChoice: ",
"1. Seconds to minutes.",
"2. Seconds to hours.",
"3. Minutes to seconds.",
"4. Minutes to hours",
"5. Hours to seconds.",
"6. Hours to minutes.",
"0. Exit.");
// Get the user's choice.
int choice = 0;
scanf("%i", &choice);
// Declare input variables for seconds, minutes and hours.
int sec = 0, min = 0, hr = 0;
// Act upon user's choice conversion.
switch(choice) {
case 1:
printf("Enter amount of seconds: ");
scanf("%i", &sec);
printf("There is %i minutes in %i seconds.", (sec / 60), sec);
break;
case 2:
printf("Enter amount of seconds: ");
scanf("%i", &sec);
printf("There is %i hours in %i seconds.", (sec / 3600), sec);
break;
case 3:
printf("Enter amount of minutes: ");
scanf("%i", &min);
printf("There is %i seconds in %i minutes.", (min * 60), min);
break;
case 4:
printf("Enter amount of minutes: ");
scanf("%i", &min);
printf("There is %i hours in %i minutes.", (min / 60), min);
break;
case 5:
printf("Enter amount of hours: ");
scanf("%i", &hr);
printf("There is %i seconds in %i hours.", (hr * 3600), hr);
break;
case 6:
printf("Enter amount of hours: ");
scanf("%i", &hr);
printf("There is %i minutes in %i hours.", (hr * 60), hr);
break;
case 0:
printf("Exiting...");
return 0;
default:
printf("Please enter a valid choice.");
break;
}
// Loop is used to ensure the program receives appropriate input string.
for(;;)
{
// Ask for and scan input.
printf("\nPerform another conversion? [yes/no]: ");
char conversion[80];
scanf("%s", &conversion);
// Convert to lowercase.
strtolower(conversion);
// Note: strcmp(const char*, const char*)
// returns as 0 (false) for a match, and 1 (true) otherwise.
// Check to see if the user wants to exit the program.
if(!strcmp("no", conversion))
{
running = false;
}
else if(strcmp("yes", conversion))
{ // Note: This is checking whether or not the string is NOT "yes"
// and giving an error if so.
printf("Please enter 'yes' or 'no'.\n");
continue;
}
break;
}
}
return 0;
}
// Function strtolower(char* input)
// Converts the input string to lowercase.
void strtolower(char* input)
{
int i = 0;
while(input[i])
input[i++] = tolower(input[i]);
}
__________________
Quote:
[14:54:13] <&Filefragg> x9 is made of 5 glow in the dark gorilla dildos vibrating in unison.
[14:54:19] <%TDD> hot
[14:54:22] <+Aoi> Win
[14:54:23] <x9> I TOLD YOU NOT TO TELL ANYONE, DAMNIT
|
Last edited by Swan : 09-14-2009 at 08:53 AM.
|

09-14-2009, 03:03 PM
|
 |
Apprentice
|
|
Join Date: Nov 2007
Posts: 930
|
|
Re: [NEWB FRIENDLY] Simple Hours to Minutes Converter.
Correct me if I am wrong, but isn't it a little long for a simple hours to minute converter?
You could do like
Enter hours:
scan hours
result = hours * 60;
print result
Isn't it?
|

09-15-2009, 09:10 AM
|
 |
When They Cry...
|
|
Join Date: Jan 2007
Location: Japan
Posts: 4,404
|
|
Re: [NEWB FRIENDLY] Simple Hours to Minutes Converter.
Quote:
Originally Posted by d great one
Correct me if I am wrong, but isn't it a little long for a simple hours to minute converter?
You could do like
Enter hours:
scan hours
result = hours * 60;
print result
Isn't it?
|
My version converts between hours, minutes and seconds based on the user's choice. His uses all of this unneeded strcmp() crap and so on.
__________________
Quote:
[14:54:13] <&Filefragg> x9 is made of 5 glow in the dark gorilla dildos vibrating in unison.
[14:54:19] <%TDD> hot
[14:54:22] <+Aoi> Win
[14:54:23] <x9> I TOLD YOU NOT TO TELL ANYONE, DAMNIT
|
|

09-15-2009, 03:01 PM
|
 |
Apprentice
|
|
Join Date: Nov 2007
Posts: 930
|
|
Re: [NEWB FRIENDLY] Simple Hours to Minutes Converter.
I see. I didn't want to check his script because it is too long, and I trusted your judgment that his is a bit messy. And I don't like reading comments.
And I can see clearly now why you said his script is messy.
But we could give him a little clap for beginners. 
|

09-15-2009, 11:11 PM
|
|
Member
|
|
Join Date: Dec 2008
Posts: 91
|
|
Re: [NEWB FRIENDLY] Simple Hours to Minutes Converter.
|

10-18-2009, 12:32 AM
|
|
Member
|
|
Join Date: Dec 2008
Posts: 91
|
|
Re: [NEWB FRIENDLY] Simple Hours to Minutes Converter.
|
 |
|