Question

Time.cpp:

#include "Time.h"

#include <iostream>
using namespace std;

Time::Time(string time)
{
   hours = 0;
   minutes = 0;
   isAfternoon = false;

   //check to make sure there are 5 characters
   if (//condition to check if length of string is wrong)
   {
       cout << "You must enter a valid military time in the format 00:00" << endl;
   }
   else
   {
       //check to make sure the colon is in the correct spot
       if ( //condition to check if colon position is wrong)
       {
           cout << "You must enter a valid military time in the format 00:00" << endl;
       }
       //check to make sure all other characters are digits
       else if (//condition to check if first hour character is not a digit)
       {
           cout << "You must enter a valid military time in the format 00:00" << endl;
       }
       else if (//condition to check if second hour character is not a digit)
       {
           cout << "You must enter a valid military time in the format 00:00" << endl;
       }
       else if (//condition to check if first minute character is not a digit)
       {
           cout << "You must enter a valid military time in the format 00:00" << endl;
       }
       else if (//condition to check if second minute character is not a digit)
       {
           cout << "You must enter a valid military time in the format 00:00" << endl;
       }
       else
       {
           /*separate the string into hours and minutes converting them to integers
           and storing them into the instance variables*/
          

           //validate that hours and minutes are valid values
           if (hours > 23)
           {
               cout << "You must enter a valid military time in the format 00:00" << endl;
           }
           else if (minutes > 59)
           {
               cout << "You must enter a valid military time in the format 00:00" << endl;
           }
           //convert military time to conventional time for afternoon times, morning times don't need converting
           else if (hours > 12)
           {
               hours = hours - 12;
               isAfternoon = true;
           }
           //account for noon
           else if (hours == 12)
           {
               isAfternoon = true;
           }
           //account for midnight
           else if (hours == 0)
           {
               hours = 12;
           }


       }
   }
}

Time::Time()
{
   hours = 12;
   minutes = 0;
   isAfternoon = true;
}

ostream& operator<< (ostream& out, Time time)
{
   //write the time in conventional form including am or pm
  
  
  
   return out;
}

---------------

Time.h:

#ifndef TIME_H
#define TIME_H

#include <string>
using namespace std;

class Time
{
   friend ostream& operator<< (ostream& out, Time time);
private:
   int hours;
   int minutes;
   bool isAfternoon;
public:
   Time(string time);
   Time();

};
#endif
----------------

SSN.cpp:

#include <iostream>
using namespace std;

int main()
{
   const int SIZE = 12;
   char ssNum[SIZE];
   bool isValid = true;  

   cout << "Enter a social security number in the format ###-##-####" << endl;
   //this version of getline stores the typed characters into a c-string
   cin.getline(ssNum, SIZE);
   //this is to prove that a c-string ends in
   if (ssNum[SIZE-1] == '\0') cout << "null character" << endl;

   //validate the format to be ###-##-####
  
  
  
  
  
   return 0;
}

-------------------------

1. In the Time.cpp file, add conditions to the decision structure which validates the data. Conditions are needed that will a

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find below the code and output screenshots.

Code for TimeTester.cpp

#include <iostream>

#include "Time.h"

using namespace std;

//function to test valid times

void test_validtime(string time){

    cout <<"\n\nChecking time: "<< time <<"\n";

    Time T1 = Time(time); //create Time class object

    cout << T1; //display valid time in conventional form

}

//function to test invalid times

void test_invalidtime(string time){

    cout <<"\nChecking time: "<< time <<"\n";

    Time T1 = Time(time); //create Time class object

}

int main()

{

    //calling test_validtime() with valid times

   test_validtime("00:00");

test_validtime("12:00");

   test_validtime("04:05");

   test_validtime("10:15");

   test_validtime("23:59");

   test_validtime("00:35");

  

   cout <<"\n";

  

    //calling test_invalidtime() with invalid times

   test_invalidtime("7:56");

   test_invalidtime("15:78");

   test_invalidtime("08:60");

   test_invalidtime("24:00");

   test_invalidtime("3e:33");

   test_invalidtime("1:111");

  

  

   return 0;

}

Code for Time.h

//#ifndef TIME_H

//#define TIME_H

#include <string>

using namespace std;

class Time

{

   friend ostream& operator<< (ostream& out, Time time);

private:

   int hours;

   int minutes;

   bool isAfternoon;

public:

   Time(string time);

   Time();

};

Code for Time.cpp

#include "Time.h"

#include <iostream>

#include <iomanip>

#include <ctype.h>

#include <string.h>

using namespace std;

Time::Time(string time)

{

   hours = 0;

   minutes = 0;

   isAfternoon = false;

   //check to make sure there are 5 characters

   if (time.length() != 5)

   {

       cout << "You must enter a valid military time in the format 00:00" << endl;

   }

   else

   {

       //check to make sure the colon is in the correct spot

       if ( time[2] != ':')

       {

           cout << "You must enter a valid military time in the format 00:00" << endl;

       }

       //check to make sure all other characters are digits

       else if (!isdigit(time[0]))

       {

           cout << "You must enter a valid military time in the format 00:00" << endl;

       }

       else if (!isdigit(time[1]))//condition to check if second hour character is not a digit)

       {

           cout << "You must enter a valid military time in the format 00:00" << endl;

       }

       else if (!isdigit(time[3]))

       {

           cout << "You must enter a valid military time in the format 00:00" << endl;

       }

       else if (!isdigit(time[4]))

       {

           cout << "You must enter a valid military time in the format 00:00" << endl;

       }

       else

       {

           /*separate the string into hours and minutes converting them to integers

           and storing them into the instance variables*/

            hours = stoi(time.substr(0,2));

            minutes = stoi(time.substr(3,5));

           //validate that hours and minutes are valid values

           if (hours > 23)

           {

               cout << "You must enter a valid military time in the format 00:00" << endl;

           }

           else if (minutes > 59)

           {

               cout << "You must enter a valid military time in the format 00:00" << endl;

           }

           //convert military time to conventional time for afternoon times, morning times don't need converting

           else if (hours > 12)

           {

               hours = hours - 12;

               isAfternoon = true;

           }

           //account for noon

           else if (hours == 12)

           {

               isAfternoon = true;

           }

           //account for midnight

           else if (hours == 0)

           {

               hours = 12;

           }

       }

   }

}

Time::Time()

{

   hours = 12;

   minutes = 0;

   isAfternoon = true;

}

ostream& operator<< (ostream& out, Time time)

{

   //write the time in conventional form including am or pm

if(time.hours > 12){

      out <<"Time in conventional form = " <<(time.hours - 12)<<":"<< setw(2) << setfill('0')<<time.minutes<<" ";

} else {

      out <<"Time in conventional form = " <<time.hours<<":"<< setw(2) << setfill('0')<<time.minutes<<" ";

}

if(time.isAfternoon)

    out <<"pm";

else

    out <<"am";

   return out;

}

Output Screenshot:

Checking time: 00:00 Time in conventional form = 12:00 am Checking time: 12:00 Time in conventional form = 12:00 pm CheckingChecking time: 08:60 You must enter a valid military time in the format 00:00 Checking time: 24:00 You must enter a valid mil

SSN.cpp

#include <iostream>

using namespace std;

int main()

{

   const int SIZE = 12;

   char ssNum[SIZE];

   bool isValid = true;

   cout << "Enter a social security number in the format ###-##-####" << endl;

   //this version of getline stores the typed characters into a c-string

   cin.getline(ssNum, SIZE);

   //this is to prove that a c-string ends in

   if (ssNum[SIZE-1] == '\0') cout << "null character" << endl;

   //validate the format to be ###-##-####

   for(int i = 0;i<SIZE-1; i++){

       if(i == 3 || i == 6){

           if(ssNum[i] != '-'){

                isValid = false;

                cout << "Dash validation failed!!!\n";

                break;

           }

       } else{

           if (!isdigit(ssNum[i])){

               isValid = false;

               cout << "Number validation failed!!!\n";

               break;

           }

          

       }

   }

   if(isValid == true)

        cout << "Entered SSN is a valid one!!!";

   return 0;

}

Output: (I have changed the code such that if dash/number validation failed, further characters are not validated. Break the loop immediately. If you don't want that you can remove the break statements)

Screenshots taken brfore adding break statements

Enter a social security number in the format ###-##-#### 912-11-5263 null character Entered SSN is a valid one!!! ... Program

:-## ## Enter a social security_number in the format ### :-# 99-125-9623 null character Number validation failed!!! Dash vali

-## ## Enter a social security number in the format ###-# 123$25-5236 null character Dash validation failed!!! ... Program fi

Enter a social security number in the format ###-##-#### 52-p86-4523 null character Number validation failed!!! Dash validati

Add a comment
Know the answer?
Add Answer to:
Time.cpp: #include "Time.h" #include <iostream> using namespace std; Time::Time(string time) {    hours = 0;   ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include <iostream> #include <cstdlib> #include <time.h> #include <string> using namespace std; int main() { srand(time (0));...

    #include <iostream> #include <cstdlib> #include <time.h> #include <string> using namespace std; int main() { srand(time (0)); int number, guess, response, reply; int score = 0 number = rand() % 100 + 1; do { do { cout << "Enter your guess "; cin >> guess; score++; if (guess < number) cout << guess << " is too low! Enter a higher number. "; else if (guess > number) cout << guess << " is too high! Enter a lower number....

  • Chapter 9 Lab Text Processing and Wrapper Classes Lab Objectives ? Use methods of the Character...

    Chapter 9 Lab Text Processing and Wrapper Classes Lab Objectives ? Use methods of the Character class and String class to process text ? Be able to use the StringTokenizer and StringBuffer classes Introduction In this lab we ask the user to enter a time in military time (24 hours). The program will convert and display the equivalent conventional time (12 hour with AM or PM) for each entry if it is a valid military time. An error message will...

  • Convert to use functions where possible #include<iostream> #include<string> using namespace std; int main() {    string...

    Convert to use functions where possible #include<iostream> #include<string> using namespace std; int main() {    string first, last, job;    double hours, wages, net, gross, tax, taxrate = .40;    double oPay, oHours;    int deductions;    // input section    cout << "Enter First Name: ";    cin >> first;    cout << "Enter Last Name: ";    cin >> last;    cin.ignore();    cout << "Enter Job Title: ";    getline(cin, job);    cout << "Enter Hours Worked:...

  • #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car {...

    #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car { private: string reportingMark; int carNumber; string kind; bool loaded; string choice; string destination; public: Car() { reportingMark = ""; carNumber = 0; kind = "Others"; loaded = 0; destination = "NONE"; } ~Car() { } void setUpCar(string &reportingMark, int &carNumber, string &kind, bool &loaded, string &destination); }; void input(string &reportingMark, int &carNumber, string &kind, bool &loaded,string choice, string &destination); void output(string &reportingMark, int &carNumber,...

  • Use C++ #include <iostream> #include <stdlib.h> #include <stdio.h> #include <cstring> using namespace std; /I Copy n...

    Use C++ #include <iostream> #include <stdlib.h> #include <stdio.h> #include <cstring> using namespace std; /I Copy n characters from the source to the destination. 3 void mystrncpy( ???) 25 26 27 28 29 11- 30 Find the first occurrance of char acter c within a string. 32 ??? mystrchr???) 34 35 36 37 38 39 / Find the last occurrance of character c within a string. 40 II 41 ??? mystrrchr ???) 42 43 45 int main() char userInput[ 81]; char...

  • please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName();...

    please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName(); double getNumberExams(); double getScoresAndCalculateTotal(double E); double calculateAverage(double n, double t); char determineLetterGrade(); void displayAverageGrade(); int main() { string StudentName; double NumberExam, Average, ScoresAndCalculateTotal; char LetterGrade; StudentName = getStudentName(); NumberExam = getNumberExams(); ScoresAndCalculateTotal= getScoresAndCalculateTotal(NumberExam); Average = calculateAverage(NumberExam, ScoresAndCalculateTotal); return 0; } string getStudentName() { string StudentName; cout << "\n\nEnter Student Name:"; getline(cin, StudentName); return StudentName; } double getNumberExams() { double NumberExam; cout << "\n\n Enter...

  • #include <iostream>   #include <string>   using namespace std;      void get_user_string(string *);   string convert_to_dash(String* );   int_search_and_replace(char, string,...

    #include <iostream>   #include <string>   using namespace std;      void get_user_string(string *);   string convert_to_dash(String* );   int_search_and_replace(char, string, string &);       int main (){    string s;    cout << "Enter a string:" << endl;    get_user_string(&s);        string dash_version = convert_to_dash(&s)    if ( dash_version != 32){    &s.push_back('-');    } Here is an example operation of the completed program: Please enter a string: the weather is great! The dash-version of your string is: Please tell me the char that...

  • #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int...

    #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...

  • #include <iostream> #include <string> using namespace std; int main() { int number; int sum = 0;...

    #include <iostream> #include <string> using namespace std; int main() { int number; int sum = 0; while(true) { cout << "Please enter a number between 1 and 11: "; cin >> number; if (number >= 1 && number <= 11) { cout << number << endl; sum = sum + number; //only add the sum when number is in range: 1-11, so add wthin this if case } else { cout << number << endl; cout << "Out of range;...

  • #include <iostream> #include <string> #include "hashT.h" #include "stateData.h" using namespace std; void stateData::setStateInfo(string sName, string sCapital,...

    #include <iostream> #include <string> #include "hashT.h" #include "stateData.h" using namespace std; void stateData::setStateInfo(string sName, string sCapital,    double stArea, int yAdm, int oAdm) {    stateName = sName; stateCapital = sCapital; stArea = stateArea; yAdm = yearOfAdmission; oAdm = orderOfAdmission;       } void stateData::getStateInfo(string& sName, string& sCapital,    double& stArea, int& yAdm, int& oAdm) {    sName = stateName; sCapital = stateCapital; stArea = stateArea; yAdm = yearOfAdmission; oAdm = orderOfAdmission;       } string stateData::getStateName() { return stateName;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT