Extracting ints and chars together

  • Thread starter auk411
  • Start date
In summary, the C++ way to extract data from a keyboard is to use the >> operator to read in the hours, minutes, and seconds.
  • #1
auk411
57
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
  • #2
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 - ';'
 
  • #3
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]
 
  • #4
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:
  • #5


One possible solution would be to use the cin function to read in the entire input as a string, and then use string manipulation techniques to extract the individual pieces of data (hours, minutes, seconds). You could then use the stoi function to convert the string representation of the integers to actual integer values. Alternatively, you could use the scanf function with the appropriate format specifier to directly read in the integers and chars from the keyboard. It is important to note that when using scanf, you will need to include a space in the format specifier before the semicolons to account for the lack of whitespace in the input. For example, your format specifier could be "%d:%d:%d ;%c ;%c" to correctly extract the data in the format of HH:MM:SS.
 

FAQ: Extracting ints and chars together

How do you extract integers and characters together from a data source?

There are a few ways to extract both integers and characters from a data source. One method is to use a regular expression to specify the pattern of the data you want to extract. Another method is to use a combination of string manipulation and type conversion to extract the desired data.

Why is it important to extract both integers and characters together?

In some cases, data sources may contain both integers and characters that are related to each other or provide important context. Extracting both types of data together allows for a more complete understanding and analysis of the data.

Can you give an example of extracting ints and chars together in a real-world scenario?

Sure, let's say you have a text file containing a list of employees and their salaries. By extracting both the employee names (characters) and their salaries (integers), you can compare and analyze the data to see if there is any correlation between employee names and their salaries.

What challenges may arise when extracting ints and chars together?

One challenge may be ensuring that the data is extracted accurately and without any errors or discrepancies. This may require careful testing and troubleshooting to ensure the correct data is being extracted.

Are there any tools or libraries that can assist with extracting ints and chars together?

Yes, there are various tools and libraries available such as regular expression libraries and string manipulation libraries that can help with extracting both integers and characters together. It may also be helpful to use a programming language that has built-in functions or methods for extracting data, such as Python's "re" library for regular expressions.

Similar threads

Back
Top