Question

In: Computer Science

I have the below program and I am having issues putting all the output in one...

I have the below program and I am having issues putting all the output in one line instead of two.

how can I transform the program for the user enter format ( HH:MM)
Like:
Enter time using 24 format ( HH:MM) : " and the user enter format " 14:25
then the program convert. instead of doing two lines make all one line.

Currently I have the user enter first the Hour and then the minutes but i'd like to change the program to have the user enter the Hour and Minute in the same line. using format : " HH:MM"

#include<iostream>
using namespace std;

void input(int& hours24, int& minutes){
  
   cout<<"Enter hours [0 -23]: "; cin >> hours24;
   cout<<"Enter minutes [0 - 59]: "; cin >> minutes;
}

void output(int hours, int minutes, char AMPM){
  
   if(hours<10) cout<<"0"; cout<<hours; cout<<":";
   if(minutes<10) cout<<"0"; cout<<minutes;
   if(AMPM=='A') cout<<" AM";
   else cout<<" PM";
   cout<<endl;
}

void convert(int& hours, char& AMPM){
   if(hours>12){
       hours-=12;
       AMPM = 'P';
   }
   else if(hours==12){
       AMPM ='P';
   }
   else if(hours==0){
       hours += 12;
       AMPM ='A';
   }
   else{
       AMPM = 'A';
   }
}


int main(){
  
   int hours, minutes;
   char AMPM, ans;
  
   do{
       input(hours,minutes);
       if(hours<0 || hours>23 || minutes<0 || minutes>59){
           cout<<"Error: Invalid hours/minutes entered.\n";
           cout<<"Program will terminate.\n";
           break;
       }
      
       convert(hours,AMPM);
       output(hours,minutes,AMPM);
      
       cout<<"Enter Y or y to continue,"
           <<" anything else quits.\n";
           cin >> ans;
      
   }while(ans=='Y' || ans =='y');


   return 0;
}

Solutions

Expert Solution

Answer: Hey dear student finds the solution of your query, if you have any doubt feel free to ask. Thanks!!

Modified Code: Output in single line only , you can see snapshot of the output.

#include<iostream>
using namespace std;

//this function is modified only ,one parameter added to take character ':'

void input(int& hours24, int& minutes,char &ch)

{
   cout<<"Enter a 24-hour time (ex. 15:23): ";
cin >> hours24>> ch >>minutes;

//when we input 13:45 13 store in hours24,':' store in ch,45 store in minutes

}

void output(int hours, int minutes, char AMPM){
  
if(hours<10) cout<<"0"; cout<<hours; cout<<":";
if(minutes<10) cout<<"0"; cout<<minutes;
if(AMPM=='A') cout<<" AM";
else cout<<" PM";
cout<<endl;
}

void convert(int& hours, char& AMPM){
if(hours>12){
hours-=12;
AMPM = 'P';
}
else if(hours==12){
AMPM ='P';
}
else if(hours==0){
hours += 12;
AMPM ='A';
}
else{
AMPM = 'A';
}
}


int main(){
  
int hours, minutes;
char AMPM, ans,chr;
  
do{
input(hours,minutes,chr);
if(hours<0 || hours>23 || minutes<0 || minutes>59){
cout<<"Error: Invalid hours/minutes entered.\n";
cout<<"Program will terminate.\n";
break;
}
  
convert(hours,AMPM);
output(hours,minutes,AMPM);
  
cout<<"Enter Y or y to continue,"
<<" anything else quits.\n";
cin >> ans;
  
}while(ans=='Y' || ans =='y');


return 0;
}

Output:


Related Solutions

