Question

CODE ERROR Please help to fix the error. I don't know why I can not output...

CODE ERROR

Please help to fix the error. I don't know why I can not output to file. when I open the output file there is an error.

here is the input data:

Princeton University
NJ Princeton
41820    8014   0.0740   0.98   0.97
Harvard University
MA Cambridge
43838    19882   0.0580   0.97   0.97
Yale University
CT New Haven
45800    12109   0.0690   0.99   0.98
Columbia University
NY New York          
51008    23606   0.0690   0.99   0.96
Stanford University
CA Stanford
44757    18136   0.0570   0.98   0.96
University of Chicago
IL Chicago
48253    12539   0.0880   0.99   0.93
Massachusetts Institute of Technology
MA Cambridge
45016    11301   0.0820   0.98   0.93
Duke University
NC Durham
47488    15465   0.1240   0.97   0.94
University of Pennsylvania
PA Philadelphia
47668    21358   0.1220   0.98   0.96

here is my code:

//

// main.cpp

// Resubmit LAB 7

//

// Created by Quoc Le on 12/8/19.

// Copyright © 2019 Quoc Le. All rights reserved.

//

#include <iostream>

#include <fstream>

#include <iomanip>

#include <string>

#include <cstdlib>

#include <sstream>

#define max 1000

using namespace std;

void getData(string[],string[],string[],double[],double[],double[],double[],double[],int&);

void outputFile(ofstream&,string[],string[],double[],double[],double[],double[],int);

int main()

{

//Declare variables

string univerName[max], state[max], city[max];

double tuition[max], enrollment[max], avgFreRetention[max], percentFreshman[max],percentGraduated[max];

int number = 0;

  

ofstream outFile;

outFile.open("outfile.txt");

  

//Input from file

getData(univerName,state,city,tuition,enrollment,avgFreRetention,percentGraduated,percentFreshman,number);

  

//Output

outputFile(outFile,univerName,state,tuition,enrollment,percentFreshman,percentGraduated,number);

outFile.close();

return 0;

}//main

/////////////////////////////

void getData(string univerName[],string state[],string city[],double tuition[],double enrollment[],double avgFreRetention[],double percentGraduated[],double percentFreshman[], int& num)

{

/*

   Pre: univerName[] - array of university name

state[] - array of state

city[] - array of city

tuition[] - array of tuition

enrollment[] - array of enrollment

avgFreRetention[] - array of average freshman retention

percentGraduated[] - array of percentage of student graduated

percentFreshman[] - array of percentage retention for freshman

number - reference to number of universities in study

   Post: nothing

   Purpose: read in universities

   */

ifstream inFile;

inFile.open("universities.txt");

if(inFile.fail() == 1)

{

cout << "No Such File" << endl;

exit(100);

}

string line;

num = 0;

while (!inFile.eof())

{

getline(inFile,univerName[num]);

  

getline(inFile,state[num]);

inFile >> tuition[num] >> enrollment[num] >> avgFreRetention[num] >>percentFreshman[num]>>

   percentGraduated[num];

cout<<"\n University \t\t\t State \t Tuition \t Enrollment \t %Fresh Succeed \t %Graduate in six year" <<endl;

cout<<univerName[num]<<"\t\t\t\t"<<state[num]<<"\n"<<tuition[num]<<"\t"<<enrollment[num]<<"\t"<<percentFreshman[num]<<"%"<<"\t"<<percentGraduated[num]<<"%"<<endl;

  

inFile.ignore(2);

num++;

}

inFile.close();

}//getData

////////////////////////////////////////////////////////////////////////

void outputFile(ofstream& outFile,string univerName[],string state[],double tuition[],double enrollment[],double percentFreshman[],double percentGraduated[],int number)

{

outFile<<"\n University \t\t\t\t State \t Tuition \t Enrollment \t %Fresh Succeed \t %Graduate in six year" <<endl;

  

for(int i=0; i<number; i++)

outFile<< univerName[i]<<"\t\t\t\t"<<state[i]<<"\t"<<tuition<<"\t"<<enrollment<<"\t"<<percentFreshman<<"%"<<"\t"<<percentGraduated<<"%"<<endl;

}//output

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Modification In the program

1. In getData() method

During reading line by line “\n” character append to reading line.

i.e   getline(inFile,univerName[num]);

If we will print it on screen, next output will go to next line

Hence we have to remove it by

Suppose we have a string “str” then

str=resize(str.size()-1)

And here

univerName[num].resize(univerName[num].size()-1);

2. in outputFile Mthod

for(int i=0; i<number; i++)

