0

I want to build a simple program that can create data, update, and delete it. I'm not using a database so I want to use an array for storing it.

But, I have no clue to do that. So Here my code :

void addDataStudent() {

char name[30];
int age;

std::cout << "Name of student :";
std::cin >> name;
std::cout << "Age of student :"
std::cin >> age;

}

I want to make array something like this, so I can manipulate the data

Student[] = {
    [1][John, 15]
    [2][Doe, 13]
}

Anybody knows how to do that ? or give me a reference so I can start learning this code. Thanks

Abhishek Patil
  • 1,373
  • 3
  • 30
  • 62
Just L
  • 3
  • 1
  • There are [many good C++ books you can use to learn C++](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). C++ is the most complicated and difficult general purpose programming language in use today. Thinking up a random task and attempting to figure out how to it in C++ is not going to be very productive. The only way to learn C++ is to start from the first chapter in a book, and methodically proceed to learn C++, one step at a time, and practice coding the sample programs from the book. Good luck. – Sam Varshavchik Mar 03 '20 at 04:30
  • If you already know what the basic types are, and what an array is, and what functions are, then the next step might be to learn about structs. – neutrino_logic Mar 03 '20 at 04:57
  • That would be nice to take a look at `map` in C++, too. – Farbod Ahmadian Mar 03 '20 at 06:59

1 Answers1

0

If you want a very simple struct-based approach, that also introduces the STL, try this:

#include <iostream>
#include <array>
//read about structs; the typedef is here so we can refer to this type simply as 'Data'
typedef struct data{
    std::string name;
    int age;
} Data;
//note the & means pass-by-reference so we reduce copying expenses
void addStudent(Data& store) {
    std::cout << "Enter name: ";
    std::cin >> store.name;
    std::cout << "Enter age: ";
    std::cin >> store.age;
}

int main() {
    const int SIZE = 3;
    std::array<Data, SIZE> Students {};  // STL fixed-size array (see std::vector also)

    for (int i = 0; i < SIZE; ++i) {
        addStudent(Students[i]);         //this (a struct inside the array) gets passed
    }                                    //to the function by reference

    for (auto student : Students) {      //here we display contents of container
        std::cout << student.name << " " << student.age << std::endl;
    }

    return 0;
}

A lot of C++ textbooks won't introduce the STL containers till much later but I think it's better to know about them from the start. Keep it up!

neutrino_logic
  • 1,289
  • 1
  • 6
  • 11
  • but now, how to put id to each of data ? i want to update and delete them. but when user input data, the old data was replaced by the new one. – Just L Mar 03 '20 at 08:27
  • Well, what you want for that are the STL's std::vector and std::map, probably. However, you might also have to dynamically allocate memory for new structs (see new, std::unique_ptr, etc.). Map allows you to make key:value pairs so you could assign each student data struct a key. More simply, you could make SIZE much larger, and then iterator over the array until the first empty struct is found, and have the user add data to it. This would entail writing a new function, such as ```void addToArray(std::array& Students```, and moving code from main to it, add iterator, etc. – neutrino_logic Mar 03 '20 at 15:26