I’m having trouble entering data from a file into an integer and character array. Basically, in this file, some the data I want transferred into a character array. Other parts, I want transferred into an integer array.

I am able to get data into the arrays, and the char array is owkring perfectly, but when it pulls in the integers, it thinks "23" is 2 and 3 and ends up putting 51 into the integer array. I think it doing this because it using the asci codes for the char 2 and 3 (I think), but how do I get it to put in the inger 23 (then 1, 0, 55, 1, as in the data file below).

A typical data file would look something like this:

>Title Below are the column names then population names and then integer data
A B C D X
>Embera
23 1 0 55 1
>Ingano
44 37 0 4 0
>Piou
56 76 5 4 5

The program looks like this:

#include <iostream>
#include <fstream>  //Provides input and output classes
using namespace std;

int main()
{


ifstream HAPLOin("HaploTb.txt");
if (!HAPLOin)
        {
        cout << "File not found.\n";
        cout << "Press enter to exit.\n"; getchar();
        return 0;
        }

const int Haplos=10;
const int Pops=4;

char Header [10][80]={""};
char IDname[Pops][Haplos]={""};
int Haplo[Pops][Haplos]={0};

int r1=0;
int c1=0;
int r2=0;
int c2=0;
int r3=0;
int c3=0;
int z=0;
int i=0;
int j=0;
char ch='a';
                                //counter for # of variable sites

while (HAPLOin.get(ch) && z<2)
        {
        if (ch=='>')
          {z++;}    // get data while loop

        if (z==1 && ch!='>' && ch!='\t')
          {Header[r1][c1]=ch; c1++;}

        if (ch==10 && z==1)
          {r1++; c1=0;}
        }

IDname[i][j]=ch;
j++;

while (HAPLOin.get(ch))
{
  while (HAPLOin.get(ch) && ch!=10)
        {

        if (ch!='>' && ch!='\t')
          {IDname[r2][c2]=ch; c2++;}
        }
r2++;
c2=0;
  while (HAPLOin.get(ch) && ch!=0)
        {
        if (ch!='>' && ch!='\t' && ch!='0')
          {Haplo[r3][c3]=ch; c3++;}
        }
r3++;
c3=0;
}

cout << "next\n\n";


for(r1=0;r1<Pops;r1++)
  for(c1=0;c1<80;c1++)
       if (Header[r1][c1]!=0)
       {cout << Header[r1][c1];}
cout << "\n\n";
for(r1=0;r1<Pops;r1++)
  for(c1=0;c1<80;c1++)
        if (IDname[r1][c1]!=0)
        {cout << IDname[r1][c1] << " ";}
cout << "\n\n";
for(r1=0;r1<Pops;r1++)
  for(c1=0;c1<80;c1++)
        if (Haplo[r1][c1]!=0)
        {cout << Haplo[r1][c1] << " ";}
cout << "\n\n";

getchar();
        return 0;
}

the digits in the file have to be converted to binary when inserting them into an int array. For example the string "123" can be converted to it like this

char nm[] = "123";
int n = 0;
for(i = 0; nm[i]; ++i)
  n = (n * 10) + nm[i] - '0';

you can also use scanf

char nm[] = "123";
int n = 0;
sscanf(nm,"%d",&n);

When dealing with an fstream, like above, is there any way to use the filename.get or filename.getline command with integers?

since get and getline write to a char*, just use atoi, for example

#include <stdlib.h>
int main(void)
{
     char *buf1 = "42";
     char buf2[] = "69.00";
     int i;
     double d;
     long l;
     i = atoi(buf1);
     l = atol(buf1);
     d = atof(buf2);
     return 0;
}

taken from here

the problem with using getline is that it doesn't separate the integers. using your example "23 1 0 55 1" there are 5 integers. but you could use extraction operator to separate them

string n;
in >> n;
int num = atoi(n.c_str());
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.