Member Avatar for onaclov2000

So I guess my question is:

Can I grab an entire line using cin.getline(), and grab an entire line and ignore the whitespaces?

What I'm trying to do is grab the following line:
2 3
and I want to know how many numbers are in it?
OR maybe a better question would be how would I grab the 2 3 individually?

What I'm trying to do is grab 2 and 3 but if i use myfile >> number it grabs them one at a time, but I can't guarantee that the line will only contain 2 numbers (could be any number), and I want to use each line as inputs to a function....does that make sense?

(I'm attempting to solve the google codejam round 1 problem, where you basically take the numbers on the line square them and add them together, if the result is 1 then you're done, if the result is greater then 1 you split the two numbers again and do it all over again).

I just can't seem to figure out how to grab the two (or x) numbers so I can work with the data.....Once i have the data I think I can solve the problem fairly easy...LOL

Thank you for any help

Recommended Answers

All 4 Replies

Grab the whole line, then parse it. You can parse with string streams using the same syntax as cin:

string line;

getline(cin, line);

istringstream iss(line);
int value;

while (iss >> value) cout << value << '\n';

There's more than one way to do this. I think that since you don't know how many numbers there are on that line then the easiest way to achieve this would be to use an integer vector and a while loop. While there's data on the line add it to the vector. This way you have easy access to storage for a random number of integers and an easy way to access and manipulate them.

Member Avatar for onaclov2000

Thanks,
I will try these when I get home, I didn't realize I could "step through" a line by using the >> and a vector.

I am a little confused about:
istringstream iss(line);

I've never used it before, so just to clarify what you're saying is that we're assigning iss the line as a stream, then we can use our >> again on it?

That being the case line won't be able to use >> without doing that above line!

Again, Thank you,

Onaclov

With getline() you'll basically end up with a string containing all the numbers on the line. Then you can use an istringstream to convert each string in your line to the corresponding number individually. It'll work, but it just seems like the hard way to solve this problem. Take a look at my example program to see what I was thinking of.

//Read random number of ints from a file

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

int main()
{
	//Open file for reading, adjust filename to a real file
	ifstream infile;
	infile.open("data.txt");
	
	int temporary;
	vector<int> numbers;
	
	//Loop through file and fill in the vector
	while (infile >> temporary)
	{
		numbers.push_back(temporary); //add to end of vector
	}
	
	//Display contents of vector to screen
	for (int i = 0; i < numbers.size(); i++)
	{
		cout << "Number " << i << " -> " << numbers[i] << endl;
	}
	
	//Clean up and exit
	infile.close();
	return 0;
}

/* Output of program
 * 
 * Number 0 -> 1
 * Number 1 -> 3
 * Number 2 -> 5
 * Number 3 -> 9
 * Number 4 -> 88
 * /

Hope this helps you. If you have any questions about the code then feel free to shoot away.

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.