0

I have an input file that contains some data in coordinate mode For example (2,3,5) translates to column 2, row 3, and level 5. I'm curious on a method of reading in the numbers after using getline(cin,string) to obtain the data. I don't know how many digits are in the data points so i can't assume the 1st character will be of length 1. Is there any libraries that can help solve the problem faster?

my gameplan so far that's not finished

void findNum(string *s){
int i;
int beginning =0;
bool foundBegin=0;
int end=0;
bool foundEnd=0
    while(*s){
        if(isNum(s)){//function that returns true if its a digit
             if(!foundBegin){
                 foundBegin=1;
                 beginning=i;
             }

        }
        if(foundBegin==1){
             end=i;
             foundBegin=0;
        }
        i++;
     }
}
James Wilks
  • 740
  • 1
  • 8
  • 20

4 Answers4

1

Try this:

#include <iostream>
#include <cstdlib>
#include <sstream>
#include <vector>
#include <string>

int main() {
    std::vector <std::string> params;

    std::string str;
    std::cout << "Enter the parameter string: " << std::endl;
    std::getline(cin, str);//use getline instead of cin here because you want to capture all the input which may or may not be whitespace delimited.

    std::istringstream iss(str);

    std::string temp;
    while (std::getline(iss, temp, ',')) {
        params.push_back(temp);
    }

    for (std::vector<std::string>::const_iterator it=params.begin(); it != params.end(); ++it) {
        std::cout << *it << std::endl;
    }

    return 0;
}

The only caveat is that the arguments will have to be non whitespace delimited.

Example input string:

1,2,3

Output:

1
2
3

Once these arguments have been parsed, you can then convert them from strings to (example) integer via the following:

template <typename T>
T convertToType(const std::string &stringType) {
    std::stringstream iss(stringType);
    T rtn;
    return iss >> rtn ? rtn : 0;
}

which can be used as follows:

int result = convertToType<int>("1");//which will assign result to a value of 1.

UPDATE:

This now works correctly on whitespace delimited input (except for newlines) like the following:

1 , 2, 3 ,  4

Which yields:

1
2
3
4
jrd1
  • 10,358
  • 4
  • 34
  • 51
  • Why do they need to be non-whitespace delimited? Don't they just need to be uniformly delimited in a known way (IE, whatever is put in as a delimiting character for getline). – IllusiveBrian Sep 28 '13 at 02:00
  • @Namfuak: You're correct about `getline`. The reason why my example has to be white-space delimited is because of `cin`. I'll edit it to reflect a more correct version. – jrd1 Sep 28 '13 at 02:02
  • Oh, I see what you are saying. Perhaps it would make more sense to use cin.getline() if for whatever reason the OP needs space support (for example, if he wanted to allow input like "1, 2, 3"). – IllusiveBrian Sep 28 '13 at 02:06
  • @Namfuak: `cin.getline()` requires a fixed length so as to determine when to stop capturing. `getline` is more appropriate here as it can capture an indefinite amount of whitespace delimited input (except newlines) - which I think is more appropriate here given the specification of the problem. But, thank you for your suggestion nonetheless! – jrd1 Sep 28 '13 at 02:14
1

jrd1's answer is pretty good, but if you'd prefer there happen to be functions for converting characters to integers (and back) already in the C standard library (cstdlib). You'd be looking for atoi.

http://en.cppreference.com/w/cpp/string/byte/atoi

IllusiveBrian
  • 3,105
  • 2
  • 14
  • 17
0
#include <sstream>

void findNums(const string &str, int &i, int &j, int &k)
{
  std::stringstream ss(str);
  char c;
  ss >> c >> i >> c >> j >> c >> k;
}
Beta
  • 96,650
  • 16
  • 149
  • 150
0

Simply use extractor operator for reading any type of value in respective variable type.

#incude<ifstream> // for reading from file
#include<iostream>

using namespace std;
int main()
{   

     int number;
     ifstream fin ("YouFileName", std::ifstream::in);

     fin >> number;   // put next INT no matter how much digit it have in         number
     while(!fin.eof())
     {
          cout << number << endl;
          fin >> number;   // put next INT no matter how much digit it have in     number and it will ignore all non-numeric characters between two numbers as well.      
     }
     fin.close();
     return 0;

}

Have a look over here for more details.

Note: Be careful while using it for character arrays and strings.. :)