Solved

input all the numbers on a line?

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

707 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

18 Experts available now in Live!

Get 1:1 Help Now