Data input

Hi,
I have to write a program where my input data will be "send" like this:
5
2 43 5 54 5
3 54 5 34
43 4 12
32 3
6
The first numbers says how many rows there will be. So I decided to create tables/vectors for each row. The problem is that I dont really know how to read data separated with space/whitespace and how to determine is it was divided by end of line sing or the space. Please help me ;)
Last edited on
How to read single line and sum all whitespace delimited values:
1
2
3
4
5
6
7
8
9
#include <sstream>
//...
std::string temp;
std::getline(std::cin, temp); //Read whole string
std::istringstream line(temp); //Create input stream from string we read before
int i, sum = 0;
while(line >> i) //Read values until the end of line;
    sum +=i;
std::cout << i;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
std::string temp;
std::getline(std::cin, temp); //Read whole string
std::istringstream line(temp); //Create input stream from string we read before
int i, x, y;
cin >> y;
int* table = new int[y][y];
for(int z = 0; z <= y; z++)
{
 x=0;
 while(line >> i) //Read values until the end of line;
 {
  Table[x][z]=i;
  x++;
 }
}

Will it save my whole data in the table?
Last edited on
Nope, you are reading only single line.
My code shows how to read one line. Loop it over to read several lines.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
	int i, x, y;
	cin >> y;
	int* table = new int[y][y];
	//for(int z = 0; z <= y; z++)
	//{
		std::string temp;
		while(std::getline(std::cin, temp)) //Read whole string
		{
			std::istringstream line(temp); //Create input stream from string we read before
			x=0;
			while(line >> i) //Read values until the end of line;
			{
				Table[x][z]=i;
				x++;
			}
		}
	//}
}

Now it works?
Last edited on
Why don't you check it out :)
I would suggest you to use vectors if your table has different length rows.

Example code using C++11:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <sstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <limits>


int main() {
    int rows;
    std::cin >> rows;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::vector<std::vector<int>> table(rows);
    for(int i = 0; i < rows; ++i) {
        std::string temp;
        std::getline(std::cin, temp);
        std::istringstream line(temp);
        std::copy(std::istream_iterator<int>(line),
                  std::istream_iterator<int>(),
                  std::back_inserter(table[i]));
    }
    for(const auto& v: table) {
        for(int i: v)
            std::cout << i << ' ';
        std::cout << '\n';
    }
}
http://ideone.com/2rlWHK
I dont understand line 12 and (18,23) :(
Thanks ;) Just last question. How to make it work?
1
2
3
4
5
6
		long long** table = new long long*[n];
		for( int i = 0; i < n; i++ )
		{
			table[i] = new long table[4];
		}

error C2440: '=' : cannot convert from 'long *' to '__int64 *'
What im trying to do is 2 demensional array with dynamic 'n' and constant [4]. Somethink like long long table[n][4]
EDIT:
I made it look like that:
1
2
3
4
5
		long long **table = new long long*[n];
		for( int i = 0; i < n; i++ )
		{
			table[i] = new long long[4];
		}

Not sure if working still testing.
Last edited on
Some inconsistency:
1
2
3
long long** table
//...
new long table
My first dynamic array! :D I have final question I think :) Can you help me to transform this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "stdafx.h"
#include <iostream>
#include <sstream>
int main()
{
	int t;
	std::cin >> t;
	for ( int x = 0; x < t; ++x )
	{
		long n;
		std::cin >> n;
		long long **table = new long long*[n];//Nie małe tworzenie tablicy
		for( int i = 0; i < n; i++ )
		{
			table[i] = new long long[4];
		}
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		for( int y = 0; y < n; y++ )
		{
			std::string temp;
			std::getline(std::cin, temp);
			std::istringstream line(temp);
			for( int v=0; v < 4; v++ ) 
			{
				line >> table[y][v];
			}
			std::cout << table[y][0] << " " << table[y][1] << " " << table[y][2] << " " << table[y][3];
		}
	}
	std::cin.get();
}

I guess its working! Thanks for your help ;)
EDIT:
I had to change std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
into
std::cin.ignore(); and its working :D
Last edited on

1 important thing, you have a memory leak - you are not freeing the memory allocated for your 2D array.

closed account (j3Rz8vqX)
1
2
3
4
5
6
	//Free memory:
	for( int i = 0; i < n; i++ )
	{
		delete []table[i];//deletes 'n' embedded arrays
	}
	delete []table;//deletes outer array 
Topic archived. No new replies allowed.