A string is entered : 22:13:44

It is entered together , with ":", just like that " 22:13:44 " It is necessary to process this line and take these numbers, that is, create 3 variables a, b, c And write the values ​​necessarily in integer variables. That is, the result should be int a = 22, b = 13, c = 44;

Tell me, please, the code that will divide this input into such variables.

I tried to do this, but I got confused in the types of variables and in their processing.

Closed due to the fact that off-topic participants HolyBlackCat , MBo , aleksandr barakin , 0xdb , Enikeyschik December 16, '18 at 16:16 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • " Learning tasks are allowed as questions only on the condition that you tried to solve them yourself before asking a question . Please edit the question and indicate what caused you difficulties in solving the problem. For example, give the code you wrote, trying to solve the problem "- HolyBlackCat, MBo, aleksandr barakin, 0xdb, Enikeyschik
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    “I tried to do this, but I got confused.” So show your code. - HolyBlackCat
  • one
    @ Vladislav, no one will write code for you completely. Here you can be helped, point out the error, and not write all the code for you. Whatever code is bad, people can push off from it and help solve your problem. - Aqua
  • int a, b, c; char delim; cin >> a >> delim >> b >> delim >> c; - Drawn Raccoon
  • "write values ​​must be in integer variables" "should be int" "I tried to do this, but I got confused in the types of variables and in their processing" - Kel Fish

3 answers 3

A program for which you are indifferent to what you type. She will only read the first three numbers anyway:

 int numbers[3], i = 0; while (i < 3) { char smb = std::cin.peek(); if(smb >= '0' && smb <= '9') std::cin >> numbers[i++]; else std::cin.ignore(); } int a{numbers[0]}, b{numbers[1]}, c{numbers[2]}; std::cout << a << ", " << b << ", " << c; 

This is just one of many options.

    Start with the simplest code. If the positions of the characters are always the same, then you can write directly

     string s = "22:13:44"; int a = (s[0] - '0') * 10 + s[1] - '0'; 

    well or seconds

    int c = (s [6] - '0') * 10 + s [7] - '0';

    Rough, but completely working.

    You can go further and do something like this.

     int a = stoi(s.substr(0,2)); int b = stoi(s.substr(3,2)); 

    (but if anything, this is with ++ 11 and older. But if your compiler does not support this, then I have bad news for you).

       #include <iostream> using namespace std; int main(){ int a, b, c; cin >> a; cin.ignore(); cin >> b; cin.ignore(); cin >> c; cout << a << endl << b << endl << c; return 0; } 

      Simple varinat. At the exit you get three numbers. Between the latter can be absolutely any character. Tov. AR Hovsepyan above offered a more versatile solution, since between the numbers can be any number of characters.