Program is not displaying output correctly

Posted by Dave Lee on Stack Overflow See other posts from Stack Overflow or by Dave Lee
Published on 2013-11-11T09:50:34Z Indexed on 2013/11/11 9:52 UTC
Read the original article Hit count: 194

Filed under:
|

My program is suppose to display information from a text file. The text file is here http://pastebin.com/qB6nX2x4

I cant find the problem in my program. I think it has to deal with the looping but im not sure. My program runs correctly but only displays the first line of text. Any help would be appreciated.

#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>

using namespace std;

int buildArrays(int A[],int B[],int C[])
{


int i=0,num;

ifstream inFile;
inFile.open("candycrush.txt");

if(inFile.fail())
  {
  cout<<"The candycrush.txt input file did not open"<<endl;
  exit(-1);
  }
while(inFile)
{
inFile>>num;
A[i]=num;

inFile>>num;
B[i]=num;

inFile>>num;
C[i]=num;

i++;
}
inFile.close();

return i;
}
void printArrays( string reportTitle, int levelsArray[], int scoresArray[], int starsArray[], int numberOfLevels )
{
cout<<endl;
cout<<reportTitle<<endl;
cout<<"Levels\tScores\tStars"<<endl;
cout<<"---------------------"<<endl;

for(int i=0;i<numberOfLevels;i++)
{
cout<<levelsArray[i]<<"\t"<<scoresArray[i]<<"\t";
for(int j=0;j<starsArray[j];j++)
{
cout<<"*";
}
cout<<endl;
}
}


void sortArrays( int levelsArray[], int scoresArray[], int starsArray[], int numberOfLevels )
{
for(int i=0;i<numberOfLevels;i++)
{
for(int j=0;j<numberOfLevels;j++)
{
if(levelsArray[i]<levelsArray[j])
{
int temp1=levelsArray[i];
int temp2=scoresArray[i];
int temp3=starsArray[i];

levelsArray[i]=levelsArray[j];
scoresArray[i]=scoresArray[j];
starsArray[i]=starsArray[j];

levelsArray[j]=temp1;
scoresArray[j]=temp2;
starsArray[j]=temp3;
}
}
}
}



int main()
{
const int MAX=400;
int levelsArray[MAX];
int scoresArray[MAX];
int starsArray[MAX];

int numberOfLevels=buildArrays(levelsArray,scoresArray,starsArray);

printArrays( "Candy Crush UNSORTED Report", levelsArray, scoresArray, starsArray, numberOfLevels );
sortArrays( levelsArray, scoresArray, starsArray, numberOfLevels);
printArrays( "Candy Crush SORTED Report", levelsArray, scoresArray, starsArray, numberOfLevels );

system("pause");
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about loops