Apr 7, 2020 at 2:06am UTC
hello guys can you please please help me out.. iven stuck with this code for hours .......
the code is about reading data from the input file and sorting it by 8 char array. apparently there's no error message but the output is not okay
#include<iostream>
#include <string>
#include <fstream>
using namespace std;
bool SameGene(char array1[], char array2[], int size);
int main()
{
const int size = 444;
bool status;
char inchar;
char firstperson[size];
char secondperson[size];
char thirdperson[size];
char forthperson[size];
char firstperson1[size];
char firstperson2[size];
char secondperson1[size];
char secondperson2[size];
char thirdperson1[size];
char thirdperson2[size];
char forthperson1[size];
char forthperson2[size];
ifstream inFile;
ofstream outFile;
inFile.open("dna.txt");
outFile.open("DNAanalysis.txt");
if (!inFile)
{
cout << " Error opening input file. exiting..." << endl;
return 1;
}
if (!outFile)
{
cout << "Error opening output file. exiting... " << endl;
return 1;
}
while(!inFile.eof())
{
for (int i = 0;i < size;i++)
{
inFile >> firstperson1[8 * i];
for (int i = 1;i < size;i++)
inFile >> firstperson2[(8 * i) - 7];
for (int j = 0;j < size;j++)
{
firstperson[j] = firstperson1[8 * i] + firstperson2[(8 * i) - 7];
}
}
if ((firstperson1[20] == 'T') || (firstperson2[20] == 'T'))
outFile << " Firstperson is a carrier. " << endl;
else if ((firstperson1[20] == 'T') && (firstperson2[20] == 'T'))
outFile << " Firstperson is anemic " << endl;
else
outFile << " Firstperson is normal " << endl;
for (int i = 1;i < size;i++)
{
inFile >> secondperson1[(8 * i) - 6];
inFile >> secondperson2[(8 * i) - 5];
inFile >> thirdperson1[(8 * i) - 4];
inFile >> thirdperson2[(8 * i) - 3];
inFile >> forthperson1[(8 * i) - 2];
inFile >> forthperson2[(8 * i) - 1];
for (int a = 0;a < size;a++)
secondperson[a] = secondperson1[(8 * i) - 6] + secondperson2[(8 * i) - 5];
for (int b = 0;b < size;b++)
thirdperson[b] = thirdperson1[(8 * i) - 4] + thirdperson2[(8 * i) - 3];
for (int c = 0;c < size;c++)
forthperson[c] = forthperson1[(8 * i) - 2] + forthperson2[(8 * i) - 1];
}
if ((secondperson1[20] == 'T') || (secondperson2[20] == 'T'))
outFile << " secondperson is a carrier. " << endl;
else if ((secondperson1[20] == 'T') && (secondperson2[20] == 'T'))
outFile << " secondperson is anemic " << endl;
else
outFile << " secondperson is normal " << endl;
if ((thirdperson1[20] == 'T') || (thirdperson2[20] == 'T'))
outFile << " thirdperson is a carrier. " << endl;
else if ((thirdperson1[20] == 'T') && (thirdperson2[20] == 'T'))
outFile << " thirdperson is anemic " << endl;
else
outFile << " thirdperson is normal " << endl;
if ((forthperson1[20] == 'T') || (forthperson2[20] == 'T'))
outFile << " forthperson is a carrier. " << endl;
else if ((forthperson1[20] == 'T' && forthperson2[20] == 'T'))
outFile << " forthperson is anemic " << endl;
else
outFile << " forthperson is normal " << endl;
if (SameGene(firstperson, secondperson, size) == true)
outFile << " first Person is related to second Person. " << endl;
else
outFile << " not related to each other. " << endl;
if (SameGene(firstperson, thirdperson, size) == true)
outFile << " first person is related to thirdperson. " << endl;
else
outFile << " not related to each other. " << endl;
if (SameGene(firstperson, forthperson, size) == true)
outFile << " first person is related to forthperson. " << endl;
else
outFile << " not related to each other. " << endl;
if (SameGene(secondperson, thirdperson, size) == true)
outFile << " second person is related to thirdperson. " << endl;
else
outFile << " not related to each other. " << endl;
if (SameGene(secondperson, forthperson, size) == true)
outFile << " second person is related to forthperson. " << endl;
else
outFile << " not related to each other. " << endl;
if (SameGene(thirdperson, forthperson, size) == true)
outFile << " thirdperson is related to forthperson. " << endl;
else
outFile << " not related to each other. " << endl;
}
inFile.close();
outFile.close();
return 0;
}
bool SameGene(char array1[], char array2[], int size)
{
bool status;
for (int f= 0;f< size;f++)
if (array1[f]== array2[f])
{
status = true;
}
else
{
status = false;
}
return status;
}
Last edited on Apr 7, 2020 at 2:09am UTC
Apr 7, 2020 at 2:20am UTC
sorrry the output should be four people are all carrier
the out put now says all for people is normal.
Apr 7, 2020 at 2:29am UTC
You should edit your original post to have code tags as I showed, makes it a lot easier to help.
Also, the contents of the input file could shed light.
Last edited on Apr 7, 2020 at 2:31am UTC