New at C++

Can someone help me figure out why this program isn't running? I keep getting this message:

32 error: 'N' was not declared in this scope

Below is what I have typed into the program.



//
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int factorial ( int N )
{
// enter the value of N
int Y = 1;

int count;
for( count = 1; count <= N; count++)
{
Y = Y * count;
}

return (Y);
}

int main()
{
// enter the values of x and y
int x;
float Y = 1;
cout << "Enter the value of x: ";
cin >> x;

int count;
for( count = 1; count <= 3; count++)
{
Y = Y + (float)(N^(count))/ (float)factorial(count);
}

cout << "The answer is: " << Y << endl;

return 0;
}
You haven't declared N in the main(). Also, there is no "^" operator in C++. You should use either pow or, what is better in your case, multiplication of N count times.
1
2
3
4
for( count = 1; count <= 3; count++)
{
Y = Y + (float)(N^(count))/ (float)factorial(count);
}

you called variable "N", to the power of count, but "N" is only declared in the "factorial" function, not the "main" function. the code above was from "main", and pretty obvious, it cannot find the variable "N".
I still don't understand what I'm doing wrong. I'm sorry, it may seem obvious to both of you, but computer programming isn't something I understand. I took out ^ and what do you mean by multiplication of N count times? Also do I need a power function as well then?
Instead of N^count you should use pow(float(N), float(count)); (see http://www.cplusplus.com/reference/clibrary/cmath/pow/) or
1
2
3
4
5
6
7
int count;
float power = N;
for( count = 1; count <= 3; count++)
{
Y = Y + X/factorial(count);
X *= N;
}
each declaration of a variable only applies to its function, unless it is a global declaration.

for example,
1
2
3
4
5
6
7
8
9
int main ()
{
int x
}

int sub ()
{
int y
}

in this case, "main" only understands x, but not y, as it is not declared in its function.

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
...
int x;
float Y = 1;
...
for( count = 1; count <= 3; count++)
{
Y = Y + (float)(N^(count))/ (float)factorial(count);
}
...
}

so if we look back to your code, is N declared in main? nope, thus the error. but on the other function

int factorial ( int N )
there it is, the N is declared IN the factorial function.

note that this only explains the error you encounter. you may, or may not encounter another error.
Thank you. Adding int main ( int N ) did take care of my error, but now it is WARNING me that int main (int) only takes zero or two arguments. What does this mean? My program is running, but the only answer I'm receiving is 1.
Melody:

You weren't supposed to add "int N" to the arguments of main(). You were supposed to declare it as a variable within main(). Putting it where you did, tells the compiler it's part of the intc, intv parameter declaration.

So:

1
2
3
4
int main()
{
int X;
...
closed account (zb0S216C)
Variables aren't constrained to main( ). Variables can be declared globally as well, that is, outside of main( ). For example:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
/*
 *
 * Global Namespace (global variables are placed here).
 *
 */
int main( )
{
    // ...
}


Wazzak
Don't teach bad habits; declaring a global variable instead of passing it byVal or byRef is lazy.
@melody
frankly speaking, i think most of the people here don't know what your code do. or maybe its just me lol

1
2
3
int x;
cout << "Enter the value of x: ";
cin >> x;

pretty obvious you declared x here, but after input, you never used it anymore. why?

Y = Y + (float)(N^(count))/ (float)factorial(count);
is the N here supposed to be x? you did not declare N before this, so i'm guessing that you know that you do not need N, but accidentally typed it in?

edit:
one does not usually make declarations within the function brackets, unless its for passing values, as said by @ciphermagi. and global declarations is the easy way out. passing values are more efficient for a program to run.

for example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main ()
{
  ...
  char x;
  side_function(x);
  ...
}

int side_function (char y)
{
  ...
  //y process
  ...
}

declarations in the function brackets are known as arguments.

the code above, has a main function, and a side function. notice that main has no arguments. (tip: usually main has no arguments at all) side_function have an argument though. why?

because, in the main function, we are calling side_function, and we are passing the char x to it. side_function accepts the value of x, and stores it in y. note that x variable is only known to main and likewise for y and side_function.

in return, do we ever call main function? nope. therefore, usually there are no arguments in the main.

side_function can then proceed to process y, and produce a result. this is just one of the simple examples available. hope this helps.
Last edited on
closed account (zb0S216C)
ciphermagi wrote:
Don't teach bad habits; declaring a global variable instead of passing it byVal or byRef is lazy. (sic)

Who died and made you the master programmer? I never said anything like that. I never! declare global variables. I know it's bad practice and I dislike global variables as much as the next guy, so why would I suggest it? I was stating that variables can be declared in in other places other than locally.

Wazzak
Last edited on
Yes there is a ^ operator. It's just not used in the way he thinks it is.
@Framework:
Considering that we're trying to answer this student's questions of "how can this be done", an answer along the lines of "this is possible" without a disclaimer or a recommended alternative immediately preceding or following it can be very well seen as a recommendation. That's all we're saying. :)

-Albatross
Topic archived. No new replies allowed.