How do i make this less bulky and difficult?

I'm working on a project for my school, and I just started with C++.

This program is going to be used by my entire school to help keep attendance for each class.

If possible, I would like help making the comments more clear, and easy for someone with no experience to understand and change, as well as shorten this while still being able to add new students.

I don't know if its possible, but is there a way to (if all codes have been typed in) for it to just say that all students have shown up to class?
(sorry for asking for so much, I'm just interested in this and getting it to work as I envisioned)
There are no errors



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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>

using namespace std;

int
main ()
{


studentinput:

  int studentID;


  cout << std::endl << "Enter your student ID: ";
  cin >> studentID;

  cout << std::endl << "Your student ID is: " << studentID << std::endl;

// change and id and student name, look at this example
//  if (studentID == *student ID here*)
//  {
//      cout << "----*student name* is here---" << std::endl;
//      goto studentinput; <DON'T CHANGE THIS LINE>
//  }
// if you want to add a new student copy and paste everything in comments above



  if (studentID == 4401053)
    {
      cout << "----Student 1 is here----" << std::endl;
      goto studentinput;
    }
  if (studentID == 4401054)
    {
      cout << "----Student 2 is here----" << std::endl;
      goto studentinput;
    }
  if (studentID == 4401055)
    {
      cout << "----Student 3 is here----" << std::endl;
      goto studentinput;
    }
  if (studentID == 4401056)
    {
      cout << "----Student 4 is here----" << std::endl;
      goto studentinput;
    }
  if (studentID == 4401057)
    {
      cout << "----Student 5 is here----" << std::endl;
      goto studentinput;
    }
  if (studentID == 4401058)
    {
      cout << "----Student 6 is here----" << std::endl;
      goto studentinput;
    }
  if (studentID == 4401059)
    {
      cout << "----Student 7 is here----" << std::endl;
      goto studentinput;
    }
  if (studentID == 4401060)
    {
      cout << "----Student 8 is here----" << std::endl;
      goto studentinput;
    }
  if (studentID == 4401061)
    {
      cout << "----Student 9 is here----" << std::endl;
      goto studentinput;
    }
  if (studentID == 4401062)
    {
      cout << "----Student 10 is here----" << std::endl;
      goto studentinput;
    }
  if (studentID != 4401053, 4401054, 4401055, 4401056, 4401057, 4401058,
      4401059, 4401060, 4401061, 5501062)
      
      // put all student IDs above <in the section that says "studentID != ...">
      // seperated by commas
      // this section makes only the valid student IDs usable, and will tell them if they entered it wrong.
    
    {
      cout << "This is not a valid student ID, please try again." << std::
	endl;
      goto studentinput;
    }

  return 0;
}
Last edited on
If possible, I would like help making the comments more clear, and easy for someone with no experience to understand and change,

Your code, you need to make the comments.

as well as shorten this while still being able to add new students.


Put the student ID's in a file.
Last edited on
goto should be avoided if there is a way to do it without. do / while loop offers this for your usage, combined with maybe a break statement.

putting the ideas together, consider a program of:
read file of student IDs into a data structure. that might be something like ID and ISHERE (bool) poked into a c++ container, like vector or map. Default the ishere to false.
let students sign in. when a student signs in, check their id in the data, set ishere to true for that entry.
find a way to stop input and produce a report on demand.
when you generate a report --- could be who isnt there, who is, both, or just a count (11/15 showed up) or however you want to present your information to the user.

what you have done is attempt to hard code data into your program. This works, but its messy as you noted and you can't change it (next semester, different students, now what?) easily. There is a time and a place for 'disposable' programs that you run once and throw away, and you can use this kind of approach for those, as long as you know its for small programs that you will throw away. I do similar things pretty regularly to one-shot extract data from a file or fix the file etc.

how would i format the student ID file if names would also be required, and how would i get the code to read it.

I'm very new to this and i don't understand most of this, I'm trying to show both (who is and isn't there), as would be expected for a schools attendance.
Last edited on
data delimiter data delimiter .... format with one 'record' or student per line.
eg
john smith, 12345
jane doe, 98765

you write the code.
look for a simple example online of ifstream() and play with it. It works a lot like cin with some scaffolding around it to open it and a loop to read each line and process it.

