0

I started c++ yesterday and can't for the life of me figure out what I am doing wrong. I am trying to make a small code that takes user input for three numbers and then calculates the squareroot of each number.

#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()
}
8
  • 1
    What error message do you get? Alternatively what output did you get and what output did you expect? Make a minimal reproducible example. Commented Aug 25, 2017 at 11:13
  • int as result of sqrt? Anyway, it is unclear what you are asking. Commented Aug 25, 2017 at 11:13
  • And what is the problem with the code you show? Doesn't it build? Do you get crashes? Unexpected results? Please elaborate. And please read about how to ask good questions. Commented Aug 25, 2017 at 11:13
  • 2
    You forgot to actually give squareroot any of the numbers Commented Aug 25, 2017 at 11:14
  • 2
    My guess though is that you have to learn about scoping and that variables declared in one scope (like a function) can't be used in another non-nested scope (like another function). Perhaps it's time to get a couple of good beginners books to read? Commented Aug 25, 2017 at 11:14

3 Answers 3

1

Give a shot with this example :

#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

}
Sign up to request clarification or add additional context in comments.

Comments

0

You should declare squareroot like:

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
}

and then, call it like:

squareroot(number1, number2, number3);

Anyway, sqrt from math.h accepts a double and returns a double. see http://www.cplusplus.com/reference/cmath/sqrt/

Comments

0

Now this is probably the closest to what I could see that you'd want to do. I'd recommend reading up a bit more on functions and scopes. There isn't much of a single point that you should read up on.

The squareroot() doesn't know about the numbers in main(). You would have to provide them to it. Also, avoid using ints for square roots or other similar functions, ints will truncate (round down) to the nearest integer or even lose data.

#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()
}

2 Comments

Thanks! That worked wonders, I'll read up on functions and scoping to get a better understanding and try to avoid using int for such purposes from now on ;)
No problem! Feel free to accept as answer if it solved your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.