Extracting ints and chars together

  • Thread starter Thread starter auk411
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around extracting three integers and two characters from a user input formatted as HH:MM:SS in C++. Participants explore various methods to achieve this, considering both C and C++ styles of input handling.

Discussion Character

  • Homework-related
  • Technical explanation

Main Points Raised

  • One participant describes the need to extract integers and characters from a specific input format without whitespace, expressing uncertainty about using standard input methods like cin.
  • Another participant suggests using the C-style scanf function to parse the input, emphasizing the importance of checking the return value to ensure successful parsing.
  • A different approach is proposed using C++ style input with std::cin, which relies on the correct format of the input string but is noted to be less neat than the C-style method.
  • One participant introduces an advanced technique involving operator overloading in C++, providing a custom implementation for inputting time data as a class, which includes methods for formatting output.

Areas of Agreement / Disagreement

Participants express varying preferences for different methods of input handling, with some favoring the C-style approach and others advocating for C++ techniques. No consensus is reached on a single best method.

Contextual Notes

Some participants note the limitations of relying on input format correctness and the potential for errors if the input does not match expectations.

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:

Similar threads

  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 8 ·
Replies
8
Views
7K
  • · Replies 8 ·
Replies
8
Views
7K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
7
Views
3K
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K