Link to home
Start Free TrialLog in
Avatar of vlg
vlg

asked on

input all the numbers on a line?

Hello

I need a function that will input a whitespace-delimited list of numbers on a line, stopping at, on or after the newline (just let me know which).

Something that, for example, creates a dynamic 1-d array, and reads the entire line, and then puts the numbers into it?

Then I would need the framework to call it within the context of reading a file (this assumes the dynamic array implementation, but I guess I'll need something similar for whichever way I end up using)-
while (there are more lines to read )
  createDynArraywithTheNumbersforThisLineInIt;
  //process that array
loop

thanks

v
Avatar of ruff_ryder
ruff_ryder
Flag of United States of America image

This is one way you can start:

int num = 0;
int array[50];

while(cin >> num)
   array[i] = num;


P.S. not compiled but you can debug from here
ASKER CERTIFIED SOLUTION
Avatar of mblat
mblat

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vlg
vlg

ASKER

ruff ryder -
Won't your code just keep skipping newlines and put all the numbers from the different lines into the same array?
I need the numbers from a line together.

mblat -

I'm sure your your code is great, but it's too advanced for me - I don't even know how to use it.  I would need some instructions as to how to use it -
It looks like all the values will be in a vector, so I need to know how to iterate through all the values in a vector.
Also, it looks like it wants a TCHAR* - what's that?  I'm using a simple getline or maybe a "Filename >>..." type thing - how can a get the line of numbers from the file into a TCHAR*?

Thanks

v
Avatar of vlg

ASKER

mblat -

I typed your code, and my compiler doesn't like TCHAR.

v
or we are in c++ programming section, not MFC.  
TCHAR is a thing.... that provides automatic support when you are switching between UNICODE and "normal" applications in Windows.

use char* instead...

as far as vector...
you don;t have to use it, you can put code in array of ints... or whatever type of vars. you need to use....

any way to iterate through vector you need

std::vector<int> iterator it;
std::vector<int> myV;
for(it = myV.begin();it != myV.end(); ++it)
{
 
  int nVal = *it;  // yes pointer to iterator
 
}

or you can use
for_each algorithm instead....

This is totally out of scope of this question now, but I it may be benificial to you to pick up STL book. STL is wonderfull library that allows you to do a lot things...

Hope it helps...