I am having a hard time getting my an output after putting in the second set...
I am having a hard time getting my an output after putting in the second set of functions and I was hoping to be able to have the results from the functions be rounded to 2 decimal places. <html> <head> <title>Length Conversion</title> <script language='JavaScript' type='text/JavaScript'> <!-- function validate(type) { if(document.my_form.textinput.value=='') { alert('Fill the Input box before submitting'); return false; }else{ if(type=="to_feet"){ var res=3.2808*document.my_form.textinput.value; var unit=" feet"; }else{ var res=0.3048*document.my_form.textinput.value; var unit=" meter"; } document.getElementById("result").innerHTML=res.toFixed(2) + unit; return false; } }...
I am having a trouble with a python program. I am to create a program that...
I am having a trouble with a python program. I am to create a program that calculates the estimated hours and mintutes. Here is my code. #!/usr/bin/env python3 #Arrival Date/Time Estimator # # from datetime import datetime import locale mph = 0 miles = 0 def get_departure_time():     while True:         date_str = input("Estimated time of departure (HH:MM AM/PM): ")         try:             depart_time = datetime.strptime(date_str, "%H:%M %p")         except ValueError:             print("Invalid date format. Try again.")             continue        ...
I have a project due and am having problems compiling a program: The only program I...
I have a project due and am having problems compiling a program: The only program I have written is check.c everything else is given and correct. Can you modify check.c to simply print out the chessboard, in other words, to get it to compile by any means thank you. The only thing you have to modify again is check.c nothing else. Just get it to print something thanks. program chess.c #include #include #include "chess.h" void get_valid_move(int mover) { int x_from,...
Please fix this code I am having issues compiling it all together there is 3 codes...
Please fix this code I am having issues compiling it all together there is 3 codes here and it's giving me errors in my main method..... I feel like it might be something simple but I can't seem to find it. package assignement2; import java.util.ArrayList; import java.util.Scanner; public class reg1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the number of items: "); int number = input.nextInt(); input.nextLine(); for (int i = 0; i <...
This needs to be done in Excel and that is where I am having issues Healthy...
This needs to be done in Excel and that is where I am having issues Healthy Snacks Co. produces snack mixes. Recently, the company has decided to introduce a new snack mix that has peanuts, raisins, pretzels, dries cranberries, sunflower seeds and pistachios. Each bag of the new snack is designed in order to hold 250 grams of the snack. The company has decided to market the new product with a emphasis on its health benefits. After consulting nutritionists, Healthy...
I am taking a Data Structures and Program Design class. BUT, I am having trouble getting...
I am taking a Data Structures and Program Design class. BUT, I am having trouble getting access to the vectors so that I can compare the guesses with the random numbers. I’ve been working on this for several days and don't feel any closer to a solution and could really use some help as I really don't know how to get this to work. Thank you! The assignment is: Design an ADT for a one-person guessing game that chooses 4...
I am having an issue with the code. The issue I am having removing the part...
I am having an issue with the code. The issue I am having removing the part when it asks the tuter if he would like to do teach more. I want the program to stop when the tuter reaches 40 hours. I believe the issue I am having is coming from the driver. Source Code: Person .java File: public abstract class Person { private String name; /** * Constructor * @param name */ public Person(String name) { super(); this.name =...
I am working on these study questions and am having trouble understanding how it all works...
I am working on these study questions and am having trouble understanding how it all works together. Any help would be greatly appreciated!! An all equity firm is expected to generate perpetual EBIT of $50 million per year forever. The corporate tax rate is 0% in a fantasy no tax world. The firm has an unlevered (asset or EV) Beta of 1.0. The risk-free rate is 5% and the market risk premium is 6%. The number of outstanding shares is...
Hi, Working on a project in my group for class and I am having some issues...
Hi, Working on a project in my group for class and I am having some issues My part is current state of the business. It is a store and the annual sales are $460,000 Other info I have is: Ownership and Compensation; Percent Ownership Personal Investment Mitchell George, Founder & CEO 25% $125,000Katie Beauseigneur, COO 15% $75,000 Melissa Dunnells, CFO15% $75,000 Also, a medium coffee price from store is $3.75 Sarah Griffin, Marketing Officer 10% $50,000 Katharina Ferry, HR Director10%...
I am having an issue with the Java program with a tic tac toe. it isn't...
I am having an issue with the Java program with a tic tac toe. it isn't a game. user puts in the array and it prints out this. 1. Write a method print that will take a two dimensional character array as input, and print the content of the array. This two dimensional character array represents a Tic Tac Toe game. 2. Write a main method that creates several arrays and calls the print method. Below is an example of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT