2017-08-25 30 views
0

我昨天開始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() 
} 
+1

什麼錯誤信息,你得到什麼?或者,你得到了什麼輸出,你期望輸出什麼?製作[mcve]。作爲'sqrt'結果的'int'是否爲 – nwp

+0

?無論如何,你不清楚你在問什麼。 – tilz0R

+0

你顯示的代碼有什麼問題?它不是建立?你有沒有崩潰?意外的結果?請詳細說明。並請[請閱讀如何提出良好問題](http://stackoverflow.com/help/how-to-ask)。 –

回答

0

您正在嘗試訪問函數中的number1-3而不傳遞其引用。你必須通過PARAMS通過數字或定義number1-3作爲一個全局變量

0

你應該申報平方根,如:

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接受一個double並返回一個double。 參見http://www.cplusplus.com/reference/cmath/sqrt/

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() 
} 
+0

謝謝!這有效奇蹟,我會閱讀函數和範圍以便更好地理解,並嘗試從現在開始避免將int用於這種目的;) –

+0

沒問題!如果它解決了您的問題,請隨時接受答案。 – N00byEdge

1

給這個例子一拍:

#include <iostream> 
#include <cmath> 
using std::cout; 
using std::cin; 

double squareroot(double number){ //function to find squareroot. 
    return sqrt(number); //variable for the square of number1 
} 

int main() { // main function 

    int number1 = 0; //first number - integer 
    int number2 = 0; //second number - integer 
    int number3 = 0; //third number - integer 

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

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

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

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

}