5 Replies - 1484 Views - Last Post: 08 October 2012 - 06:18 AM Rate Topic: -----

#1 Martijn13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-October 12

Splitting numbers in strings to ints

Posted 07 October 2012 - 09:24 AM

Hello there!



I have the following problem:

I want to write a program that gets the time in the form hours:minutes via stdin, and then give output like this. The problem for me is: how do I split the input to two integers for hours and minutes?

I have been trying to find a solution for almost a hour. Every time I found code that didn't work for me, and that I didnt understand.

Please note: I'm just starting to learn C++. Please explain your answer extensively.

To add some information (can't find an edit button)

I have found solution using vectors, find_first_of and boost libaries. I just didn't understand.

Is This A Good Question/Topic? 0
  • +

Replies To: Splitting numbers in strings to ints

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3105
  • Posts: 10,903
  • Joined: 08-August 08

Re: Splitting numbers in strings to ints

Posted 07 October 2012 - 09:48 AM

Read up on sstream:
http://www.cplusplus...m/stringstream/
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4292
  • View blog
  • Posts: 13,458
  • Joined: 25-December 09

Re: Splitting numbers in strings to ints

Posted 07 October 2012 - 09:59 AM

Quote

I have been trying to find a solution for almost a hour. Every time I found code that didn't work for me, and that I didnt understand.

Quote

Please note: I'm just starting to learn C++.

Then I suggest you try to write your own solution. You will not learn anything by trying to "find" a solution. You learn programming by programming. So show us your best effort at solving this problem and we may be able to help point you in the right direction.


Jim
Was This Post Helpful? 0
  • +
  • -

#4 Martijn13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-October 12

Re: Splitting numbers in strings to ints

Posted 08 October 2012 - 04:54 AM

Well, Jim got me thinking. Eventually I found out about strncpy(). This is what I came up with:

int main()
{
    char time[5], hour[2];
    int hour_num;
    cout << "give time"<<endl;
    cin >> time;
    cout << " "<<endl;

    strncpy(hour,time,2);
    hour[2]='\0';
    cout << hour <<endl;

    hour_num = atoi(hour);

    cout << hour_num <<endl;

    return 0;
}




That returns the first two characters of time[]. Now I have another question: how do I get the last two characters of time[]? Or to be more general: how do I get any part of an array? I can't figure this out.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3105
  • Posts: 10,903
  • Joined: 08-August 08

Re: Splitting numbers in strings to ints

Posted 08 October 2012 - 06:13 AM

Arrays start at 0, so the first three elements of time would be time[0], time[1], and time[2].
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4292
  • View blog
  • Posts: 13,458
  • Joined: 25-December 09

Re: Splitting numbers in strings to ints

Posted 08 October 2012 - 06:18 AM

I suggest you start by studying the following links: Arrays, Character Sequences. I also suggest that instead of the C-string you use std::string. By using the string class you won't need to worry about not having enough size for your string, and you will have functions like substr() to help pick out a section of the string.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1