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()
}
  • 1
    What error message do you get? Alternatively what output did you get and what output did you expect? Make a [mcve]. – nwp Aug 25 '17 at 11:13
  • `int` as result of `sqrt`? Anyway, it is unclear what you are asking. – unalignedmemoryaccess Aug 25 '17 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](http://stackoverflow.com/help/how-to-ask). – Some programmer dude Aug 25 '17 at 11:13
  • 2
    You forgot to actually give `squareroot` any of the numbers – harold Aug 25 '17 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](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to read? – Some programmer dude Aug 25 '17 at 11:14
  • On an unrelated note, and something you should learn if you read a good book, is that if you declare a function to return a value (like you do with your `squareroot` function, you declare it should return an `int` value) then you *must* return something. Otherwise you will have something called *undefined behavior*. – Some programmer dude Aug 25 '17 at 11:17
  • The code doesn't build because number1, number2 and number3 is undefined. I understand that the numbers are in the main function and not in the squareroot function but I am unsure how to declare global variables and get user input on them. – D. Ravndal Aug 25 '17 at 11:23
  • int squareroot - is rarely right (most roots aren't ints) and secondly, you must return a value. I think you should return to your c++ book section on functions. – UKMonkey Aug 25 '17 at 11:27

3 Answers3

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

}
Mr. Go
  • 567
  • 1
  • 5
  • 20
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/

Federico
  • 743
  • 9
  • 22
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()
}
N00byEdge
  • 1,106
  • 7
  • 18
  • 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 ;) – D. Ravndal Aug 25 '17 at 11:40
  • No problem! Feel free to accept as answer if it solved your problem. – N00byEdge Aug 25 '17 at 11:43