----it is not working on x code- #1 says 2 unused variables---- C++ -----------
ID: 3733525 • Letter: #
Question
----it is not working on x code- #1 says 2 unused variables---- C++ ----------- These 3 questions were answered incorrectly in the expert q qnd a- they had errors . please help if you are another expert out there. this should all be in c++
THIS IS THE CODE THEY GAVE ME BELOW
this code is incorrect
John Smith 10 15
Sarah Johnson 40 12
Mary Taylor 27 13
Jim Stewart 25 8
______________
#include
#include
#include
using namespace std;
//Function declaration
void readData(ifstream& dataIn,ofstream& dataOut);
int main() {
string fname,lname;
double workingHrs,hourlyRate;
//defines an input stream for the data file
ifstream dataIn;
//Defines an output stream for the data file
ofstream dataOut;
//Setting the precision
dataOut<
//Opening the input file
dataIn.open("employees.txt");
//checking whether the file name is valid or not
if(dataIn.fail())
{
cout<<"** File Not Found **";
return 1;
}
else
{
//creating and Opening the output file
dataOut.open("results.txt");
readData(dataIn,dataOut);
//Closing the intput file
dataIn.close();
//Closing the output file.
dataOut.close();
}
return 0;
}
//This function will read and process the data and write to outfile
void readData(ifstream& dataIn,ofstream& dataOut)
{
string fname,lname;
double workingHrs,hourlyRate;
while(dataIn>>fname>>lname>>workingHrs>>hourlyRate)
{
dataOut<
}
}
QUESTION 2
this code is incorrect
#include
#include
#include
using namespace std;
int getData(int *arr) {
cout << "Enter file name: ";
string filename;
cin >> filename;
ifstream in(filename.c_str());
int size = 0;
if(in.is_open()) {
int score;
string temp;
while(in >> temp >> temp >> score) {
arr[size++] = score;
}
} else {
cout << "can not open " << filename << " to read" << endl;
}
return size;
}
double avg(int *arr, int size) {
double total = 0;
for(int i = 0; i < size; ++i) {
total += arr[i];
}
return total/size;
}
int max(int *arr, int size) {
int temp = arr[0];
for(int i = 0; i < size; ++i) {
if(arr[i] > temp) {
temp = arr[i];
}
}
return temp;
}
int main() {
int arr[1000];
int size = getData(arr);
cout << "Average score is " << avg(arr, size) << endl;
cout << "Maximum score is " << max(arr, size) << endl;
return 0;
}
QUESTION 3
THIS IS THE CODE THEY GAVE ME
incorrect code below
#include
#include
#include
#include
#include
void countchar(char *filename)
{
ifstream fin(filename);
char ch;
std::string line;
int i,j,alpha=0,others=0,digi=0,lines=0;
cout<<"The orginal text is"< while(fin.get(ch))
{
i=ch;
cout<
if(i>64&&i<91||i>96&&i<123)
alpha++;
else
if(i>47&&i<58)
digi++;
else
if (i == ' ')
lines++;
else
others++;
}
fin.clear();
fin.seekg (0, ios::beg);
cout<
cout<
cout<
}
int main()
{
char *filename="nameofyourfile.txt";
char a;
countchar(filename);
return 0;
getch();
}
Explanation / Answer
Please find my answer for Q3.
Please repost others in separate post.
The Below given code should satisfy all your needs... let me know if you need any help on this!
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
char toUpper(char ch) { //convert a char to upper case
if (ch >= 'a' && ch <= 'z')
ch = ch - 32;
return ch;
}
char toLower(char ch) { //conert a char to lower case
if (ch >= 'A' && ch <= 'Z')
ch = ch + 32;
return ch;
}
int main() {
string inFile;
cout << "Enter input file name: ";
cin >> inFile; //read file name
ifstream in (inFile.c_str()); //open infile
ofstream o1 ("Output1"); //open out files
ofstream o2 ("Output2");
ofstream o3 ("Output3");
int charCount = 0;
int lineCount = 0;
int maxLineSize = 0;
int currLineSize = 0;
char ch;
while (in >> noskipws >> ch) { //loop for each charecters
o1 << toUpper(ch); //print lower to file 1
o2 << toLower(ch); //print upper to file 2
if (ch == ' '){ //count line when new line feed is encounters
lineCount ++;
if (currLineSize > maxLineSize)
maxLineSize = currLineSize;
currLineSize = 0;
}
else { //for other char count chars
charCount ++;
currLineSize ++;
}
}
o1.close();
o2.close();
o3 << "Total number of char: " << charCount << endl;
o3 << "Total number of lines: " << lineCount << endl;
o3 << "Max verse size: " << maxLineSize << endl;
cout << "Total number of char: " << charCount << endl;
cout << "Total number of lines: " << lineCount << endl;
cout << "Max verse size: " << maxLineSize << endl;
o3.close();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.