[NEWB FRIENDLY] Simple Hours to Minutes Converter.

Discussion in 'Programming General' started by prorathack1, Sep 13, 2009.

[NEWB FRIENDLY] Simple Hours to Minutes Converter.
  1. Unread #1 - Sep 13, 2009 at 10:54 PM
  2. prorathack1
    Joined:
    Mar 12, 2009
    Posts:
    239
    Referrals:
    0
    Sythe Gold:
    0

    prorathack1 Active Member

    [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
    }
    
        
    
    
     
  3. Unread #2 - Sep 14, 2009 at 3:46 AM
  4. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    [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]);
    }
    
    
     
  5. Unread #3 - Sep 14, 2009 at 10:03 AM
  6. d great one
    Joined:
    Nov 4, 2007
    Posts:
    930
    Referrals:
    2
    Sythe Gold:
    0

    d great one Apprentice

    [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?
     
  7. Unread #4 - Sep 15, 2009 at 4:10 AM
  8. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    [NEWB FRIENDLY] Simple Hours to Minutes Converter.

    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.
     
  9. Unread #5 - Sep 15, 2009 at 10:01 AM
  10. d great one
    Joined:
    Nov 4, 2007
    Posts:
    930
    Referrals:
    2
    Sythe Gold:
    0

    d great one Apprentice

    [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. ;)
     
  11. Unread #6 - Sep 15, 2009 at 6:11 PM
  12. super_
    Joined:
    Dec 20, 2008
    Posts:
    91
    Referrals:
    0
    Sythe Gold:
    0

    super_ Member

    [NEWB FRIENDLY] Simple Hours to Minutes Converter.

  13. Unread #7 - Oct 17, 2009 at 7:32 PM
  14. super_
    Joined:
    Dec 20, 2008
    Posts:
    91
    Referrals:
    0
    Sythe Gold:
    0

    super_ Member

    [NEWB FRIENDLY] Simple Hours to Minutes Converter.

< Functions Help | Runescape Applet >

Users viewing this thread
1 guest


 
 
Adblock breaks this site