Extracting ints and chars together

  • Thread starter Thread starter auk411
  • Start date Start date
AI Thread Summary
To extract three integers (hours, minutes, seconds) and two characters (colons) from a time input in the format HH:MM:SS in C++, the preferred method is using `scanf` for its ability to parse without whitespace. An alternative C++ approach utilizes `std::cin` with a character variable to capture the colons, but it relies on the input format being correct. A more structured solution involves overloading the extraction operator for a custom `Time` class, allowing for cleaner code and better encapsulation. The discussion highlights that while the C-style method is still valid in C++, it may be more efficient for this specific task. Overall, the choice of method depends on the desired balance between simplicity and adherence to C++ conventions.
auk411
Messages
54
Reaction score
0

Homework Statement



I need to know how, in C++, to extract 3 integers and 2 chars. In other words, I need to extract from the keyboard this data: HH:MM:SS, where the HH, MM, and SS are integers (standing for hours, minutes, and seconds). The two semicolons are chars. There are NO spaces, returns, other whitespaces. So when the user enters the appropriate numbers into HH:MM:SS how do I extract the information keeping the hours as the hours, the chars as the chars, minutes as the minutes, and seconds as the seconds.

Homework Equations



cin can't be the answer because it delimits using whitespace, and there is no whitespace. I also haven't been taught how to convert strings to ints. So I doubt that is the way the prof
wants us to go.

The Attempt at a Solution


 
Physics news on Phys.org
Here's the C style of doing this.
Code:
#include <stdio.h>
.
.
.
int hrs, mins, secs;
printf("Enter the time as HH:MM:SS.\n");
scanf(" %d:%d:%d", &hrs, &mins, &secs);
.
.
.

BTW, ':' is a colon. A semicolon is this character - ';'
 
The scanf-solution is my preferred solution, although I'd check if the return-value of scanf() equals 3 (the number of fields succesfully parsed).

Here's an alternate, more C++ like, solution:

Code:
int hrs, mins, secs;
char ch;
std::cout << "Enter the time as HH:MM:SS." << std::endl;
if (std::cin >> hrs >> ch >> mins >> ch >> secs)
{
   ...
}

This one is less neat, because it depends on the format of the input string to be correct.

[EDIT]Note that the more C++ like way of doing things is in this case is worse the the old way to do it!
Also note that the old C style way still works in C++![/EDIT]
 
Here's a fancy way of doing it by overloading the >> operator. All in all, a fairly elegant solution, IMO. The ignore method comes in handy. You can also overload the << operator to print it out, of course.

Code:
#include <iostream>
#include <iomanip>

class Time
{
public:
    int hours, minutes, seconds;
    friend std::istream &operator >> (std::istream &is, Time &t);
};

std::istream &operator >> (std::istream &is, Time &t)
{
        is >> t.hours;
        is.ignore(1,':');
        is >> t.minutes;
        is.ignore(1,':');
        is >> t.seconds;
        return is;
}

int main(int argc, char *argv[])
{
    Time t;
    std::cin >> t;

    std::cout << std::setw(2) << std::setfill('0') << t.hours << ":"
              << std::setw(2) << std::setfill('0') << t.minutes << ":"
              << std::setw(2) << std::setfill('0') << t.seconds << '\n';

    return 0;
}
 
Last edited:
Thread 'Have I solved this structural engineering equation correctly?'
Hi all, I have a structural engineering book from 1979. I am trying to follow it as best as I can. I have come to a formula that calculates the rotations in radians at the rigid joint that requires an iterative procedure. This equation comes in the form of: $$ x_i = \frac {Q_ih_i + Q_{i+1}h_{i+1}}{4K} + \frac {C}{K}x_{i-1} + \frac {C}{K}x_{i+1} $$ Where: ## Q ## is the horizontal storey shear ## h ## is the storey height ## K = (6G_i + C_i + C_{i+1}) ## ## G = \frac {I_g}{h} ## ## C...
Back
Top