簡體   English   中英

在結構指針方面需要幫助

[英]Need help in structure pointers

在c ++中,我必須編寫一個程序,在該程序中,我必須編寫一個菜單驅動程序來輸入,編輯或顯示學生的姓名,ID和cgpa。 代碼如下:

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;

struct student
{
    char name[20];
    int id;
    float cgpa;
};

void input(struct student *);
void display(struct student *);
void edit(struct student *);

int main()
{
    int ch;
    struct student a;

    while(ch!=4)
    {
        cout<<"1.input";
        cout<<"\n2.edit";
        cout<<"\n3.display";
        cout<<"\n4.exit";
        cout<<"\nenter your choice";
        cin>>ch;
        if(ch==1)
        {
            cout<<"enter name, id and cgpa of student\n";
            input(&a);
        }
        else if(ch==2)
        {
            cout<<"enter new name, id and cgpa  of the student\n";
            edit(&a);
        }
        else if(ch==3)
        {
            display(&a);
        }
        else if(ch==4)
        {
            ch=ch;
        }
        else{cout<<"wrong choice\n";}
    }

    return 0;
}

void input(struct student *s)
{
    cin>>s->name;
    cin.ignore(256,'\n');
    cin>>s->id;
    cin>>s->cgpa;
}

void edit(struct student *s)
{
    cin>>s->name;
    cin.ignore(256,'\n');
    cin>>s->id;
    cin>>s->cgpa;
}

void display(struct student *s)
{
    cout<<s->name<<"\n";
    cout<<s->id<<"\n";
    cout<<s->cgpa<<"\n";
}

但是,每當我通過cin.getline()函數輸入名稱時,它都不會在屏幕上顯示名稱。 而是在此處留有空白,並打印id和cgpa。 Cin工作正常,但cin不占空間。 使用cin.getline()時未打印名稱的代碼有什么問題。

在調用cin.ignore()之前先調用cin.getline()

要么

進行虛擬調用cin.getline()以使用cin的尾隨換行符

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM