Question

In: Computer Science

Write a C++ program that displays the current time

Write a C++ program that displays the current time

Solutions

Expert Solution

Program:

#include<iostream>
#include<ctime>
using namespace std;

int main()
{
   int hours,mins,sec;
   string amOrpm;
   time_t currentDateAndTime;
   struct tm * getTime;

   time (&currentDateAndTime); //get the current system date and time
   getTime = localtime (&currentDateAndTime); //store the time in struct variable

   hours=getTime->tm_hour; //extract hours from time
   mins=getTime->tm_min; //extract mintues from time
   sec=getTime->tm_sec; //extract seconds from time

   //if hours is greater than 12 set time as PM
   if(hours>12)
   {
       hours=hours-12;
       amOrpm="PM";
   }
   //else set time as AM
   else
       amOrpm="AM";
  
   //display the time      
   cout<<"\nThe current time is "<<hours<<":"<<mins<<":"<<sec<<" "<<amOrpm<<"\n";
   return 0;
}

Output:


Related Solutions

C++ program Overloaded Hospital Write a c++ program that computes and displays the charges for a...
C++ program Overloaded Hospital Write a c++ program that computes and displays the charges for a patient’s hospital stay. First, the program should ask if the patient was admitted as an inpatient or an outpatient. If the patient was an inpatient, the following data should be entered: The number of days spent in the hospital The daily rate Hospital medication charges Charges for hospital services (lab tests, etc.) The program should ask for the following data if the patient was...
c++ Write a program that displays the status of an order. a) Program uses 2 functions...
c++ Write a program that displays the status of an order. a) Program uses 2 functions (in addition to main ()). b) The first function asks the user for the data below and stores the input values in reference parameters. c) Input data from user: # of spools ordered, # of spools in stock, any special shipping & handling charges over and above the $10 rate. d) The second function receives as arguments any values needed to compute and display...
Write a C++ program that asks the user to enter in three numbers and displays the...
Write a C++ program that asks the user to enter in three numbers and displays the numbers in ascending order. If the three numbers are all the same the program should tell the user that all the numbers are equal and exits the program. Be sure to think about all the possible cases of three numbers. Be sure to test all possible paths. Sample Runs: NOTE: not all possible runs are shown below. Sample Run 1 Welcome to the order...
For C++ Write a program that opens a specified text file then displays a list of...
For C++ Write a program that opens a specified text file then displays a list of all the unique words found in the file. Hint: Store each word as an element of a set.
Write a C program that selects and displays the maximum value of five numbers to be...
Write a C program that selects and displays the maximum value of five numbers to be entered when the program is executed. (Hint : Use a for loop with both a scan_s and if statement inside the loop.)
C++ Overloaded Hospital Write a program that computes and displays the charges for a patient’s hospital...
C++ Overloaded Hospital Write a program that computes and displays the charges for a patient’s hospital stay. First, the program should ask if the patient was admitted as an in-patient or an out-patient. Please keep asking the user until the user enters the valid choice. Please refer to the test cases for more details. If the patient was an in-patient the following data should be entered: • The number of days spent in the hospital • The daily rate •...
Write a program in C++ that asks the user for his/her Taxable Income and displays the...
Write a program in C++ that asks the user for his/her Taxable Income and displays the Income Tax owed. Use the following table to calculate the Income Tax: Taxable Income Tax Rate $0 to $9,225 / 10% $9,226 to $37,450 / $922.50 plus 15% of the amount over $9,225 $37,451 to $90,750 / $5,156.25 plus 25% of the amount over $37,450 $90,751 to $189,300 / $18,481.25 plus 28% of the amount over $90,750 $189,301 to $411,500 / $46,075.25 plus 33%...
Write a C++ program that displays the numbers between 1000 and 9999, which are divisible by...
Write a C++ program that displays the numbers between 1000 and 9999, which are divisible by sum of the digits in them. For example, 2924 is divisible by (2+9+2+4 = 17). Your program should display all these possible numbers in the given range, and each number should be separated from the other by a space.
Write a C program that creates a structure and displays its content. • Create a struct...
Write a C program that creates a structure and displays its content. • Create a struct that will be used to hold a student's name, age, and year in school (Freshman, Sophomore, Junior, or Senior) • Within function main, dynamically allocate space to hold the structure and assign a pointer to point to the memory space allocated • Read in (from the keyboard) the student's name, age, and year in school • Create a separate function with the prototype: void...
In c++ please. Write a program that opens a specified text tile then displays a list...
In c++ please. Write a program that opens a specified text tile then displays a list of all the unique words found in the file. Hint: Store each word as an element of a set.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT