Hello,

let's say the user enters a value in this format xx:xx:xx
where xx is an integer, how can I assign each xx to integer vars?
I though of everything I know..
can't use getline(),getchar() bcuz they accept characters only, can't use cin either..
thought of storing the xx:xx:xx on an array but don't know if it's possible to convert char variable to int variable..

for example, the user enters 55:44:33
the program assignes 55,44,33 to three different variables based on a char that I define (:)
sorry if I'm not being clear enough..

any help?

Are you using C or C++ ?

Ok getline is C++.

Maybe you can hack something out of the examples in this link.

Actually there are many ways..
To start with may be this will help: atoi() converts a char* to an int.

let's say the user enters a value in this format xx:xx:xx, where xx is an integer, how can I assign each xx to integer vars?

this is one way.

#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
using namespace std ;

int main()
{
  string str ;
  getline( cin, str ) ;
  replace( str.begin(), str.end(), ':', ' ' ) ;
  istringstream stm(str) ;
  int a, b, c ; 
  stm >> a >> b >> c ;
  cout << a << '\t' << b << '\t' << c << '\n' ; 
}

and this is another (using boost)

#include <iostream>
#include <string>
#include <boost/tokenizer.hpp>
#include <boost/lexical_cast.hpp>

int main()
{
  using namespace std;
  using namespace boost;
  string str ;
  getline( cin, str ) ;
  tokenizer<> tokens(str);
  tokenizer<>::iterator iter = tokens.begin() ;
  try
  {
    int a = lexical_cast<int>(*iter), b = lexical_cast<int>(*++iter), 
                 c = lexical_cast<int>(*++iter) ;
    cout << a << '\t' << b << '\t' << c << '\n' ;  
  }
  catch( const bad_lexical_cast& ) { cout << "error in input\n" ; }
}

three calls to getline() with the first two using : as delimeter and third using newline. Then convert each string obtained into numerical value using sprintf() or strtol() or atoi() or stringstream, etc.

three calls to getline() with the first two using : as delimeter and third using newline. Then convert each string obtained into numerical value using sprintf() or strtol() or atoi() or stringstream, etc.

if multiple reads can be used, the easiest is

#include <iostream>
int main()
{
  int a, b, c ;  char colon ;
  std::cin >> a >> colon >> b >> colon >> c ;
  std::cout << a << '\t' << b << '\t' << c << '\n' ;  
}

int a, b, c ; char colon ;
std::cin >> a >> colon >> b >> colon >> c ;

Don't have a compiler to test any code at the moment but I'd be concerned that cin will fail when trying to read the third char of the input (a colon) into the variable a (which is an int). At least that's why I suggested serial calls to getline instead of >>.

Thanks guys, your replies were really useful :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.