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 ;)
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;
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 = newint[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++;
}
}
int main()
{
int i, x, y;
cin >> y;
int* table = newint[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++;
}
}
//}
}
Thanks ;) Just last question. How to make it work?
1 2 3 4 5 6
longlong** table = newlonglong*[n];
for( int i = 0; i < n; i++ )
{
table[i] = newlong 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
longlong **table = newlonglong*[n];
for( int i = 0; i < n; i++ )
{
table[i] = newlonglong[4];
}
#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;
longlong **table = newlonglong*[n];//Nie małe tworzenie tablicy
for( int i = 0; i < n; i++ )
{
table[i] = newlonglong[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