7 Replies - 693 Views - Last Post: 29 October 2010 - 09:03 AM Rate Topic: -----

#1 Guest_Patric*


Reputation:

[C++]Reading ints from line

Posted 23 October 2010 - 05:55 AM

Hello,

I've problem with reading ints from line. The user w'll enter in console numbers separate with space something like this:

Quote

1 20 25 63 0
63 25 65 2 1 35 220 0


The line ends on number 0. The number of ints in each line is beetween 0 and 1000. So I need solution, which get me ints to the array:

Quote

int tab[10][10];


I know there is function getline. But how convert strings to ints with good performance?

Thanks for any help?

Is This A Good Question/Topic? 0

Replies To: [C++]Reading ints from line

#2 newclearner  Icon User is offline

  • D.I.C Regular

Reputation: 103
  • View blog
  • Posts: 302
  • Joined: 29-September 10

Re: [C++]Reading ints from line

Posted 23 October 2010 - 06:15 AM

Can we see your code, if you have written any?
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6048
  • View blog
  • Posts: 23,473
  • Joined: 23-August 08

Re: [C++]Reading ints from line

Posted 23 October 2010 - 06:18 AM

stringstream
Was This Post Helpful? 1
  • +
  • -

#4 Guest_Patric*


Reputation:

Re: [C++]Reading ints from line

Posted 23 October 2010 - 09:12 AM

I write this:
#include <iostream>
#include <string>
using namespace std;

int main()
{
	int tab[100][1000];
	unsigned long sum;
	int i = 0, j;
	string line;
	while(getline(cin, line))
	{
		j = 0;
		while(line[line.length() - 1]  != '0')
		{
			tab[i][j] = atoi(line);
		}
		++i;
	}
	return 0;
}



But atoi doesn't work on strings.

JackOfAllTrades could you tell me how to use this?

Thanks for help.

This post has been edited by macosxnerd101: 23 October 2010 - 09:15 AM
Reason for edit:: Please use CODE tags, not QUOTE tags when posting code.

Was This Post Helpful? 0

#5 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: [C++]Reading ints from line

Posted 23 October 2010 - 07:00 PM

Click here
http://www2.research...bs/bs_faq2.html

Scroll down to this heading
"How do I convert an integer to a string?"

That shows how to do the opposite of what you want to do.
Reverse the logic and you'll have what you want.
Was This Post Helpful? 0
  • +
  • -

#6 Guest_Patric*


Reputation:

Re: [C++]Reading ints from line

Posted 28 October 2010 - 11:12 AM

I write something like this:
int itos(string s)
{
	istringstream is(s);
	int tmp;
	is>>tmp;
	return tmp;
}

int main()
{
	string s = "1234 234 536";
	cout<<itos(s)<<endl;
}


But now I have problem how to get the rest numbers from string? Now I get 1234 but nothing more. How to get the rest numbers? (234, 536)??

I thinking about clearing part of string which I read and convert again to int in loop while string length > 0.

Thanks for help but I have still some problem with this exercise.
Was This Post Helpful? 0

#7 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: [C++]Reading ints from line

Posted 28 October 2010 - 03:21 PM

Split the string into an array of strings (using whitespace as the delimiter).
You could use a stringstream to do that too if you wanted but do it any way you feel good about.

Then loop through the string array and send the strings one by one to your function and load your int array with the returned ints.
Was This Post Helpful? 0
  • +
  • -

#8 Guest_Patric*


Reputation:

Re: [C++]Reading ints from line

Posted 29 October 2010 - 09:03 AM

Thank you very much. Now I know how to do this exercise :)

Thanks for all :)
Was This Post Helpful? 0

Page 1 of 1