There is a minor error in my program that would not allow me to run. I want to c
ID: 3875231 • Letter: T
Question
There is a minor error in my program that would not allow me to run. I want to construct a program that asks a user to enter an id then it will search for the name in the text file that matches with the id. Here are my codes. Please do this in C++ and provide comments, Thanks.
CLASS.TXT:
321 Jimmy
232 Holly
551 Billy
626 Tony
975 David
STUDENT.H
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class student
{
public:
student();
student(string file);
int print_id (int idSearch[]);
private:
string filename;
int idSearch[100];
string nameSearch[100];
};
student::student()
{
filename = " ";
for (int i = 0; i < 5; ++i)
{
idSearch[i] = 0;
}
for (int i = 0; i < 5; ++i)
{
nameSearch[i] = " ";
}
}
student::student(string file)
{
filename = file;
fstream inputfile;
inputfile.open(filename);
while (!inputfile.eof())
{
for (int i = 0; i < 5; ++i)
{
inputfile >> idSearch[i] >> nameSearch[i];
}
}
}
int student:: print_id(int idSearch[])
{
int id;
cout << "Enter ID: ";
cin >> id;
for (int j = 0; j < 5; ++j)
{
if (id == idSearch[j])
{
cout <<"Name: " << nameSearch[j] << endl;
}
else
{
cout << "Invalid ID" << endl;
}
}
return 0;
}
STUDENT.CPP
#include <iostream>
#include <string>
#include "student.h"
using namespace std;
int main()
{
student mystudent("class.txt");
mystudent.print_id();
system("pause");
return 0;
}
Explanation / Answer
This is C++ code:
#include "STUDENT.h"
namespace CheggQSConsole
{
void STUDENT::Main(std::vector<std::wstring> &args)
{
try
{
std::wcout << L"Please Enter Student ID" << std::endl;
std::wstring stdId;
std::getline(std::wcin, stdId);
SearchMethod(stdId);
}
catch (const std::exception &Ex)
{
throw Ex;
}
}
void STUDENT::SearchMethod(const std::wstring &stdId)
{
//Input text path
std::wstring pathToFile = LR"(E:chegg2071CLASS.txt)";
std::vector<std::vector<std::wstring>> groups;
std::vector<std::wstring> current;
for (auto line : File::ReadAllLines(pathToFile))
{
if (line->Contains(stdId) && current.empty())
{
current = std::vector<std::wstring>();
groups.push_back(current);
current.clear();
std::wcout << L"Details are: " << line << std::endl;
std::wstring tempVar;
std::getline(std::wcin, tempVar);
}
}
std::wcout << L"No results found! Please check input" << std::endl;
std::wstring tempVar2;
std::getline(std::wcin, tempVar2);
}
}
Student.h
#include <string>
#include <vector>
#include <iostream>
#include <stdexcept>
namespace CheggQSConsole
{
class STUDENT
{
static void Main(std::vector<std::wstring> &args);
/// <summary>
/// This method will take ID as input,
/// method will search in line by line in txt
/// </summary>
/// <param name="stdId"></param>
private:
static void SearchMethod(const std::wstring &stdId);
};
}
............................................
This is C# code(You may convert in CPP)
........................................
This ic C# code
STUDENT.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CheggQSConsole
{
public class STUDENT
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Please Enter Student ID");
string stdId = Console.ReadLine();
SearchMethod(stdId);
}
catch (Exception Ex)
{
throw Ex;
}
}
/// <summary>
/// This method will take ID as input,
/// method will search in line by line in txt
/// </summary>
/// <param name="stdId"></param>
private static void SearchMethod(string stdId)
{
//Input text path
string pathToFile = @"E:chegg2071CLASS.txt";
List<List<string>> groups = new List<List<string>>();
List<string> current = null;
foreach (var line in File.ReadAllLines(pathToFile))
{
if (line.Contains(stdId) && current == null)
{
current = new List<string>();
groups.Add(current);
current = null;
Console.WriteLine("Details are: " + line);
Console.ReadLine();
}
}
Console.WriteLine("No results found! Please check input");
Console.ReadLine();
}
}
}
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If you are comfortable in VB,
so here is code for VB
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Namespace CheggQSConsole
Public Class STUDENT
Private Shared Sub Main(ByVal args As String())
Try
Console.WriteLine("Please Enter Student ID")
Dim stdId As String = Console.ReadLine()
SearchMethod(stdId)
Catch Ex As Exception
Throw Ex
End Try
End Sub
Private Shared Sub SearchMethod(ByVal stdId As String)
Dim pathToFile As String = "E:chegg2071CLASS.txt"
Dim groups As List(Of List(Of String)) = New List(Of List(Of String))()
Dim current As List(Of String) = Nothing
For Each line In File.ReadAllLines(pathToFile)
If line.Contains(stdId) AndAlso current Is Nothing Then
current = New List(Of String)()
groups.Add(current)
current = Nothing
Console.WriteLine("Details are:" & vbLf & line)
Console.ReadLine()
End If
Next
Console.WriteLine("No results found! Please check input")
Console.ReadLine()
End Sub
End Class
End Namespace
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.