Error: (A) not declared in this scope

Nov 7, 2018 at 5:59pm
Hey, I try to start the program, but it says that a variable is not declared in this scope, even though I did initiate it. Any help?


#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;


const char fd[] = ("Duomenys.txt");
const char fr[] = ("Rezultatas.txt");
const int Cmax = 100;
int main()
{
setlocale(LC_ALL, "Lithuanian");

int Ug[Cmax]; int n;
void Skaityti (int A[], int & n);
{
ifstream fd ("Duomenys.txt");
fd >> n;
for (int i = 0; i < n; i++)
fd >> A[i];
fd.close();
}


return 0;
}
Nov 7, 2018 at 6:06pm
You cannot declare define a function inside another function.
Remove the semicolon after void Skaityti (int A[], int & n)
and move the whole function outside main().
Last edited on Nov 7, 2018 at 6:18pm
Nov 7, 2018 at 6:17pm
Still doesn't let me run it, then it just says "Error: expected unqualified-id before '{' token".
Nov 7, 2018 at 6:18pm
You cannot declare a function inside another function.

You can declare a function inside another function; you just can't define a function inside another function.

That is why the OP is getting that specific error message. The errant semicolon at the end of void Skaityti (int A[], int & n); means the compiler reads the line as a declaration, not the start of the definition.

Then, the opening brace on the next line is simply read as the start of a nested code block within main().

Then, when the symbol A is encountered, the compiler doesn't know what it is, because nothing called A has been declared within the scope of that nested block, nor within the scope of main(), nor at any higher scope.
Last edited on Nov 7, 2018 at 6:18pm
Nov 7, 2018 at 6:18pm
Still doesn't let me run it, then it just says "Error: expected unqualified-id before '{' token".

Did you remove the semicolon?
Last edited on Nov 7, 2018 at 6:19pm
Nov 7, 2018 at 6:20pm
Still doesn't let me run it, then it just says "Error: expected unqualified-id before '{' token".

Please post your new code. Without seeing exactly what you've done, we can't be sure what you've done wrong.
Nov 7, 2018 at 6:57pm
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;


const char fd[] = ("Duomenys.txt");
const char fr[] = ("Rezultatas.txt");
const int Cmax = 100; //masyvo dydis
int main()
{
setlocale(LC_ALL, "Lithuanian");

int Ug[Cmax]; int n;
return 0;
)
void Skaityti (int A[], int & n)
{

ifstream fd ("Duomenys.txt");
fd >> n;
for (int i = 0; i < n; i++)
fd >> A[i];
fd.close();
}









Nov 7, 2018 at 8:40pm
You have the wrong sort of bracket at the end of main.

) should be }
Nov 7, 2018 at 9:02pm
Do note that this forum supports "code tags" that make code more readable. See: http://www.cplusplus.com/articles/jEywvCM9/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

const char fd[] = ("Duomenys.txt");
const char fr[] = ("Rezultatas.txt");
const int Cmax = 100; //masyvo dydis

int main()
{
  setlocale(LC_ALL, "Lithuanian");
  int Ug[Cmax];
  int n;
  // no functions were called here
  return 0;
)  // <-- mismatching parenthesis


void Skaityti( int A[], int & n )
{
  ifstream fd ("Duomenys.txt");
  fd >> n;
  for (int i = 0; i < n; i++)
    fd >> A[i];

  fd.close();
}


Many* code editors do have similar visual features, and might suggest indentation, etc too.


Compiler does mention line numbers:
1
2
17:1: error: expected primary-expression before ')' token
28:1: error: expected '}' at end of input

Line 17 does have that mismatching ).

Line 28 has }, but that matches with { on line 21.
Therefore, the { on line 11 is without a pair.


* I presume. I've used only one.
Last edited on Nov 7, 2018 at 9:07pm
Nov 8, 2018 at 3:26pm
Okay, it works now. Thank you guys so much!
Nov 8, 2018 at 3:45pm
You're welcome! Glad it helped, and I hope the explanations made sense.
Topic archived. No new replies allowed.