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 .

Attachments
``````-1.987582064	-1.921697293	14.35348685
-1.980833597	-1.913173814	14.14457704
-1.974314672	-1.912827834	13.93937762
-1.968764429	-1.920306829	13.73815158
-1.964819209	-1.935282114	13.54116688
-1.963024959	-1.957455651	13.34869542
-1.963848987	-1.986564664	13.16101336
-1.967690945	-2.022384383	12.97840252
-1.974892957	-2.064729167	12.80115248
-1.985748808	-2.113452266	12.62956329
-2.000512192	-2.168444412	12.46394852
-2.019403981	-2.229631421	12.30463871
-2.042618535	-2.296970913	12.15198487
-2.070329065	-2.370448236	12.00636228
-2.102692053	-2.450071663	11.86817424
-2.139850764	-2.535866836	11.73785597
-2.181937858	-2.627870463	11.61587842
-2.229077116	-2.726123197	11.50275194
-2.281384279	-2.830661633	11.39902987
-2.338967	-2.941509312	11.30531168
-2.401923891	-3.058666618	11.22224566
-2.470342647	-3.18209947	11.15053094
-2.544297199	-3.311726674	11.09091852
-2.623843875	-3.447405847	11.04421092
-2.709016509	-3.588917874	11.01126032
-2.799820469	-3.735949873	10.99296428
-2.896225564	-3.888076782	10.99025895
-2.998157807	-4.044741764	11.00410882
-3.105490067	-4.205235801	11.03549243
-3.21803164	-4.368677047	11.08538336
-3.335516873	-4.533990754	11.15472575
-3.457593033	-4.699890874	11.24440366
-3.583807714	-4.864864768	11.35520388
-3.713596205	-5.027162787	11.48777179
-3.846269398	-5.184794825	11.64256061
-3.981002938	-5.335536208	11.81977454
-4.116828532	-5.476945448	12.01930742
-4.25262844	-5.606396373	12.24067903
-4.387134341	-5.721126798	12.48297228
-4.5189318	-5.818305319	12.74477581
-4.646471576	-5.895116687	13.02413689
-4.76808883	-5.948864744	13.31853084
-4.882031053	-5.977089992	13.62485296
-4.986495027	-5.977696698	13.93943884
-5.079672538	-5.949082195	14.25811764
-5.159803788	-5.890259109	14.57630079
-5.225236577	-5.800959937	14.88910581
-5.274488517	-5.681713136	15.19151106
-5.306308787	-5.533880952	15.47853387
-5.319735444	-5.359651728	15.74542064
-5.314144201	-5.161983231	15.98783545
-5.289284816	-4.944498267	16.20203218
-5.245302028	-4.711338828	16.38499636
-5.182739019	-4.466989465	16.53454528
-5.102522814	-4.216083855	16.64937915
-5.005932467	-3.963209991	16.72908106
-4.894552294	-3.71272899	16.77406861
-4.770213486	-3.468620205	16.78550465
-4.634928202	-3.234361764	16.76517782
-4.490820495	-3.012851303	16.71536488
-4.34005824	-2.806367423	16.63868743
-4.184789729	-2.616568666	16.53797345
-4.027087786	-2.444524106	16.41613238
-3.868903342	-2.290768085	16.27604945
-3.712029482	-2.155371141	16.12050196
-3.558076132	-2.038019586	15.95209823
-3.408454892	-1.938097249	15.77323763
-3.264372968	-1.854764252	15.58608907``````
3
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by WaltP

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.

Edited by rahul8590: n/a

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.

Edited by WaltP: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.