I need some help with one some of my homework. I am having trouble.
Write a C++ program:
1. Read the data in the file into two arrays: one is for IDs (the left column) and one
is for marks (the right column);
2. Ask the user to enter an ID, e.g., 0771716;
3. Search the mark corresponding to the entered ID and print the mark on the screen;
4. Ask the user if s/he would like to continue to search, if yes then go to Step 2, if
not then terminate the program
the numbers in the file are in two columns first it shows the 7 digit ID and then there is a space and then the mark is beside it.
This is what i have so far.
using namespace std;
ifstream in("StudentData.txt", ios::in);
while (count<32, ++count)
cin >> ID;
if (count >32, count++)
cout << "do you want to enter another ID ? If not please enter 999 : "
<< Arr << endl;
This assumes that we haven't been introduced to functions.
Otherwise, this is a bad program, with deeply nested control structures.
(It can be made cleaner by intelligent use of goto; but that is not the way forward.
That is not a substitute for factoring the code into functions, which is what we would do in the future.)
// 1. Read the data in the file into two arrays: one is for IDs (the left column)
// and one is for array_of_marks (the right column);
// 1 a. declare the two arrays
// vector: see https://cal-linux.com/tutorials/vectors.html
std::vector<std::string> array_of_ids ; // anything can be read as a string
std::vector<std::string> array_of_marks ; // anything can be read as a string
// 1 b. open the file for input
std::ifstream file( "StudentData.txt" ) ;
if( file.is_open() )
// 1c. read in the data
std::string id ;
std::string mark ;
while( file >> id >> mark ) // for each id, mark pair read in
char again ;
// 2. Ask the user to enter an ID, e.g., 0771716;
std::cout << "enter an id: " ;
std::cin >> id ;
// 3. Search the mark corresponding to the entered ID
std::size_t i = 0 ;
for( ; i < array_of_ids.size() ; ++i )
if( array_of_ids[i] == id ) // found it
// and print the mark on the screen;
std::cout << "marks: " << array_of_marks[i] << '\n' ;
break ; // quit the for loop: we are done with this id
// if we went through the entire loop till the very end,
// without finding the id
if( i == array_of_ids.size() ) std::cout << "no such id\n" ;
// 4. Ask the user if s/he would like to continue to search
std::cout << "continue (y/n)? " ;
std::cin >> again ;
while( again == 'y' || again == 'Y' ) ; // if yes then go to Step 2
else std::cout << "failed to open input file\n" ;