*HOW DO I CHANGE THIS FROM A VOID FUNCTION TO A NON-VOID WITH PARAMETERS?*
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void studentStats() {
ifstream inputFile;
inputFile.open("outFile.txt");
string studentData;
string studentID;
string ID, exam1, exam2, exam3;
string header;
cout << "Enter a Student ID: ";
cin >> studentID;
bool found =false;
while (inputFile) {
inputFile >> ID;
inputFile >> exam1;
inputFile >> exam2;
inputFile >> exam3;
if (ID.compare(studentID)==0) {
cout << ID << " " << exam1 << " " <<
exam2 << " " << exam3 << endl;
cout << "FOUND!" << endl;
found=true;
break;
}
}
if(!found) {
cout<<studentID << " NOT FOUND!" <<
endl;
}
inputFile.close();
}//ends studentStats() function
int main() {
studentStats();
}
Code -
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
//change the void to bool means a boolean variable
bool studentStats() {
ifstream inputFile;
inputFile.open("outFile.txt");
string studentData;
string studentID;
string ID, exam1, exam2, exam3;
string header;
cout << "Enter a Student ID: ";
cin >> studentID;
bool found =false;
while (inputFile) {
inputFile >> ID;
inputFile >> exam1;
inputFile >> exam2;
inputFile >> exam3;
if (ID.compare(studentID)==0) {
cout << ID << " " << exam1 << " " <<
exam2 << " " << exam3 << endl;
//cout << "FOUND!" << endl;
//found=true;
//whenever id is found return true as a boolean parameter
return true;
break;
}
}
if(!found) {
//cout<<studentID << " NOT FOUND!" <<
endl;
cout<<studentID<<endl;
//id is not found return false as a boolean parameter
return false;
}
inputFile.close();
}//ends studentStats() function
int main() {
//studentStats() function return true or false so accordingly set
the condition
//if it return true means value is found then print Found
if(studentStats()){
cout<<" Found! ";
}//else if not found print it will return false then print Not
found
else
cout<<" Not found";
}
Screenshots -
*HOW DO I CHANGE THIS FROM A VOID FUNCTION TO A NON-VOID WITH PARAMETERS?* #include<iostream> #include<fstream>...
*HOW TO SEARCH FOR THE STUDENT ID IN C++ USING TEXT FILES?* Text File Content: ID G1 G2 G3 200 20 20 20 30 30 30 30 400 40 40 40 25 25 25 25 78 78 78 78 666 66 6 66 777 77 100 77 420 77 88 99 1711 10 10 10 404 40 50 60 505 100 100 100 void studentStats() { ifstream inputFile; inputFile.open("outFile.txt"); string studentData; string studentID; string ID,...
#include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...
My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all the upper, lower case and digits in a .txt file. The requirement is to use classes to implement it. -----------------------------------------------------------------HEADER FILE - Text.h--------------------------------------------------------------------------------------------- /* Header file contains only the class declarations and method prototypes. Comple class definitions will be in the class file.*/ #ifndef TEXT_H #define...
#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:" <<...
4) What is the output if the input istom - Sawyer? #include <iostream> using namespace std; int main() { string playerName; cout << "Enter name"; cin >> playerName; cout << endl « playerName; return 0; } a. Tom - Sawyer b. Tom Sawyer c. Tom d. Sawyer 5) Which XXX generates "Adam is 30 years old." as the output? #include <iostream> using namespace std; int main() { string name = "Adam"; int age = 30; XXX return 0; } a....
#include <iostream> #include <vector> #include <fstream> #include <time.h> #include <chrono> #include <sstream> #include <algorithm> class Clock { private: std::chrono::high_resolution_clock::time_point start; public: void Reset() { start = std::chrono::high_resolution_clock::now(); } double CurrentTime() { auto end = std::chrono::high_resolution_clock::now(); double elapsed_us = std::chrono::duration std::micro>(end - start).count(); return elapsed_us; } }; class books{ private: std::string type; int ISBN; public: void setIsbn(int x) { ISBN = x; } void setType(std::string y) { type = y; } int putIsbn() { return ISBN; } std::string putType() { return...
employee.h ---------- #include <stdio.h> #include <iostream> #include <fstream> class Employee { private: int employeeNum; std::string name; std::string address; std::string phoneNum; double hrWage, hrWorked; public: Employee(int en, std::string n, std::string a, std::string pn, double hw, double hwo); std::string getName(); void setName(std::string n); int getENum(); std::string getAdd(); void setAdd(std::string a); std::string getPhone(); void setPhone(std::string p); double getWage(); void setWage(double w); double getHours(); void setHours(double h); double calcPay(double a, double b); static Employee read(std::ifstream& in); void write(std::ofstream& out); }; employee.cpp ---------- //employee.cpp #include...
*****Complete void example 4, 7, 8, 9, 10 #include <iostream> #include <fstream> #include <vector> #include <string> using namespace std; void rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r ") { str.erase(str.find_last_not_of(chars) + 1); } vector<string> *get_words() { vector<string> *words = new vector<string>(); fstream infile; string word; infile.open("words.txt"); getline(infile, word); while(infile) { rtrim(word); words->push_back(word); getline(infile, word); } return words; } void example_1(vector<string> *words) { // find words that contain the substring 'pet' and 'cat' // HINT: use find(str, p) method....
Modify PartTwo.cpp so that it is uses a vector, named vect, instead of an array. PartTwo.cpp Is posted here: // This program reads data from a file into an array. Then, it // asks the user for a number. It then compares the user number to // each element in the array, displays the array element if it is larger. // After looking at entire array, it displays the number of elements in the array larger // than the user...
The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter the...