Solved

input all the numbers on a line?

Posted on 2002-05-05
5
200 Views
Last Modified: 2010-04-02
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
0
Comment
Question by:vlg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:ruff_ryder
ID: 6990497
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
0
 
LVL 4

Accepted Solution

by:
mblat earned 100 total points
ID: 6990501
#define MAX_STRING_LEN   1024
// you may have something different that ints here
std::vector<int>   vNumbers;

void ParseString(TCHAR* szString)
{
   TCHAR* pStopString,p;

   pStopString = p = szString;
 
   while(pStopString)
   {
      int nVal = strtod(p,&pStopString);
      vNumbres.push_back(nVal);
      p = pStopString;
      p++;  
   }
}

This may need some debugging , but gives you an idea.

Hope it helps....
0
 

Author Comment

by:vlg
ID: 6990523
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
0
 

Author Comment

by:vlg
ID: 6990571
mblat -

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

v
0
 
LVL 4

Expert Comment

by:mblat
ID: 6991463
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...
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

689 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question