2016-02-28 147 views
-2

我正在寫一個簡單的C++程序,它將不同主題中的學生信息及其標記。爲什麼程序在調用函數時跳過輸入?

#ifndef STUDENT_H_ 
#define STUDENT_H_ 
#include<string> 


class Student { 
private: 
    std::string name; 
    std::string dept; 
    unsigned char age; 
    std::string usn; 
public: 
    Student(); 
    void getinfo(); 
    void display(); 
    virtual ~Student(); 
}; 


class Score:public Student { 
private: 
    int math; 
    int sci; 
    int chem; 
    int english; 
    int SS; 
public: 
    void marks(); 
    void dispscore(); 
    Score(); 

}; 

#endif /* STUDENT_H_ */ 

以上是頭文件。

#include "Student.h" 
#include <iostream> 

using namespace std; 

Student::Student() 
{ 
age = 0x00; 
} 

Student::~Student() 
{ 

} 
Score::Score() 
{ 
    math = 0x00; 
    sci = 0x00; 
    chem = 0x00; 
    SS = 0x00; 
    english = 0x00; 
} 

void Student::getinfo() 
{ 
    cout<<"Enter the Students Name: "<<endl; 
    getline(cin,name); 
    cout<<"Enter Department--> "<<endl; 
    getline(cin,dept); 
    cout<<"Enter USN--> "<<endl; 
    getline(cin,usn); 
    cout<<"Enter Age--> "<<endl; 
    cin>>age; 
} 

void Student::display() 
{ 
    cout<<name<<endl; 
    cout<<dept<<endl; 
    cout<<usn<<endl; 
    cout<<age<<endl; 
} 

void Score::marks() 
    { 
    cout<<"Enter Math Score: "<<endl; 
    cin>>math; 
    cout<<"Enter Science Score: "<<endl; 
    cin>>sci; 
    cout<<"Enter Chemistry Score: "<<endl; 
    cin>>chem; 
    cout<<"Enter English Score: "<<endl; 
    cin>>english; 
    cout<<"Enter the Social Studies Score: "<<endl; 
    cin>>SS; 

} 

    void Score::dispscore() 
{ 
    cout<< math << endl; 
    cout<<sci<<endl; 
    cout<<chem<<endl; 
    cout<<english<<endl; 
    cout<<SS<<endl; 

} 

int main() 
{ 
    Score s; 
    s.getinfo(); 
    s.marks(); 
    s.display(); 
    s.dispscore(); 


} 

以上是cpp文件。編譯後,我得到的輸出如下所示,並面臨以下問題,它們是: 1.跳過數學輸入分數並直接從科學輸入分數開始, 2.輸入的年齡爲23,但是在控制檯,它不顯示它爲「23」,但在輸出中看到一行2和下一行3 - >這是否發生,因爲我按enter ?.

Enter the Students Name: 
Smith Diaz 
Enter Department--> 
ECE 
Enter USN--> 
4So08ec112 
Enter Age--> 
23 
Enter Math Score: 
Enter Science Score: 
77 
Enter Chemistry Score: 
66 
Enter English Score: 
89 
Enter the Social Studies Score: 
57 
Smith Diaz 
ECE 
4So08ec112 
2 
3 
77 
66 
89 
57 
+0

'cin'爲你下一個'getline()'操作帶''\ n''。 –

回答

0

age具有類型unsigned char,所以cin只讀一個字符,並將其存儲在那裏。

嘗試將會員的類型age更改爲int

+0

我改變了從char到int的類型,它解決了這兩個問題。謝謝! – PsychedGuy

相關問題