Question

In: Computer Science

Write this program in C++ language. Use the concept of structures. DO NOT use vectors. Q...

Write this program in C++ language. Use the concept of structures. DO NOT use vectors.

Q (4) Create a structure called time. Its three members, all type int, should be called hours,
minutes, and seconds. Write a program that prompts the user to enter a time value in hours,
minutes, and seconds. This should be in 12:59:59 format. This entire input should be assigned
first to a string variable. Then the string should be tokenized thereby assigning the 1st token
to hours, 2nd token to minutes, and 3rd token to seconds member variables of the structure
called time. Finally inside a void Print_in_Seconds(void) member function of time structure,
it should print out the total number of seconds represented by this time value:
long totalsecs = t1.hours*3600 + t1.minutes*60 + t1.seconds (10 Points)

Solutions

Expert Solution

Program Code [C++]

#include <iostream>
#include <sstream>

using namespace std;

// time struct

struct time {
        
        // All Required members for hours, minutes and seconds
        
        int hours;
        int minutes;
        int seconds;
        
        // member function Print_in_Seconds
        
        void Print_in_Seconds() {
                
                // A variable of long type which will store total seconds as mentioned in question
                // this is a pointer which refers to the member variables of the C++ struct or class
                // So we can use this to access every member of time struct
                // Below we are accessing hours, minutes and seconds of time struct using this and adding them
                // Then returning the total seconds
                
                long totalsecs = this->hours * 3600 + this->minutes * 60 + this->seconds;
                cout << "Total number of seconds: " << totalsecs << endl;
        }
};

int main() {
        
        struct time t1;
        
        // Asking user to enter a tome
        
        cout << "Enter a time value in hours, minutes and seconds: ";
        string timeVal;
        cin >> timeVal; // Taking input as a string
        
        // Tokenizing string three times to get value of hours
        
        stringstream t(timeVal);    // We'll tokenize using stringstream of c++
        string temp;                // An extra variable to store tokenized string
        
        getline(t, temp, ':');  // First getting hours ex: 12:59:59 -> temp will have 12
        t1.hours = stoi(temp);  // Now since it is a string, we need to convert to int as hours member of struct time and storing in t1 object
        
        getline(t, temp, ':');  // Now getting minutes ex: 12:59:59 -> temp will now have 59
        t1.minutes = stoi(temp);    // stoi(string to int conversion) again converting to string & storing in t1's minutes
        
        getline(t, temp, ':');  // Now getting seconds => temp will have 59 in case of 12:59:59
        t1.seconds = stoi(temp);    // Converting again to int
        
        // Now printing time on console
        
        cout << "time is " << t1.hours << ":" << t1.minutes << ":" << t1.seconds << endl;
        t1.Print_in_Seconds();  // Calling Print_in_Seconds function to show total seconds on console
        
        return 0;
}

Sample Output:-

----------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERIES!!!
HIT A THUMBS UP IF YOU DO LIKE IT!!!


Related Solutions

Please do not use vectors or any previously posted code Write a C++ program which reads...
Please do not use vectors or any previously posted code Write a C++ program which reads in a list of process names and integer times from stdin/cin and simulates round-robin CPU scheduling on the list. The input is a list of lines each consisting of a process name and an integer time, e.g. ProcessA 4 ProcessB 10 Read line by line until an end-of-transmission (^d) is encountered. You should read the list and represent it in a linked list data...
Please use C language and use link list to do this program. This program should ask...
Please use C language and use link list to do this program. This program should ask user to enter two fraction polynomials. Then user chocie if he want add it or multiple it. I need you please to test it to see if it work with these two fraction polynomials 1-  Left Poly Pointer: 1/1x2 + 3/4x + 5/12 2-Right Poly Pointer: 1/1x4 – 3/7x2 + 4/9x + 2/11 AND AFTER ADDING 3- Resulting Poly Pointer: 1/1x4 + 4/7x2 + 43/36x...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 4: Calorie Counting Specifications: Write a program that allows the user to enter the number of calories consumed per day. Store these calories in an integer vector. The user should be prompted to enter the calories over the course of one week (7 days). Your program should display the total calories consumed over...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 1: Largest and Smallest Vector Values Specifications: Write a program that generates 10 random integers between 50 and 100 (inclusive) and puts them into a vector. The program should display the largest and smallest values stored in the vector. Create 3 functions in addition to your main function. One function should generate the...
Write an interactive program in c++ that allows the user toenter 2-15 vectors (use the...
Write an interactive program in c++ that allows the user to enter 2-15 vectors (use the if statement). Display the resultant on the screen.
C language <stdio.h> use double and const Write a program that asks the user for the...
C language <stdio.h> use double and const Write a program that asks the user for the radius of a circle (as a double) and displays the diameter, circumference, and area of the circle. Use a constant of 3.14159 as the value of pi. Have a blank line between the input and the output. Follow the 3 steps in the Information Processing Cycle - Input, Processing, and Output. The formulas needed are: diameter = 2 * radius circumference = 2 *...
Use C language Write a program that reads in a series of lines of input character...
Use C language Write a program that reads in a series of lines of input character by character (using getchar()). The first line of the input contains an integer which specifies the number of remaining lines of input, each of which contains a floating point number. The integer value on the first line can be read with scanf(), but all of the following lines can only be read with getchar(). Each line after the first contains a single floating point...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three random integers in the range [1, 100] and continues as follows: If the right-most digit of all the three integers is equal, the program displays them in ascending order on the screen and continues. If the generated integers have different right-most digits, they are not displayed and the program continues. The program terminates once the right-most digits of all the three random numbers are...
How do I create this program? Using C++ language! Write a program that reads data from...
How do I create this program? Using C++ language! Write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global varibles are the mean, standard deviation, and the number of data entered. All other varibles must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function....ALL...
1a) Write a program in C programming language to determine *pass* or *fail*. Use the GP...
1a) Write a program in C programming language to determine *pass* or *fail*. Use the GP ( 0.00 - 1.49 -> fail 1.50 - 4.00 -> pass ) 1b) Write a program in C to display month in Islamic Calendar.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT