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 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 *...
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...
Write the following in C language for Arduino: Write a program that turns on the LED...
Write the following in C language for Arduino: Write a program that turns on the LED at 25%, 50%, 75%, 100%, and then 0% brightness with a one second delay in between each change. Remember you are going to need to use a PWM pin and use the "analogWrite" command. The maximum value for our Arduino R3 boards is 255 and you need five steps (25%, 50%, 75%, 100%, and 0%) so you will need to determine the values for...
Write a C program with the following prompt *do not use gotostatement* "Write an interactive...
Write a C program with the following prompt *do not use goto statement* "Write an interactive program that implements a simple calculator. The program should allow the user to choose a binary arithmetic operation and enter two terms to which to apply the operation. The program should then compute the result and display it to the user. Your calculator will have two modes: double-precision mode and integer mode. Double-precision mode will do calculations and output using variables of type double....
C++ Vectors. Create a program do the following in the program: 1. declare an vector without...
C++ Vectors. Create a program do the following in the program: 1. declare an vector without specifying the size 2. use push_back to add random integers between 100 and 999 to the vector 3. write a function that returns the smallest, largest, and average of the numbers in the vector display the smallest, largest, and average of the numbers in the vector
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT