Need help with formating a simple program!!

This is a discussion on Need help with formating a simple program!! within the C++ Programming forums, part of the General Programming Boards category; I am creating a program to read in 24 hour notation time and output standard time. For example 23:32 should ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    6

    Need help with formating a simple program!!

    I am creating a program to read in 24 hour notation time and output standard time.
    For example 23:32 should output 11:32 PM.

    I have finished the program but am having trouble formatting the input.

    This is what it shows on the screen when I run the program.

    Please enter the time of day in 24 hour notation.
    Hour : Min
    23
    : 32

    Notice how after you enter the 23, it skips a line. I would like it to show up as 23:32 instead of 23
    : 32

    Is there a way to make it stay on the same line after cin?
    Here is a copy of the input part of the code.

    cout<<"Please enter the time of day in 24 hour notation.";
    cout<<"\nHour : Min \n";
    cin>>hour;
    cout<<" : ";
    cin>>min;

    Here is the entire program code below if you need to copy and paste it.


    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int inputFunction(int& hour, int& min);
    int convertFunction(int hour);
    string meridiemFunction(int militaryHour);
    void outputFunction(int hour, int min, string meridiem);
    char repeatFunction(char repeat);
    
    int main()
    {
    string meridiem;
    int hour, min, militaryHour;
    char repeat('N');
    
    do
    {
    inputFunction(hour, min);
    militaryHour=hour;
    hour=convertFunction(hour);
    meridiem=meridiemFunction(militaryHour);
    outputFunction(hour, min, meridiem);
    repeat=repeatFunction(repeat);
    cout<<endl<<endl;
    
    
    
    }while(repeat=='y'||repeat=='Y');
    return(0);
    }
    
    int inputFunction(int& hour, int& min)
    {
    int ok;
    do
    {
    cout<<"Please enter the time of day in 24 hour notation.";
    cout<<"\nHour : Min \n";
    cin>>hour;
    cout<<" : ";
    cin>>min;
    if(hour>24||min>59)
    {
    ok=0;
    cout<<"Invalid input for Hours or Minutes";
    }
    else
    ok=1;
    }while(ok==0);
    return(0);
    
    }
    
    int convertFunction(int hour)
    {
    if(hour>12)
    {
    hour=hour-12;
    }
    return(hour);
    
    }
    
    string meridiemFunction(int militaryHour)
    {
    string meridiem;
    if(militaryHour>11)
    meridiem="PM";
    else
    meridiem="AM";
    return(meridiem);
    }
    
    void outputFunction(int hour, int min, string meridiem)
    {
    cout<<"The hour in standard time is ";
    cout<<hour<<":"<<min<<" "<<meridiem<<endl;
    }
    
    char repeatFunction(char repeat)
    {
    int ok;
    do
    {
    cout<<"Would you like to enter another time? (Y/N): ";
    cin>>repeat;
    if(repeat=='Y'||repeat=='y'||repeat=='N'||repeat== 'n')
    ok=1;
    else
    {
    cout<<"Enter a valid character.";
    ok=0;
    }
    }while(ok==0);
    return(repeat);
    
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,481
    This is a guess from a C programmer (learning C++); did you try put or write method?
    ostream :: put - C++ Reference

    Tim S.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    You seem to have trouble formatting the source code as well.
    It should look something like this.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int inputFunction(int &hour, int &min);
    int convertFunction(int hour);
    string meridiemFunction(int militaryHour);
    void outputFunction(int hour, int min, string meridiem);
    char repeatFunction(char repeat);
    
    int main()
    {
        string meridiem;
        int hour, min, militaryHour;
        char repeat('N');
    
        do {
            inputFunction(hour, min);
            militaryHour = hour;
            hour = convertFunction(hour);
            meridiem = meridiemFunction(militaryHour);
            outputFunction(hour, min, meridiem);
            repeat = repeatFunction(repeat);
            cout << endl << endl;
        } while (repeat == 'y' || repeat == 'Y');
        return (0);
    }
    
    int inputFunction(int &hour, int &min)
    {
        int ok;
        do {
            cout << "Please enter the time of day in 24 hour notation.";
            cout << "\nHour : Min \n";
            cin >> hour;
            cout << " : ";
            cin >> min;
            if (hour > 24 || min > 59) {
                ok = 0;
                cout << "Invalid input for Hours or Minutes";
            } else
                ok = 1;
        } while (ok == 0);
        return (0);
    
    }
    
    int convertFunction(int hour)
    {
        if (hour > 12) {
            hour = hour - 12;
        }
        return (hour);
    
    }
    
    string meridiemFunction(int militaryHour)
    {
        string meridiem;
        if (militaryHour > 11)
            meridiem = "PM";
        else
            meridiem = "AM";
        return (meridiem);
    }
    
    void outputFunction(int hour, int min, string meridiem)
    {
        cout << "The hour in standard time is ";
        cout << hour << ":" << min << " " << meridiem << endl;
    }
    
    char repeatFunction(char repeat)
    {
        int ok;
        do {
            cout << "Would you like to enter another time? (Y/N): ";
            cin >> repeat;
            if (repeat == 'Y' || repeat == 'y' ||
                repeat == 'N' || repeat == 'n')
                ok = 1;
            else {
                cout << "Enter a valid character.";
                ok = 0;
            }
        } while (ok == 0);
        return (repeat);
    }
    > Is there a way to make it stay on the same line after cin?
    No - well not any good, simple and portable ways anyway.

    Input is usually line buffered, so your program doesn't see anything at all until the user presses return. It then (well the cin input conversion) then uses as much of that input as possible.

    You could type in
    23 32<enter>
    and you would just get a : printed on the line by itself, or just at the start of whatever output normally follows.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    I had the source code formatted right. I don't know why it didn't copy over when I pasted it. I still don't understand how to get it so the : is in between the hour and minute when the user inputs the time. Could someone show me an example? Sorry, but this is my first programming class and the teacher isn't really doing a good job, just gives us assignments and goes home.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    Yeah, simply copy/pasting from the forum doesn't work. You have to copy it from your code editor.

    > I still don't understand how to get it so the : is in between the hour and minute when the user inputs the time
    Get the user to type it in, then ignore it within your program.
    cin >> hour;
    char skippedColon; cin >> skippedColon;
    cin >> minute;

    To get the program to read "23" then output a ":" before you get a chance to type in "32" is certainly beyond your skill for the moment, possibly beyond the skill of your tutor as well.
    You should check what the assignment is really asking you to do, because this doesn't seem right for a basic programming course.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    Thanks for the help. Your right, this is beyond the scope of the assignment. Wanted to know how to do it just out of curiosity. If I can't figure this one out, I'll just prompt for the hour an minutes separately.

    Thanks Again

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    Thanks for the help. Your right, this is beyond the scope of the assignment. Wanted to know how to do it just out of curiosity. If I can't figure this one out, I'll just prompt for the hour an minutes separately.

    Thanks Again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  2. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 09:28 AM
  3. cin and formating
    By Hexxx in forum C++ Programming
    Replies: 3
    Last Post: 10-09-2004, 01:11 PM
  4. Need help formating Win XP
    By gqchynaboy in forum Tech Board
    Replies: 6
    Last Post: 11-13-2003, 12:42 PM
  5. Formating
    By jrahhali in forum Tech Board
    Replies: 10
    Last Post: 08-29-2003, 08:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21