c ++类输出没有完全显示

时间:2016-04-19 21:42:49

标签: 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;

}

请告诉我我错过了什么?

1 个答案:

答案 0 :(得分:5)

您将数字(例如数字)与数字混淆,例如'1'。它们是完全不同的东西。第一个是我有多少头。数字&#34; 1&#34;是一个可以代表阿拉伯数字系统中第一的标记。

int option = 0;

好的,option是一个整数。

cin >> option;

你从用户那里读了一个整数。

switch (option) {
case  '1':

然后将它与角色1进行比较,除非您想将其与第一个进行比较。

如果您从用户那里读取数字,请将它们与一个数字进行比较。如果您从用户那里读取字符,请将它们与'1'之类的字符进行比较。保持原状。