Simple program, need help.

This is a discussion on Simple program, need help. 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

    Simple program, need help.

    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.




    #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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    It's astonishing that without the code tags checking script, almost NOBODY reads this thread
    << !! Posting Code? Read this First !! >>
    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.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    Sorry, I'm new here. What you just said makes no sense to me at all. Could you please explain what you mean?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,764
    Quote Originally Posted by Rjstamey View Post
    Sorry, I'm new here. What you just said makes no sense to me at all. Could you please explain what you mean?
    Read the page the link goes to.
    Use Code Tags and indent your code correctly.
    And, whatever else it says.

    Tim S.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    Need help with formating a simple program!!
    Reposted here, this is closed.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 01:16 PM
  2. simple program, simple error? HELP!
    By colonelhogan44 in forum C Programming
    Replies: 4
    Last Post: 03-21-2009, 12:21 PM
  3. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 09:37 PM
  4. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 10:28 AM
  5. Need help with simple, simple program.
    By LightsOut06 in forum C Programming
    Replies: 5
    Last Post: 09-01-2005, 09:31 PM

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