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 unspecified
length; that is,
the program does not know in advance how many numbers are in the
file.
b. Save the output of the program in a file.
c. Modify the function getNumber so that it reads a number from
the
input file (opened in the function main), outputs the number to
the
output file (opened in the function main), and sends the number
read
to the function main. Print only 10 numbers per line.
d. Have the program find the sum and average of the numbers.
e. Modify the function printResult so that it outputs the final
results to
the output file (opened in the function main). Other than
outputting the
appropriate counts, this new definition of the function
printResult
should also output the sum and average of the numbers.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
void initialize(int& zeroCount, int& oddCount, int&
evenCount);
void getNumber(ifstream& in, ofstream& out, int&
num);
void classifyNumber(int num, int& zeroCount, int& oddCount,
int evenCount);
void printResults(ofstream& out, int zeroCount, int oddCount,
int evenCount, int sum);
int main()
{
int counter;
int number;
int zeros;
int odds;
int evens;
int sum = 0;
char fileName[15];
ifstream inFile;
ofstream outFile;
cout << "\n\n\tProgram that classifies numbers.";
cout << "\n\n\tEnter the input file name: ";
cin >> fileName;
inFile.open(fileName);
if (!inFile.is_open())
{
cout << "\n\n\tError in opening input file";
exit(0);
}
cout << "\tEnter the output file name:";
cin >> fileName;
outFile.open(fileName);
if (!outFile.is_open())
{
cout << "\n\n\tError in opening input file";
exit(0);
}
initialize(zeros, odds, evens);
counter = 0;
while (true)
{
getNumber(inFile, outFile, number);
if (!inFile)
break;
sum += number;
counter++;
if ((counter % 10) == 0)
outFile << endl;
classifyNumber(number, zeros, odds, evens);
}
printResults(outFile, zeros, odds, evens, sum);
return 0;
}
void initialize(int& zeroCount, int& oddCount, int&
evenCount)
{
zeroCount = 0;
oddCount = 0;
evenCount = 0;
}
void getNumber(ifstream& in, ofstream& out, int&
num)
{
if ((in >> num) != NULL)
out << num << "\t";
}
void classifyNumber(int num, int& zeroCount, int&
oddCount, int& evenCount)
{
switch (num % 2)
{
case 0:
evenCount++;
if (num == 0);
zeroCount++;
break;
case 1:
case -1:
oddCount++;
}
}
void printResults(ofstream& out, int zeroCount, int oddCount, int evenCount, int sum)
{
double avg = (sum * 1.0) / (oddCount + evenCount);
out << "\nThere are " << evenCount << "evens"
<< "which includes" << zeroCount <<
"zeros.";
out << "\nThe number of odd numbers is : " <<
oddCount;
out << "\nThe sum of the above numbers is : " <<
sum;
out << "\nThe average of the above numbers is " <<
avg;
}
// I have corrected the errors. Please use the below code.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
void initialize(int& zeroCount, int& oddCount, int&
evenCount);
void getNumber(ifstream& in, ofstream& out, int&
num);
void classifyNumber(int num, int& zeroCount, int& oddCount,
int& evenCount);
void printResults(ofstream& out, int zeroCount, int oddCount,
int evenCount, int sum);
int main()
{
int counter;
int number;
int zeros;
int odds;
int evens;
int sum = 0;
char fileName[15];
ifstream inFile;
ofstream outFile;
cout << "\n\n\tProgram that classifies numbers.";
cout << "\n\n\tEnter the input file name: ";
cin >> fileName;
inFile.open(fileName);
if (!inFile.is_open())
{
cout << "\n\n\tError in opening input file";
exit(0);
}
cout << "\tEnter the output file name:";
cin >> fileName;
outFile.open(fileName);
if (!outFile.is_open())
{
cout << "\n\n\tError in opening input file";
exit(0);
}
initialize(zeros, odds, evens);
counter = 0;
while (true)
{
getNumber(inFile, outFile, number);
if (!inFile)
break;
sum += number;
counter++;
if ((counter % 10) == 0)
outFile << endl;
classifyNumber(number, zeros, odds, evens);
}
printResults(outFile, zeros, odds, evens, sum);
return 0;
}
void initialize(int& zeroCount, int& oddCount, int&
evenCount)
{
zeroCount = 0;
oddCount = 0;
evenCount = 0;
}
void getNumber(ifstream& in, ofstream& out, int&
num)
{
if ((in >> num) != NULL)
out << num << "\t";
}
void classifyNumber(int num, int& zeroCount, int&
oddCount, int& evenCount)
{
switch (num % 2)
{
case 0:
evenCount++;
if (num == 0)
zeroCount++;
break;
case 1:
oddCount++;
}
}
void printResults(ofstream& out, int zeroCount, int oddCount, int evenCount, int sum)
{
double avg = (sum * 1.0) / (oddCount + evenCount);
out << "\nThere are " << evenCount << " evens"
<< " which includes " << zeroCount << "
zeros.";
out << "\nThe number of odd numbers is : " <<
oddCount;
out << "\nThe sum of the above numbers is : " <<
sum;
out << "\nThe average of the above numbers is " <<
avg;
}
Can some help me with my code I'm not sure why its not working. Thanks In...
Code in C++: Please help me fix the error in function: void write_account(); //function to write record in binary file (NOTE: I able to store data to a txt file but however the data get error when I try to add on data the second time) void display_all(); //function to display all account details (NOTE: This function is to display all the info that save account.txt which is ID, Name, and Type) void modify_account(int); //function to modify record of file...
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 have 2 issues with the C++ code below. The biggest concern is that the wrong file name input does not give out the "file cannot be opened!!" message that it is coded to do. What am I missing for this to happen correctly? The second is that the range outputs are right except the last one is bigger than the rest so it will not output 175-200, it outputs 175-199. What can be done about it? #include <iostream> #include...
//This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...
//This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...
C++: Need help debugging my code I am writing this and using some other examples as reference code while rewriting it with my own understanding of the material but am having trouble making it finally compile. Below is a picture of the error messages. //main.cpp //Semester Project //Created by J---on 5/6/2019 #include <iostream> #include <fstream> #include <string> #include <sstream> #include <bits/stdc++.h> using namespace std; void instructions(); //displays program details and instructions void openFile(); void takeInput(int*); void switchBoard(int*); struct price {...
Write the definitions of the member functions of the class integerManipulation not given in Example 10-11. Also, add the following operations to this class: Split the number into blocks of n-digit numbers starting from right to left and find the sum of these n-digit numbers. (Note that the last block may not have ndigits. If needed add additional instance variables.) Determine the number of zeroes. Determine the number of even digits. Determine the number of odd digits Also, write a...
#include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...
In c++ please How do I get my printVector function to actually print the vector out? I was able to fill the vector with the text file of names, but I can't get it to print it out. Please help #include <iostream> #include <string> #include <fstream> #include <algorithm> #include <vector> using namespace std; void openifile(string filename, ifstream &ifile){ ifile.open(filename.c_str(), ios::in); if (!ifile){ cerr<<"Error opening input file " << filename << "... Exiting Program."<<endl; exit(1); } } void...
Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found; string document[1000][6]; ifstream infile; char s[1000];...