Solved

input all the numbers on a line?

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel/Word Add-in in what language? 5 128
Compile GLUT with Visual Studio 2015 1 171
Create a path if not exists 7 89
How to Correctly derive class from CWinThread in MFC 4 64
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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

856 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