I need to end the input loop by hitting the enter key. Tried to find something, and I got some guy here saying that this code below will work, sadly it doesn't. What's wrong?
#include <iostream> #include <sstream> using namespace std; int main() { int a = 0, h = 0, i=0; string line; int *tab=new int; while (getline(cin, line) && line.length() > 0) // line not empty { stringstream linestr(line); while (linestr >> a)// recommend better checking here. Look up std::strtol { tab[i]=a; } i++; } return 0; } Go it, thanks!
Here's the code:
#include <iostream> #include <sstream> using namespace std; int main() { int a = 0, i=0; string line; getline(cin, line); stringstream linestr(line); int *tab = new int[line.size()/2+1]; while ( linestr >> a ) { tab[i]=a; i++; } for(int j=0; j<i; j++) cout<<tab[j]<<" "; return 0; }
int *tab=new int;thentab[i]=a;.... oh boy, you're about to have a bad time