Question

*HOW TO SEARCH FOR THE STUDENT ID IN C++ USING TEXT FILES?* Text File Content: ID...

*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, exam1, exam2, exam3;
   string header;

   system("cls");

   cout << "Enter a Student ID: ";
   cin >> studentID;

   inputFile >> header;
   getline(inputFile, header);
   cout << header << endl;

   inputFile >> ID;
   inputFile >> exam1;
   inputFile >> exam2;
   inputFile >> exam3;


   while (getline(inputFile, studentData)) {

   }

   if (studentID == ID) {
       cout << ID << " " << exam1 << " " << exam2 << " " << exam3 << endl;
       cout << "FOUND!" << endl;
   }
   else {
       cout << "NOT FOUND!" << endl;
   }

  
   inputFile.close();

   system("pause");
}//ends studentStats() function

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

Please find the updated code below::::

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

void studentStats() {
   ifstream inputFile;
   string studentData;
   string studentID;
   string ID, exam1, exam2, exam3;
   string header;
   system("cls");
   getline(inputFile, header,'\n');
   cout << "Enter a Student ID: ";
   cin>>studentID;
   string line;//for read line
   inputFile.open ("outFile.txt"); //name of file here. plz mention Complete path if file is not at root
   if (inputFile.is_open()) //if file opened
   {
       while( getline(inputFile, line,'\n') ) { //get row from text file
           stringstream ss(line); //stream to other variable
           ss >> ID;
           ss >> exam1;
           ss >> exam2;
           ss >> exam3;

           if (studentID == ID) {
               cout << "Student FOUND!" << endl;
               cout << ID << " " << exam1 << " " << exam2 << " " << exam3 << endl;
               return ;
           }
       }
       inputFile.close(); //close file
       cout<<"File scan done........"<<endl;
   }
   else //if file not found show the below message
   {
       cout << "Sorry, we could not find the file." << endl;
   }
   cout << "Student NOT FOUND!" << endl;
   inputFile.close();
   system("pause");
}//ends studentStats() function


int main(){
   studentStats();
   return 0;
}

text file

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

output:

Add a comment
Know the answer?
Add Answer to:
*HOW TO SEARCH FOR THE STUDENT ID IN C++ USING TEXT FILES?* Text File Content: ID...
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
  • *HOW DO I CHANGE THIS FROM A VOID FUNCTION TO A NON-VOID WITH PARAMETERS?* #include<iostream> #include<fstream>...

    *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 << " " <<...

  • [C++] Using Files—Total and Average Rainfall Write a program that reads in from a file a...

    [C++] Using Files—Total and Average Rainfall Write a program that reads in from a file a starting month name, an ending month name, and then the monthly rainfall for each month during that period. As it does this, it should sum the rainfall amounts and then report the total rainfall and average rainfall for the period. For example, the output might look like this: During the months of March–June the total rainfall was 7.32 inches and the average monthly rainfall...

  • 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 th...

    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...

  • I wrote code in C++ that takes a text file containing information on different football players...

    I wrote code in C++ that takes a text file containing information on different football players and their combine results, stores those players as a vector of objects of the class, and prints out those objects. Finally, I wrote a function that calculates the average weight of the players. MAIN.CPP: #include "Combine.h" #include using namespace std; // main is a global function // main is a global function int main() { // This is a line comment //cout << "Hello,...

  • Write a C++ program which performs +, -, *, / and $ on hexadecimal operands. The...

    Write a C++ program which performs +, -, *, / and $ on hexadecimal operands. The maximum length of any operand or a solution is 40 digits. The input will be in the following format: Op1 op op2 = There is no space between operands and operator. Note 5/2 = quotient 2, remainder 1 2$3 = 8 The output should be of the form 2*3=6. Read date from a file. TEST DATA (input.txt): AAAA+BBF= BFD+2DE= 100*AA= 100$5= 100/F= 10000000000000-1= AAAAABBBBBCCCCCDDDDDEEEEEFFFFF-ABCDEF0123456789ABCDEF=...

  • 4) What is the output if the input istom - Sawyer? #include <iostream> using namespace std;...

    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....

  • I have a C++ code that lets me enter, display and delete a student record. I...

    I have a C++ code that lets me enter, display and delete a student record. I need to implement a function that prints the average grade score of the students I input. Below is my code and a picture of how my code looks right now. #include<iostream> #include<stdlib.h> using namespace std; //Node Declaration struct node {    string name;    string id;    int score;    node *next;   }; //List class class list {        private:        //head...

  • C++ Assignment on Search and Sort for Chapter 8 Problem: Modify the student grade problem to...

    C++ Assignment on Search and Sort for Chapter 8 Problem: Modify the student grade problem to include the following: •   Write a Search function to search by student score •   Write a Search function to search by student last name •   Write a Sort function to sort the list by student score •   Write a Sort function to sort the list by student last name •   Write a menu function that lets user to choose any action he/she want to...

  • Rework this project to include a class. As explained in class, your project should have its...

    Rework this project to include a class. As explained in class, your project should have its functionalities moved to a class, and then create each course as an object to a class that inherits all the different functionalities of the class. You createclass function should be used as a constructor that takes in the name of the file containing the student list. (This way different objects are created with different class list files.) Here is the code I need you...

  • Assignment Predator / Prey Objectives Reading from and writing to text files Implementing mathematical formulas in...

    Assignment Predator / Prey Objectives Reading from and writing to text files Implementing mathematical formulas in C++ Implementing classes Using vectors Using command line arguments Modifying previously written code Tasks For this project, you will implement a simulation for predicting the future populations for a group of animals that we’ll call prey and their predators. Given the rate at which prey births exceed natural deaths, the rate of predation, the rate at which predator deaths exceeds births without a food...

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
ADVERTISEMENT