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;
}