CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2007
    Posts
    9

    Problem with arrays

    I am trying to take numbers from a text file and then do some math with them, but I am having some difficulty saving the numbers from the text to an array. Every time it saves a number from the array it replaces all the numbers in the array with the new number. I think that part of the problem is that the text file contains both text and numbers, so I have to call the input from the text char instead of int.

    #include <iostream>
    #include <fstream.h>
    #include <string>
    #include <cmath>
    using namespace std;

    int main()
    {

    int n;
    int m;
    int q;
    int pair;
    int p;
    double position;
    int w;

    char * x[2000];
    char * y[2000];
    char * z[2000];

    float theta;
    float l;
    float h;
    float r;
    double i;

    double dist[2];

    char output[10];


    //Opens the text document

    fstream protein;
    protein.open("c:\\Ferretin.txt",ios::in);
    if (!protein)
    {
    cout << "unable to open file\n";
    }

    position=0;

    //Takes words and numbers from the text document

    while (protein>>output)
    {
    //Takes the sixth number from each column of the text

    if (floor((position-5)/11)==(position-5)/11)
    {
    w=(position-5)/11;
    x[w]=output; //Saves the number to the array
    }

    cout << x[100]<<"\t"<<x[101]<<"\n"; //x[100] changes everytime the code is looped, but it should stay the same.

    if (floor((position-6)/11)==(position-6)/11)
    {
    w=(position-6)/11;
    y[w]=output; //Gets cartesian coordinates
    }

    if (floor((position-7)/11)==(position-7)/11)
    {
    w=(position-7)/11;
    z[w]=output; //Gets cartesian coordinates
    }

    position=position+1;
    }

    protein.close();

    //The code below this point seems to work fine.

    q=1;

    l=1;

    for (n=1;n<10;n=n+1)
    {
    for (m=n+1;m<10;m=m+1)
    {
    dist[q]=sqrt(double(x[n]-x[m])*(x[n]-x[m])+(y[n]-y[m])*(y[n]-y[m])+(z[n]-z[m])*(z[n]-z[m]));
    cout <<dist[q] <<"\t"<< q<< "\t"<<n<<"\t"<< m<<"\t"<< x[w]<<"\t"<< x[1401]<<"\n";
    q=q+1;
    };
    };


    theta=0.01;
    cout << theta << "\n";
    for (theta=0.001;theta<1.570796;theta=theta+0.1)
    {
    i=0;
    h=4*3.1415926*sin(theta)/l;
    for (pair=1;pair<q;pair=pair+1)
    {
    i=i+4*3.1415926*sin(h*dist[pair])/(h*dist[pair]);
    };
    cout << dist[1] << "string" << i << "\n";
    ofstream scattering("c:\\scattering.txt", ios::app);
    scattering << i << "\n";
    };



    cin >> p;
    cout << dist[q] << "string" << i << "\n";
    return 0;
    }

  2. #2
    Join Date
    Dec 2006
    Posts
    154

    Re: Problem with arrays

    your x y & z variables are arrays to pointers, so each time set a value you put in it a pointer. You end up setting the same value each time, a pointer to the buffer named output. This output is then erased to be replaced by something else, so all the fields point to this buffer and this buffer is erased.

    A good way to solve this would be to use string instead of char* & vector instead of arrays, that way you wont have to play with pointers & buffers.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Problem with arrays

    Another thing. Don't ever do this:
    Code:
    #include <iostream>
    #include <fstream.h>
    You are mixing the standard I/O header with a non-standard I/O header. Use all standard headers:
    Code:
    #include <iostream>
    #include <fstream>
    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Jan 2003
    Posts
    615

    Re: Problem with arrays

    Please use code tags.

    Btw, what does this expression do;
    Code:
    if (floor((position-5)/11)==(position-5)/11)
    {
    
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured