James Wilks
James Wilks

Reputation: 740

Reading in unknown length of numbers

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++;
     }
}

Upvotes: 0

Views: 2413

Answers (4)

Mohsin Kokab
Mohsin Kokab

Reputation: 9

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.. :)

Upvotes: 0

IllusiveBrian
IllusiveBrian

Reputation: 3234

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

Upvotes: 1

jrd1
jrd1

Reputation: 10726

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

Upvotes: 1

Beta
Beta

Reputation: 99172

#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;
}

Upvotes: 0

Related Questions