Control structures, arrays, and File Input

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.

#include <iostream>
#include <fstream>
#include <vector>
#include<string>
using namespace std;

int main()
{
ifstream in("StudentData.txt", ios::in);

std::string Arr[32];
std::string ID;
double count=0;
while (count<32, ++count)
{
cin >> ID;
if (count >32, count++)
break;
else
cout << "do you want to enter another ID ? If not please enter 999 : "
<< Arr[32] << endl;
++count;
}

in.close();
cin.get();
system("pause");
return 0;
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

int main()
{
    // 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
        {
            array_of_ids.push_back(id) ;
            array_of_marks.push_back(mark) ;
        }

        char again ;
        do
        {
            // 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" ;
}
Last edited on
Topic archived. No new replies allowed.