0

Like the title, I can't receive it continuously. At the first input, it's very normal. But when I press Enter to go to next input, it turned out like this:

Image :D

Here is my source code:

#include <iostream>
#include <fstream>
#include <string>
#include <stddef.h>
#include <stdint.h>

using namespace std;

typedef struct SV {
    char name[50];
    uint8_t age;
    char gender;
    float average;
    float math;
    float literature;
    float chemical;
    float physical;
    float biological;
} SV_Info;

class SV_Manager {
    private:
        SV_Info *sv_info;
    public:
        /**
         * @brief Constructor SV_Manager, nếu không truyền 
         * tham số là số sinh viên tối đa hiện tại thì sẽ 
         * mặc định là 100 sinh viên
         * 
         */
        SV_Manager() {
            sv_info = new SV_Info[100];
        }

        SV_Manager(size_t max_sv) {
            sv_info = new SV_Info[max_sv];
        }

        void Add_SV() {
            cout << "Nhap ten cua sinh vien (toi da 50 ki tu): ";
            char name[50];
            cin >> name;
            cout << "Nhap tuoi cua sinh vien: ";
            uint8_t age;
            cin >> age;
            cout << "Nhap gioi tinh cua sinh vien: ";
            char gender[10];
            cin >> gender;
            cout << "Nhap diem Ngu Van cua sinh vien: ";
            float literature;
            cin >> literature;
            cout << "Nhap diem Toan cua sinh vien: ";
            float math;
            cin >> math;
            cout << "Nhap diem Ly cua sinh vien: ";
            float physical;
            cin >> physical;
            cout << "Nhap diem Sinh cua sinh vien: ";
            float biological;
            cin >> biological;

        }
};


int main(int argc, char **argv) {
    std::cout << "Hello World!" << std::endl;
    SV_Manager manager;
    manager.Add_SV();
    system("pause");
}

I'm using MinGW GCC 6.3.0 to compile this. I have tried all solutions on Internet but it's useless. Can anyone help me?

kiner_shah
  • 3,939
  • 7
  • 23
  • 37
  • For multi-word strings, use `cin.getline`. – kiner_shah Nov 19 '21 at 03:22
  • Err... I followed your advice. It's worked, but for "name" variable. It's happens again when I use ```cin.getline``` with "gender". But it's even worse, it doesn't even wait for me to press any key and skip that variable. – NeonLightions Nov 19 '21 at 03:28
  • ***it's even worse*** it's should be better than before really. My guess is you can't enter the gender properly. Read this: https://stackoverflow.com/a/10553849/4688321 – kiner_shah Nov 19 '21 at 03:33
  • 1
    Thank you, it's worked! – NeonLightions Nov 19 '21 at 03:54

0 Answers0