C++ c++;为多个函数使用用户输入数据
嗨,我想写一个任意三角形的程序。 虽然我已经完成了任务的第一部分,那就是找出三角形是真是假。我希望能够使用用户输入的数据来计算周长,然后最终计算三角形的面积 但当计算周长时,它是一个相当大的数字C++ c++;为多个函数使用用户输入数据,c++,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
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;
}
问题在于,所有
%d
都应替换为%lf
,以匹配类型double
并删除行
scanf_s(“%d”、&a、“%d”、&b、“%d”、&c)代码>,一旦扫描一次,你不能再次扫描以获得相同的值。你期望的输出是什么,现实是什么?15+15+32应该等于62,而我得到19992646这不是C。删除printf
和scanf
东西,使用std::cout
和std::cin
@ChristianHackl我的类要求我使用printf和scanf。我已经将所有%d更改为%lf,现在我收到0.0000,我也只想输入此数据一次,而不想再次输入。@Elijahceney删除行scanf\s(“%d”、&a、“%d”、&b、“%d”、&c)代码>,谢谢,我仍然得到周长等于0.000而不是a+b+c的问题。@Elijahceney这是因为你的程序也包含很多打字错误:例如:printf_s(“三角形的周长是:“%d,p”)代码>应该是printf_s(“三角形的周长是:%d”,p)代码><代码>如果(“a+b>c&&a+c>b&&b+c>a”)
应该是if(a+b>c&&a+c>b&&b+c>a)
。你需要更多的学习和练习来避免这种错误。