2016-04-19 22 views
0

此代碼不會產生錯誤,但在輸出只是這條線將顯示,然後程序退出:C++類的輸出不能完全顯示

cout << "write 1 for areaoftrapezium and 2 for areaofrhombus and 3 for      areaofParallelogram " << endl; 
cin >> option; 

這裏的全部代碼,我不知道什麼是錯的

#include<iostream> 
using namespace std; 

class project 
{ 

private: 
float base, base2, height; 
float diagonal, diagonal2; 
float base3, aldtude; 
    public: 
void trapezium() { 

    float areaoftrapezium; 
    areaoftrapezium = 0.5*(base + base2)*height; 
    cout << "the area of trapezium is:" << areaoftrapezium; 
    } 
void rhombus() { 

    float areaofrhombus; 
    areaofrhombus = 0.5*diagonal*diagonal2; 
    cout << "the area of rhombus is:" << areaofrhombus; 
} 
void Parallelogram() { 

    float areaofParallelogram; 
    areaofParallelogram = base3*aldtude; 
    cout << "the area of Parallelogram is:" << areaofParallelogram; 
} 

project(int a, int b, int c){ 
    base = a; 
    base2 = b; 
    height = c; 
} 
project(int d, int e) { 

    diagonal = d; 
    diagonal2 = e; 

} 


float getbase() { 
    return base; 
} 
float getbase2() { 
    return base2; 
} 
float getheight() { 
    return height; 
} 
float getdiagonal() { 
    return diagonal; 

} 
float getdiagonal2() { 
    return diagonal2; 
} 
float getbase3() { 
    return base3; 
} 
float getaldtude() { 
    return aldtude; 
} 
}; 

int main() 

    { 
    int a, b, c, d, e, f, h; 

int option = 0; 

project obj(); 


cout << "write 1 for areaoftrapezium and 2 for areaofrhombus and 3 for areaofParallelogram " << endl; 
cin >> option; 
switch (option) { 

case '1': 
{ 
    cout << "Enter the value for two bases & height of the trapezium: " <<  endl; 
    cin >> a; 
    cin >> b; 
    cin >> c; 

    project obj(a, b, c); 
    obj.trapezium(); 

} 
break; 

case '2': 
{ 
    cout << "Enter diagonals of the given rhombus:" << endl; 
    cin >> d; 
    cin >> e; 
    project obj(d, e); 
    obj.rhombus(); 
} 
break; 

case '3': 
{ 
    cout << "Enter base and altitude of the given Parallelogram: " << endl; 
    cin >> f; 
    cin >> h; 
    project obj(f, h); 

    obj.Parallelogram(); 

} 
break; 

} 

system("pause"); 
return 0; 

} 

請告訴我我失蹤了什麼?

+0

嘗試在你的switch-case中添加一個默認塊,看看它是否進入它。 – Bettorun

回答

5

您正在混淆數字,例如數字,如'1'。他們是完全不同的東西。頭號是我有多少頭。數字「1」是可以表示阿拉伯數字系統中的第一位的標記。

int option = 0; 

好的,option是一個整數。

cin >> option; 

而且你從用戶讀取一個整數。

switch (option) { 
case '1': 

然後你把它比作人物1,除非你想把它比作頭號。

如果您從用戶那裏讀取數字,請將它們與數字相比較。如果您閱讀用戶的字符,請將它們與'1'等字符進行比較。保持直線。