简体   繁体   中英

Reading program from input file

Im suppose to write a program that will be read from a file. I have the program written but i have to modify it so it can be read from the file.

My question is how do modify my program so i can be read from an input file rather than the standard input?

Here are the directions* * ** * ** * ** * ** * ** * ****

The input for this version of the program will be read from a file named candycrush.txt. Changes to buildArrays

Add a declaration for an input file stream to the variable declarations within buildArrays. For example:

ifstream inFile;

Before getting the first integer from the user, open the input file and make sure that it opened correctly

inFile.open( "candycrush.txt" );

  if ( inFile.fail() )
     {
     cout << "The candycrush.txt input file did not open";
     exit(-1);
     }

As soon as I make the modifications i get errors, any advise would be helpful.

#include <iostream> // access to cin, cout
#include <cstring>
#include <cstdlib>
#include<cmath>

using namespace std;

int buildArrays(int A[],int B[],int C[])
{
   int a,i=0;
   cout<<"Enter the level: ";
   cin>>a;
   while(a!=-999)
   {
       A[i]=a;
       cout<<"Enter the score: ";
       cin>>a;
       B[i]=a;
       cout<<"Enter the stars: ";
       cin>>a;
       C[i]=a;
       cout<<"Enter the level: "; 
       cin>>a;
       i++;
    }
    return i;
}

 void printArrays( string reportTitle, int levelsArray[], int scoresArray[], int starsArray[], int numberOfLevels )  
{
    cout<<reportTitle<<endl;
    cout<<"Levels\tScores\tStars"<<endl;
    for(int i=0;i<numberOfLevels;i++)
   {
       cout<<levelsArray[i]<<"\t"<<scoresArray[i]<<"\t";
       for(int j=0;j<starsArray[i];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");

}

what error did you get ?? did you include fstream in your code

#include <fstream>

For using file i/o you have have to include fstream in your code properly.

follow this link for more help

http://www.cplusplus.com/doc/tutorial/files/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM