Well ,it may sound a bit preposterous , but i have a pretty weird problem (which i have solved many times , dunno why it isnt working for me this time ) .

I have a data file from which i randomly pick up row nos . The data file has list of integers , actually they are co ordinates of a mathematical equations .

I only extract out the numbers from them and filter out the -ve symbols and decimals .
I am able to print those extracted numbers on screen , but not able to transfer the number as it is into another integer array.

#include <iostream>
#include <fstream>
#include <string>




using namespace std;

int main () {
  string  line;
  int linesize , guess;

  srand ( time(NULL) );                               // For random line to be choosen
  guess = rand() % 10 + 1;                             // this random number must be a multiple of 39
  guess = guess * 39 ;
  cout<<" the randomly choosen line number is "<<guess/39<<endl;



  ifstream myfile ("data.txt");
  if (myfile.is_open())
  {
                   myfile.seekg (guess, ios::beg);   // positioing the start of each line to fetch all the coordinates
      getline (myfile,line);
      cout << line << endl;
}

else cout << "Unable to open file";

  linesize = line.size();

  int aint[linesize];               // The arrays satisfying the condition  
                                            //in integer form


  cout<<" \n \n the char array of the same is "<<endl;

char *a=new char[line.size()+1];  // Conversion of string to char  
a[line.size()]=0;                             //array
memcpy(a,line.c_str(),line.size());



  for ( int i = 0 ; i<linesize; i++)
  if (a[i] >= '0' && a[i] <= '9')            //extracting only nos and    
                                                     //saving  in integer array 
  {
      aint[i] = a[i] - '0';
      cout<<aint[i] ;
  }



    cout<<endl;


    cout<<aint[0]<<aint[1] ;    // printing undesired values

getchar();



  return 0;
}

Although . i have transferred all the characters from character array to integer specifying the condition , i am getting weird numbers when i am trying to print the content of the integer array . I dont have the slightest inkling on why is it happening.


PS:
Please use the data file attached to it as a reference to extract , since the random extraction is strictly based on the data file .

Recommended Answers

All 5 Replies

When you loop through the line you convert only the digits. But if one of the locations is not a digit, not only do you skip it in the source string, but you also skip the same position in the destination array -- leaving uninitialized data in that position.

Kudos to you ,

I could diagonise and fix it correctly , but have some memory issues like the rest of the integer array is holding some arbitrary numbers and i would to truncate the array till extracted digits.


Srry ,
I solved that issue too .
Now , if i want to put this in a header file and call it in some other program , how do i go about doing that.

aint array must be allocated with new operator. The way you tried to do it is not yet supported by most c++ compilers.

linesize = line.size();

  int *aint = new int[linesize];               // The arrays satisfying the condition

Well it runs in MinGW and i am very happy with that , Certainly for my seminal project ideas i would keep that in mind.

Put the code in a separate cpp file as a function, create a header file that includes only the function prototype.

In the 'other' source file, include the header. Compile the two source files together and viola -- a dual-source program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.