Help with atoi.

Hello everyone, I'm a complete noob in c++, I can't figure out how to do this. I need to do a countdown timer, I already have the whole code, but I need to ask for the time in the same line and I dunno how to do it.

Actually my code is this:

1
2
3
4
5
6
7
8
9
  
    int h,m,s;

    cout << "Horas: ";
    cin >> h;
    cout << "Minutos: ";
    cin >> m;
    cout << "Segundos: ";
    cin >> s;


I need that the program recognize the time in the same line in format hh:mm:ss.

I've been researching and I really dont know how to do it, someone told me that I need to change string into integers using atoi or something like that.

I really hope that someone could help me ASAP, is kinda urgent.

Thanks in advance.
1
2
3
4
5
6
7
8
int h, m, s ;
char colon ;

cout << "enter time as hh:mm:ss: " ;

cin >> h >> colon >> m >> colon >> s ;

// ... 
Wow, thanks both. The link is not useful for me, since I have the code for the clock already, but tyvm.

And JLBorges, thanks that was really easy, lol. I tried it and is working like a charm, but... Is there any other way to do it using strings? I supposed to use atoi in the code =P

Thanks again.
> supposed to use atoi in the code

Read the data into c-style arrays of char, with std::istream::getline()

1
2
3
4
5
const std::size_t N = 16 ;
char hh[N], mm[N], ss[N] ;
const char delimiter = ':' ;
cin.getline( hh, N, delimiter ) ; // http://www.cplusplus.com/reference/istream/istream/getline/
// etc. 


Then convert to int with std::atoi() http://en.cppreference.com/w/cpp/string/byte/atoi
I didn't understand =(, as I said before, I'm really new at this, is my 3rd week practicing. =(
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int main()
{
    const std::size_t N = 16 ;
    char hh[N], mm[N], ss[N] ;
    const char delimitador = ':' ;
    
    std::cout << "tiempo de entrada como hh:mm:ss\n" ; 
    std::cin.getline( hh, N, delimitador ) ; 
    std::cin.getline( mm, N, delimitador ) ; 
    std::cin.getline( ss, N ) ; // delimiter is '\n' 
    
    const int h = std::atoi(hh) ;
    const int m = std::atoi(mm) ;
    const int s = std::atoi(ss) ;
    
    std::cout << "Horas: " << h << "  Minutos: " << m << "  Segundos: " << s << '\n' ; 
}

http://coliru.stacked-crooked.com/a/f3445c054f5043da
omg thank you tyvm =)
Topic archived. No new replies allowed.