outFile<< univerName[i]<<"\t\t\t\t"<<state[i]<<"\t"<<tuition<<"\t"<<enrollment<<"\t"<<percentFreshman<<"%"<<"\t"<<percentGraduated<<"%"<<endl;

}//output

We can see here "<<tuition<<"\t"<<enrolment

These are array, so index no is omitted, we have to provide the index no for all such array in the for loop. Other wise it will print only base address of the array

Next , instead of “\t” if we will use setw is better. It creates a specific length of a column

So data will visible in perfect column wise.

std::left : for all out statement in left alignment

C++ CODE

#include <iostream>
#include <fstream>
#include <iomanip> // for setw
#include <string>
#include <cstdlib>
#include <sstream>
#define max 1000
using namespace std;

void getData(string[],string[],string[],double[],double[],double[],double[],double[],int&);
void outputFile(ofstream&,string[],string[],double[],double[],double[],double[],int);

int main()
{

   //Declare variables
   string univerName[max], state[max], city[max];
   double tuition[max], enrollment[max], avgFreRetention[max], percentFreshman[max],percentGraduated[max];
   int number = 0;

   ofstream outFile;
   outFile.open("outfile.txt");


   //Input from file

   getData(univerName,state,city,tuition,enrollment,avgFreRetention,percentGraduated,percentFreshman,number);
   //Output

   outputFile(outFile,univerName,state,tuition,enrollment,percentFreshman,percentGraduated,number);
   outFile.close();

   return 0;

}//main end

/////////////////////////////Read from file

void getData(string univerName[],string state[],string city[],double tuition[],double enrollment[],double avgFreRetention[],
           double percentGraduated[],double percentFreshman[], int& num)
{

   /*
   Pre: univerName[] - array of university name
   state[] - array of state
   city[] - array of city
   tuition[] - array of tuition
   enrollment[] - array of enrollment
   avgFreRetention[] - array of average freshman retention
   percentGraduated[] - array of percentage of student graduated
   percentFreshman[] - array of percentage retention for freshman
   number - reference to number of universities in study
   Post: nothing
   Purpose: read in universities
*/

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

   if(inFile.fail() == 1)
   {
       cout << "No Such File" << endl;
       exit(100);
   }

   string line;
   num = 0;
   while (!inFile.eof()) // Until end of the file
   {

       getline(inFile,univerName[num]); // read line by line
       getline(inFile,state[num]);

       // while reading one line from file , a new line character appended to the string, so we need to remove it
       univerName[num].resize(univerName[num].size()-1); // remove the last character from string
       state[num].resize(state[num].size()-1); // remove the last character from string

       inFile >> tuition[num] >> enrollment[num] >> avgFreRetention[num] >>percentFreshman[num]>> percentGraduated[num];

       //Printing to console
       cout<<"\n University \t\t\t State \t Tuition \t Enrollment \t %Fresh Succeed \t %Graduate in six year" <<endl;
       cout<<univerName[num]<<"\t\t\t\t"<<state[num]<<"\n"<<tuition[num]<<"\t"<<enrollment[num]<<"\t"<<percentFreshman[num]<<"%"<<"\t"
           <<percentGraduated[num]<<"%"<<endl;

       inFile.ignore(2);
       num++;
   }
   inFile.close();

}//getData

////////////////////////////////// Write to file
void outputFile(ofstream& outFile,string univerName[],string state[],double tuition[],double enrollment[],double percentFreshman[],
               double percentGraduated[],int number)
{
   outFile<<std::left <<setw(50)<<"\nUniversity "<<setw(20) <<" State "<<setw(10) <<" Tuition "<<setw(20) <<" Enrollment "<<setw(20) <<
   " %Fresh Succeed "<<setw(20) <<" %Graduate in six year" <<endl; // Using setw for showing in columns and each column of fixed width

   for(int i=0; i<number; i++)
       // Using setw for showing in columns and each column of fixed width
       outFile<<std::left <<setw(50)<< univerName[i]<<std::left <<setw(20)<<state[i]<<setw(10)<<tuition[i]<<setw(20)<<enrollment[i]<<setw(5)
       <<percentFreshman[i]<<"%"<<setw(15)<<""<<setw(5)<<percentGraduated[i]<<setw(0)<<"%"<<endl;

}//output

CODE SCREENSHOT

SCREEN SHOT OF OUTPUT

1. CONSOLE

2. OUT PUT FILE CREATED

