c ++使用多个功能的用户输入数据

时间:2017-04-09 09:10:22

标签: c++

您好我正在寻找任意三角形的程序。 虽然我完成了我的任务的第一部分,即找出三角形是真还是假。我希望能够使用用户输入的数据来计算周长,然后最终计算三角形的面积。

但是当计算周长时,这是一个相当大的数字。

This is my code so far.#include "stdafx.h"
#include "math.h"

    enter code here

// ConsoleApplication6.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "math.h"

/* enter three variables a, b ,c to create a triangle*/
int main()
{
    double a;   /*insert dimensions of side a*/
    double b;   /*insert dimensions of side b*/
    double c;   /*insert dimensions of side c*/
    double p;   /*variable for the perimeter of a triangle*/
    double s;   /*variable for the area of a triangle*/

    /* Get the user to enter the dimensions of a*/
    printf_s("enter the  dimensions of a: ");   
    scanf_s("%d", &a);                          

    /* Get the user to enter the dimensions of b*/
    printf_s("enter the  dimensions of b: ");   
    scanf_s("%d", &b);
    /* Get the user to enter the dimensions of c*/
    printf_s("enter the  dimensions of c: ");   
    scanf_s("%d", &c);                          

    /* Conditions of a triangle*/
    if ("a + b > c && a + c > b && b + c > a")  

        printf_s("True\n");     /* Display True if able to make a triangle*/

    else printf_s("False\n"); /* Display false if unable to make a triangle*/

    double p = a + b + c;   

    /*Scan user input data a, b, c*/
    scanf_s("%d", &a, "%d", &b, "%d", &c);

    /*output total perimeter*/
    printf_s("The perimeter of the triangle is: ""%d, p");  





    return 0;
}

1 个答案:

答案 0 :(得分:1)

问题是所有%d都应替换为%lf才能匹配double类型。

并删除行scanf_s("%d", &a, "%d", &b, "%d", &c);,一旦扫描一次,就无法再次扫描以获得相同的值。