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 .