Of course you are new to it. You wouldnt have done the above otherwise :P
Give it a try. Start small. Try to open a file and just print it to the screen to start with.
see if you can do that and then we can build up from there to put the data into a container instead of just writing it back out. Do small parts of a program and build up to what you want.
I am not sure how new you are to C++, but you should defintely not write everything inside the main program. As others have mentioned, what you are doing is hard-coding the thing. I encourage you to learn about making your own functions/methods instead of just using main. As jonnin stated, you should make an input file where the program can read student ID and names. This makes the program you more modular, since everything isn't hard-coded.

How do I make this less bulky and difficult?

This is a difficult question to answer, since there are so many possible answer, but I would have done this:
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <map>
#include <string>
#include <algorithm>

void getInput(int&);

/* If you do not know about classes yet, I heavily suggest learning about
 * them. They are extremely important and are used to organize data. You can
 * a few more other things with them, but as a beginner, you just need to know
 * that you can organize data with it. If you can't get around classes,
 * I would try to learn about structs. They are similar to class, except
 * a programmer can access everything inside the struct, which defeats the
 * purpose of something called inforamtion-hiding. */
class student {
  public:
    /* This is the "public" area of the class. This means any object that is
     * instantiated can access these variables or functions directly */
    student(); // A constructor. It's the same name as the class.
    bool isStudentID_inDatabase();
    void printStatusOfStudent();
    
    void queryStudent(const int& idToLookFor);
  private:
    /* This is the "private" area of the class. Instantiated objects are not
     * able to access these variables or functions directly, and therefore
     * have to rely on public functions to do so. There is also something
     * called a "friend" function if you want to research it. */
    std::map<int, std::string> student_id;
};

int main() {
  /* Below, we instantiated an object called "student_object". This object
     is of the the student class, which is defined above. */
  student student_object;

  // Boolean value search to create a looping menu, as seen below.
  bool search = true;

  // id_to_SearchFor should be pretty obvious
  int id_to_SearchFor;

  while(search) {
    getInput(id_to_SearchFor); // Get the input for ID
    if(id_to_SearchFor == -1) {
      /* You can define a specific value to exit the loop. For this program,
         I let -1 denote the exit code. */
      search = false;
    }
    else {
      /* If the exit code was not entered, that means the user of the program
       * wants to find student based on a student ID. The function 
       * queryStudent is defined below. */
      student_object.queryStudent(id_to_SearchFor);
    }
  }
  return 0;
}

student::student() {
  /* This is the constructor of the class. When you instantiate an object
   * that belongs to a specific class, the constructor of that class
   * is invoked and and whatever statements is inside that constructor
   * is executed. In this constructor, the students are set up with unique
   * student ID numbers and names.
   *
   * std::map allows us to set unique key fields in the square brackets.
   * This makes sense because students  may have the same name, but surely
   * they are  unable to have the same ID number. As jonnin was saying, it
   * is probably better to have these in a file where the program can read
   * from. */

  student_id[4401053] = "Student A";
  student_id[4401054] = "Student B";
  student_id[4401055] = "Student C";
  student_id[4401056] = "Student D";
  student_id[4401057] = "Student E";
  student_id[4401058] = "Student F";
  student_id[4401059] = "Student G";
  student_id[4401060] = "Student H";
  student_id[4401061] = "Student I";
  student_id[4401062] = "Student J";

}

void student::queryStudent(const int& x) {
  /* This finds the student in the std::map container */
  if(!student_id.count(x)) {
    /* If the student is not found... */
    std::cout << "The student with an id: " << x << " was not found.\n";
  }
  else {
    /* If the student is found... */
    std::cout << "Student ID: " << x << std::endl;
    std::cout << "Student name: " << student_id[x] << " is here\n";
  }
}

void getInput(int& x) {
  /* A function for getting input. It just looks pretty. That's all. */
  std::cout << " >>> ";
  std::cin >> x;
}
Last edited on
I started working with C++ less than a week ago, and truthfully, it's the first programming language I've learned.

jonnin and fiji885, thank you for the help, you two have been extremely helpful, I don't plan to mark this as solved because you guys have been so helpful, I'd like to see what others may have to say. I'll update the code as I go.
Topic archived. No new replies allowed.