试图找到一个数字的平方根但代码不起作用。 C ++

时间:2017-08-25 11:11:58

标签: c++

我昨天开始使用c ++而不能为我的生活弄清楚我做错了什么。我正在尝试制作一个小代码,用于输入三个数字的用户输入,然后计算每个数字的平方根。

#include "stdafx.h"
#include <iostream>
#include <cmath>
using std::cout;
using std::cin;

 int squareroot(){ //function to find squareroot.
 int sQ1 = sqrt(number1); //variable for the square of number1
 int sQ2 = sqrt(number2); //variable for the square of number2
 int sQ3 = sqrt(number3); //variable for the square of number3

 cout << sQ1 << "\n";//outputs the square of number 1
 cout << sQ2 << "\n";//outputs the square of number 2
 cout << sQ3 << "\n";//outputs the square of number 3
}

int main() { // main function
int number1 = 0; //first number
int number2 = 0; //second number
int number3 = 0; //third number

cout << "type number1"; //asks user to input first number
cin >> number1; //stores user input into variable number1

cout << "type number2"; //asks for second number
cin >> number2; //stores second number into number2

cout << "type number3"; // asks for third number
cin >> number3; //stores third number

cout << number1 << "\n"; //outputs number1
cout << number2 << "\n"; //ouputs number2
cout << number3 << "\n"; //outputs number3

squareroot(); //runs function squareroot()
}

3 个答案:

答案 0 :(得分:1)

用这个例子给出一个镜头:

hwndMain = win32gui.FindWindow(None,"busmaster")

答案 1 :(得分:0)

您应该声明squareroot,如:

int squareroot(int number1, int number2, int number3){ //function to find squareroot.
int sQ1 = sqrt(number1); //variable for the square of number1
int sQ2 = sqrt(number2); //variable for the square of number2
int sQ3 = sqrt(number3); //variable for the square of number3
cout << sQ1 << "\n";//outputs the square of number 1
cout << sQ2 << "\n";//outputs the square of number 2
cout << sQ3 << "\n";//outputs the square of number 3
}

然后,将其称为:

squareroot(number1, number2, number3);

无论如何,math.h中的sqrt接受一个double并返回一个double。 见http://www.cplusplus.com/reference/cmath/sqrt/

答案 2 :(得分:0)

现在这可能是我能看到的最接近你想做的事情。我建议你阅读一些关于功能和范围的内容。您应该阅读的内容并不多。

squareroot()不知道main()中的数字。你必须提供它们。另外,避免对平方根或其他类似函数使用整数,整数将截断(向下舍入)到最接近的整数,甚至丢失数据。

#include "stdafx.h"
#include <iostream>
#include <cmath>
using std::cout;
using std::cin;

void squareroot(float n1, float n2, float n3){ //function to find squareroot.
    cout << sqrt(n1) << "\n"; //outputs the square of number 1
    cout << sqrt(n2) << "\n"; //outputs the square of number 2
    cout << sqrt(n3) << "\n"; //outputs the square of number 3
}

int main() { // main function
    float number1 = 0; //first number
    float number2 = 0; //second number
    float number3 = 0; //third number

    cout << "type number1 "; //asks user to input first number
    cin >> number1; //stores user input into variable number1

    cout << "type number2 "; //asks for second number
    cin >> number2; //stores second number into number2

    cout << "type number3 "; // asks for third number
    cin >> number3; //stores third number

    cout << "\n" << number1 << "\n"; //outputs number1
    cout << number2 << "\n"; //ouputs number2
    cout << number3 << "\n"; //outputs number3

    squareroot(number1, number2, number3); //runs function squareroot()
}