0

I am trying to program a school bulletin (I'm sorry if that's not the right word, what I meant with "bulletin" is the thing were are the grades of each student. English isn't my 1° language) and I want to ask the user the name of the student and then create a

int student_name;

so I don't need to create a 1000 of

int student_1
int student_2

and then just use cin <<. So, how can I do that? I hope my question was easy to understand. Thanks!

4
  • 1
    How about std::unordered_map<std::string, int> where the key is the student name and the value is a note. en.cppreference.com/w/cpp/container/unordered_map Commented Nov 22, 2021 at 13:44
  • 2
    You can't create a variable identifier based on input, and you never need to. This is what std::vector and arrays and tables like std::map are for. Commented Nov 22, 2021 at 13:44
  • What to do next depends on what the name is for. Do you intend to locate students by name and store or find out some other information about them, or do you want to collect all your students in one place and then look up their names (and other data)? Commented Nov 22, 2021 at 13:55
  • Note that a std::map or std::unordered_map that use the student’s name as a key is going to be problematic if you ever have two students with the same name :) Commented Nov 22, 2021 at 22:48

2 Answers 2

1
#include <iostream>
#include <map>
#include <string>
void createStudent(std::map<string, int>& input)
{
    std::string name;
    int grade;
    for(int i=0; i<1000; ++i)
    {
        std::cin>>name>>grade;
        input.insert(std::pair<string, int>(name, grade));
    }
    return;
}
void showContentMap(std::map<string, int>& input)
{
    for(int i=0; i<input.size(); ++i)
    {
        std::cout<<input[i].first<<" : "<<input[i].second<<std::endl;
    }
    return;
}
int main()
{
    std::map<string, int> students;
    createStudent(students);
    showContentMap(students);
    return 0;
}

Explanation:

  1. An execution of the command-function createStudent() assigns into the parameter students their identifiers such as name and grade. It is known that the quantity of students is 1000.
  2. The command-function showContentMap() demonstrates the content of the parameter students.
  3. The main properties of a student are name and grade. These properties will be assigned into the parameter map<string, int> students.
Sign up to request clarification or add additional context in comments.

3 Comments

Some things to improve, apart from no written explanation: using namespace std;, global variable, hardcoded std::cin, map passed by copy, ordered container even though no order required.
@pptaszni, I have corrected.
@João Guilherme, if my answer helped to you, mark this question as solved.
0

Since you want to create a system that allows you to add and remove student names, it is best to use something like std::vector<std::string> studentNames;. Every time that you want to append a new student name, you can use studentNames.pushBack("John Doe");.

Remember to #include <vector> it at the top of your code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.