Solved

input all the numbers on a line?

Posted on 2002-05-05
5
195 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now