I can't run this code, it gives an error. Can you fix the error and rewrite the code for me and can you also correct the average part? #include #include #include #include #include #include #define SIZE 50 using namespace std; int main() { int sum1 = 0, counter = 0; int num[SIZE]; string word; ifstream infileFirst; ifstream infileSecond; ofstream outfile; infileFirst.open("group1.txt"); infileSecond.open("group2.txt"); outfile.open("groupBar.out"); outfile << fixed << showpoint << setprecision(2); outfile << "\t\t\t\t Bar Graph " << endl; outfile << "Course \t Course"; int i; for (i = 0; i <= 100; i = i + 10) { outfile << setfill(' ') << right << setw(5) << i; } outfile << endl; outfile << "ID \t\t Average "; outfile << setfill('.') << "|" << right << setw(5) << "|" << right << setw(5) << "|" << right << setw(5) << "|" << right << setw(5) << "|" << right << setw(5) << "|" << right << setw(5) << "|" << right << setw(5) << "|" << right << setw(5) << "|" << right << setw(5) << "|" << right << setw(5) << "|" << right << endl; infileFirst >> word; int number; double avg; string txt1[50]; string txt2[50]; string star =""; double avg1[50]; double avg2[50]; int sum = 0, k = 0, numberCounter = 0; while (!infileFirst.eof()) { infileFirst >> number; if (number == -999) { avg = double(sum) / numberCounter; avg1[k] = avg; for (int i = 0; i < avg/2; i++) { star += "*"; } txt1[k++] = word + " \t" + to_string(avg)+ "\t" + star + "\n"; infileFirst >> word; star = ""; sum = 0, numberCounter = 0; continue; } numberCounter++; sum += number; } sum = 0, numberCounter = 0, k=0; infileSecond >> word; outfile << fixed << showpoint << setprecision(2); while (!infileSecond.eof()) { infileSecond >> number; if (number == -999) { avg = (double(sum) / numberCounter); avg2[k] = avg; for (int i = 0; i < avg / 2; i++) { star += "#"; } txt2[k++] =" \t\t" + to_string(avg) + "\t" + star + "\n"; infileSecond >> word; star = ""; sum = 0, numberCounter = 0; continue; } numberCounter++; sum += number; } for ( i = 0; i < 5; i++) { outfile << txt1[i] << txt2[i]; } outfile << endl; outfile << "Group 1 -- ****" << endl; outfile << "Group 2 -- ****" << endl; double result1 = 0.0; for (i = 0; i < 10; i++) { result1 = (avg1[i] = +avg)/ i; } outfile << "Avg for group 1: " << result1 << endl;; double result2 = 0.0; for (i = 0; i < 10; i++) { result2 = (avg1[i] = +avg); } outfile << "Avg for group 1: " << result2; system("pause"); return 0; }
I can't run this code, it gives an error. Can you fix the error and rewrite the code for me and can you also correct the average part?
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<iomanip>
#include<string>
#include<cctype>
#define SIZE 50
using namespace std;
int main()
{
int sum1 = 0, counter = 0;
int num[SIZE];
string word;
ifstream infileFirst;
ifstream infileSecond;
ofstream outfile;
infileFirst.open("group1.txt");
infileSecond.open("group2.txt");
outfile.open("groupBar.out");
outfile << fixed << showpoint << setprecision(2);
outfile << "\t\t\t\t Bar Graph " << endl;
outfile << "Course \t Course";
int i;
for (i = 0; i <= 100; i = i + 10)
{
outfile << setfill(' ') << right << setw(5) << i;
}
outfile << endl;
outfile << "ID \t\t Average ";
outfile << setfill('.') << "|" << right << setw(5) << "|" << right << setw(5) << "|" << right << setw(5) << "|" << right << setw(5) << "|" << right << setw(5) << "|" << right << setw(5) << "|" << right << setw(5) << "|" << right << setw(5) << "|" << right << setw(5) << "|" << right << setw(5) << "|" << right << endl;
infileFirst >> word;
int number;
double avg;
string txt1[50];
string txt2[50];
string star ="";
double avg1[50];
double avg2[50];
int sum = 0, k = 0, numberCounter = 0;
while (!infileFirst.eof())
{
infileFirst >> number;
if (number == -999)
{
avg = double(sum) / numberCounter;
avg1[k] = avg;
for (int i = 0; i < avg/2; i++)
{
star += "*";
}
txt1[k++] = word + " \t" + to_string(avg)+ "\t" + star + "\n";
infileFirst >> word;
star = "";
sum = 0, numberCounter = 0;
continue;
}
numberCounter++;
sum += number;
}
sum = 0, numberCounter = 0, k=0;
infileSecond >> word;
outfile << fixed << showpoint << setprecision(2);
while (!infileSecond.eof())
{
infileSecond >> number;
if (number == -999)
{
avg = (double(sum) / numberCounter);
avg2[k] = avg;
for (int i = 0; i < avg / 2; i++)
{
star += "#";
}
txt2[k++] =" \t\t" + to_string(avg) + "\t" + star + "\n";
infileSecond >> word;
star = "";
sum = 0, numberCounter = 0;
continue;
}
numberCounter++;
sum += number;
}
for ( i = 0; i < 5; i++)
{
outfile << txt1[i] << txt2[i];
}
outfile << endl;
outfile << "Group 1 -- ****" << endl;
outfile << "Group 2 -- ****" << endl;
double result1 = 0.0;
for (i = 0; i < 10; i++)
{
result1 = (avg1[i] = +avg)/ i;
}
outfile << "Avg for group 1: " << result1 << endl;;
double result2 = 0.0;
for (i = 0; i < 10; i++)
{
result2 = (avg1[i] = +avg);
}
outfile << "Avg for group 1: " << result2;
system("pause");
return 0;
}
Step by step
Solved in 2 steps