2

Here my code:

#include<iostream>
#include<string.h>
#define SIZE 100

struct person{
  std::string name;
  int age;
};

void entry(struct person *info){
  std::getline(std::cin, info->name);
  std::cin >> info->age;
}

int main(int argc, char const *argv[]) {
  struct person roster[SIZE];
  int n; // number of people in the roster:
  std::cin >> n;
  for (int i = 0; i < n; i++){
    entry(&roster[i]);
  }
  return 0;
}

I am learning how to use "struct" in c++ and in this program, I created a roster includes name and age, but the program crashed whenever I try to read the string "name". Can you help me? Thank you, I stuck at this for days.

P.s: I am learning c++ with a C-book, so my code might contain C-ism.

  • 1
    "I am learning c++ with a C-book" -- please don't. They are very different languages – Ajay Brahmakshatriya Jun 10 '21 at 02:57
  • Can you also show the input that you provide to your program? It might help understand where exactly, your program crashes. – Ajay Brahmakshatriya Jun 10 '21 at 02:58
  • You say <>. What is your input? For example, is it something like: "2frank10johnni12"? There is nothing obviously wrong with your program, so you need to provide the text you type into the prompt. – stanm Jun 10 '21 at 19:53

2 Answers2

0

Please don't mind the prints to user I've introduced here. The execution flow is just so much more clear with them.

So, you should look toward the following:

#include<iostream>
#include<string.h>
#define SIZE 100

struct person{
    std::string name;
    int age;
};

void entry(struct person *info){
    std::cout << "Enter name: ";
    std::getline(std::cin, info->name);
    std::cout << "\nAge: ";
    std::cin >> info->age;
    std::cin.ignore();
    std::cout << std::endl;
}

int main(int argc, char const *argv[]) {
    person roster[SIZE];
    int n; // number of people in the roster:
    std::cout << "Number of people in roster: ";
    std::cin >> n;
    std::cin.ignore();
    for (int i = 0; i < n; i++){
        entry(&roster[i]);
    }
}

Example:

Number of people in roster: 3
Enter name: Foo
Age: 22

Enter name: Bar
Age: 33

Enter name: Baz
Age: 44

The main issue you were having is of a complex nature, the roots of which are in the getline() behavior. I would suggest you to check this thread for more elaboration.

For what is std::cin.ignore() check the documentation. And on when and how to use it, check this thread.

rawrex
  • 4,044
  • 2
  • 8
  • 24
0

After declaring a struct, to declare the variable, you simply do the same way when declaring an int or double:

(struct) <struct_name> <variable_name> = <value>;

And that's where C and C++ is different. In C++, the struct keyword is optional before in declaration of a variable. In C, it is mandatory. That's why

I am learning c++ with a C-book

is a huge no. While some of their syntaxes are similar, the rest are vastly different. If you are learning C++, please find the corresponding course/book/video. They're abundance.

For the getline() problem, you can include cin.ignore() for the program to ignore the rest of the character of that line and go to a new line.

#include<iostream>
#include<string.h>
#define SIZE 100

struct person{
    std::string name;
    int age;
};

void entry(person *info){
    std::cin.ignore(); //ignores every character until next line
    std::getline(std::cin, info->name);
    std::cin >> info->age;
}

int main(int argc, char const *argv[])
{
    person roster[SIZE];
    int n; // number of people in the roster:
    std::cin >> n;
    for (int i = 0; i < n; i++)
    {
        entry(&roster[i]);
    }

    for (int i = 0; i < n; i++)
    {
        person cur = roster[i];
        std::cout << cur.name << " " << cur.age << "\n";
    }
    return 0;
}

Result:

2
Beck
8
John Doe
25
Beck 8
John Doe 25
silverfox
  • 1,568
  • 10
  • 27