jeudi 24 octobre 2019

Identifying String or Int from ifstream

I'm trying to identify whether an input from a ifstream is an int or a string in C++11. the ifstream will give either a string or an int and I need to do different things for each. If it's an int, I need to use the first two as locations in a 2d array with the third as the value. If its a string, I need to create a NodeData object.

   for (;;) {

  int n1, n2, n3;
  string s1;
  infile >> s1;
  //trying isdigit and casting string to int
  if (isdigit( stoi(s1.c_str()))) {
     //check if last 2 ints exist
     if ((infile >> n2 >> n3)) {
        n1 = stoi(s1);
        //end of input check
        if (n1 == 0) {
           break;
        }

        C[n1][n2] = n3;
     }
  }
  else {
     NodeData temp = NodeData(s1);
     data[size] = temp;
     size++;
  }

} I have tried isdigit and several different types of casting but they haven't worked. It keeps thinking the number in the string is not an int when it is.

Aucun commentaire:

Enregistrer un commentaire