Add a comment
Know the answer?
Add Answer to:
CODE ERROR Please help to fix the error. I don't know why I can not output...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please!!! need help asap!!!! write a C++program to analyze a small subset of the data that...

    Please!!! need help asap!!!! write a C++program to analyze a small subset of the data that has been collected. See file universities.txt .Use precisely seven parallel arrays: one for name of university, one for state, one for city, one for yearly tuition, one for enrollment, one for average freshman retention, and one for the percent of students who graduate with in six years. Note that the percentage of student accepted is not stored.An output file is opened in main() and...

  • write a C++program to analyze a small subset of the data that has been collected. See...

    write a C++program to analyze a small subset of the data that has been collected. See file universities.txt .Use precisely seven parallel arrays: one for name of university, one for state, one for city, one for yearly tuition, one for enrollment, one for average freshman retention, and one for the percent of students who graduate with in six years. Note that the percentage of student accepted is not stored.An output file is opened in main() and remains open until the...

  • can someone help me fix this. The function that finds the minimum and maximum values of...

    can someone help me fix this. The function that finds the minimum and maximum values of the array and outputs the value and index doesnt find the maximum value of the array. #include <iostream> #include <fstream> #include<iomanip> using namespace std; void readArray(ifstream& inF, int arr[], int&ArrSize); void writeArray(ofstream & outF, int arr[], int & ArrSize); void MaxAndMin(ofstream & outF, int arr[], int & ArrSize, int &high, int &low); int main() {    int arr[100];    int i = 0;   ...

  • I NEED A PSEUDOCODE ALGORITHM FOR THIS CODE PLEASE C++: #include #include #include #include using...

    I NEED A PSEUDOCODE ALGORITHM FOR THIS CODE PLEASE C++: #include #include #include #include using namespace std; int NumOfEmployees(); int TotDaysAbsent(int); double AverageAbsent(int, int); int main() {         cout << endl << "Calculate the average number of days a company's employees are absent." << endl << endl;      int numOfEmployees = NumOfEmployees();         TotDaysAbsent(numOfEmployees);    return 0; } int NumOfEmployees() {    int numOfEmployees = 0;     cout << "Please enter the number of employees in the company: ";         cin >> numOfEmployees;     while(numOfEmployees <= 0)     {            ...

  • I NEED A PSEUDOCODE ALGORITHM FOR THIS CODE PLEASE C++: #include #include #include #include using namespace...

    I NEED A PSEUDOCODE ALGORITHM FOR THIS CODE PLEASE C++: #include #include #include #include using namespace std; int NumOfEmployees(); int TotDaysAbsent(int); double AverageAbsent(int, int); int main() {         cout << endl << "Calculate the average number of days a company's employees are absent." << endl << endl;      int numOfEmployees = NumOfEmployees();         TotDaysAbsent(numOfEmployees);    return 0; } int NumOfEmployees() {    int numOfEmployees = 0;     cout << "Please enter the number of employees in the company: ";         cin >> numOfEmployees;     while(numOfEmployees <= 0)     {...

  • Can some help me with my code I'm not sure why its not working. Thanks In...

    Can some help me with my code I'm not sure why its not working. Thanks In this exercise, you are to modify the Classify Numbers programming example in this chapter. As written, the program inputs the data from the standard input device (keyboard) and outputs the results on the standard output device (screen). The program can process only 20 numbers. Rewrite the program to incorporate the following requirements: a. Data to the program is input from a file of an...

  • Hello, I have written a code that I now need to make changes so that arrays...

    Hello, I have written a code that I now need to make changes so that arrays are transferred to the functions as pointer variable. Below is my code I already have. #include<iostream> #include<string> #include<iomanip> using namespace std; //functions prototypes void getData(string id[], int correct[], int NUM_STUDENT); void calculate(int correct[], int incorrect[], int score[], int NUM_STUDENT); double average(int score[], int NUM_STUDENT); void Display(string id[], int correct[], int incorrect[], int score[], double average, int NUM_STUDENT); int findHigh(string id[], int score[], int NUM_STUDENT);...

  • C++. I need to alter this code below to the current specifications. Console Progressing Complete!!! Specifications...

    C++. I need to alter this code below to the current specifications. Console Progressing Complete!!! Specifications Included is an input file. Included is a function to aide, if needed Use the functions, enum, and namespace defined in Assignment 6. You will need to use a loop to read each student’s data You will need to load every homework grade into an array Calculate the homework average The homework average can be calculated while the array is loaded Move the printing...

  • I am getting an error that the variable num was not declared in the scope on...

    I am getting an error that the variable num was not declared in the scope on the line of code that has; while(num != 5). Do you know how to fixe this error? #include <iostream> #include <iomanip> #include <string> #include<fstream> #include <cstring> using namespace std; //Declare the structure struct Games { string visit_team; int home_score; int visit_score; }; // Function prototypes int readData(Games *&stats); int menu(); void pickGame(Games *&stats, int size); void inputValid (Games *&stats, int size); void homeTotal(Games *&stats,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT