Kyle Remmings
Kyle Remmings

Reputation: 21

'num1' was not declared in this scope

#include <cmath>
using namespace std;
int answer;
int cube (int num1)
{
    answer = num1 * num1 * num1;
    return answer;
}

int main()
{
    cout << "enter your number to cube";
    cin >> num1;
    cube(num1);
    return 0;
}

Extremely new to coding and have less than a day of experience, can anyone tell me what I did wrong?

Upvotes: 2

Views: 5066

Answers (6)

tyChen
tyChen

Reputation: 1494

If you want to read a input to store as a variable, you need firstly declare it like below.

    int num1;
    cin >> num1;

The first line tells the computer that you want some space to store a variable, and then the input can be stored in it by calling cin.

One more thing, it's not good to use using namespace std, it will cause many strange problems. And I suggest you learn some knowledge about operating system, it'll be very helpful for programming.

Upvotes: 0

Anmol
Anmol

Reputation: 11

Scope refers to visibility of variables, meaning which parts of program can see it or use it. The variable "answer" has global scope. A variable in global scope can be used anywhere in the program. That is why you can use variable answer inside function cube. But the variable num1 is declared inside function cube. So it has scope local to function cube. Thus variable num1 has no existence outside of function cube, for function main no variable of name num1 exists. This is why you are getting an error 'num1' was not declared in this scope. To get rid of this error declare 'num1' in scope of main before its first use.

Upvotes: 1

Colin Hicks
Colin Hicks

Reputation: 360

C++ has a concept called scope.

num1 was declared in the scope of cube but not in main. Essentially what this means is, the name num1 has meaning in cube since you declared the variable in there but because the scope of that name is limited to the cube function, the name was undefined when you tried to reference it in main.

This post gives a beginner friendly introduction to some of the concepts discussed here

Upvotes: 1

Rakmo
Rakmo

Reputation: 1982

Full Code working:

#include <cmath>
using namespace std;
int answer;
int cube (int num1)
{
    answer = num1 *num1*num1;
    return answer;
}

int main()
{
    int num1;
    cout << "enter your number to cube";
    cin >> num1;
    cube(num1);
    return 0;
}

Upvotes: 0

TruckerCat
TruckerCat

Reputation: 1487

The error message 'num1' was not declared in this scope means that the variable you are trying to use is not existing in the place where you are trying to use it.

You have not declared num1. You need to declare it like this in your main function.

int main()
{
    cout << "enter your number to cube";
    int num1 = 0;
    cin >> num1;
    cube(num1);
    return 0;
}

Upvotes: 1

RvdK
RvdK

Reputation: 19790

Your trying to store the input from the user into num1, but you haven't declared num1 yet. (looking up the error message in google should have gotten you enough info)

int num1;
cin >> num1;

Upvotes: 0

Related Questions