Can anyone help me pls

On lines 46 (for) and 52 ( '}' ) it says it is expected a declaration. What does that mean?

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
int main()
{
	using namespace std;
	
	string fName, lName;
	char ans, more;
	void FirstName();
	void LastName();
	const int SIZE = 10;  
    float scores[SIZE]; 
    float total = 0;  

		cout << "First Name: ";
	getline(cin,fName);

	if (fName.length() != 0)
	{
		cout << "Last Name: ";
		getline(cin,lName);
	}
	else
	{
		cout << "Invalid input!!";
		return 0;
	}

		if (lName.length() != 0)
		{
		  cout << "Score ";
		  cin >> scores[i];
		}
		else
		{
			cout << "Invalid input!!";
			return 0;
		}

   return 0;
}



	int i;

 
  for (int i = 0; i < SIZE; i++)
   {
      cout << "Score " << i + 1 << ": ";
      cin >> scores[i];
	 // cout<<"More? :";
	//  cin>>more;
   }

  while(more != 'n')
  {
   
   cout<<"more?";
   cin>> more;
}
  if (more == 'y' || more == 'Y')
  {
	  cin>>scores[i];
  }

  if (more == 'n' || more == 'N')
  {
	cout<<"end";
  }
    else
  {
	cout<<"end";
  }

  for (int i = 0; i < SIZE; i++)
   {
      total += scores[i];
   }

  if (more == 'y' || more == 'Y' || more == 'n' || more == 'N')
  {
	  cout<<"end"<<endl;
  }
  else
  {
	cout<<"end";
  }
Last edited on
It's out of scope of the SIZE variable. That is, SIZE only exists with the scope of main.

Incidentally, why do you have a bunch of code outside of main?
Last edited on
Your code after line 39 is not inside main() or any other function.
I changed it and put everything inside main but the program is still not running right. It says the variable 'i' is used without being initialized.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <string>
#include <iomanip>






int main()
{
	using namespace std;
	
	string fName, lName;
	char ans, more;
	void FirstName();
	void LastName();
	const int SIZE = 10;  
    float scores[SIZE]; 
    float total = 0;  
	int i;

		cout << "First Name: ";
	getline(cin,fName);

	if (fName.length() != 0)
	{
		cout << "Last Name: ";
		getline(cin,lName);
	}
	else
	{
		cout << "Invalid input!!";
		
	}

		if (lName.length() != 0)
		{
		  cout << "Score ";
		  cin >> scores[i];
		}
		else
		{
			cout << "Invalid input!!";
			
		}

	
 
  for (int i = 0; i < SIZE; i++)
   {
      cout << "Score " << i + 1 << ": ";
      cin >> scores[i];
	  cout<<"More? :";
	  cin>>more;
	  
   }


  while(more != 'n')
  {
   
   cout<<"more?";
   cin>> more;
}
  if (more == 'y' || more == 'Y')
  {
	  cin>>scores[i];
  }

  if (more == 'n' || more == 'N')
  {
	cout<<"end";
  }
    else
  {
	cout<<"end";
  }

  for (int i = 0; i < SIZE; i++)
   {
      total += scores[i];
   }

  if (more == 'y' || more == 'Y' || more == 'n' || more == 'N')
  {
	  cout<<"end"<<endl;
  }
  else
  {
	cout<<"end";
	return 0;
  }
}
Last edited on
You're returning a couple of times there where I don't think you should be.
Well I don't think that was the problem bcuz I just removed them and it said the samething
Return is still in the wrong place. You effectively have a branch of logic where return is never hit. You should always return from main.

As for the problem with i, you're redeclaring it. Need to remove the int from lines 50 and 80 there.
ahh icic
So where would I have to return in this case?
It says the variable 'i' is used without being initialized.

The problem was not what iHutch105 said but because in line 40 when you use i, it has not been given a sensible value and is possibly indexing out of bounds of your array.

i must have a value before this line is executed.
cin >> scores[i];

In this loop the user will be asked "More" 10 times and their response is never acted upon.

1
2
3
4
5
6
7
8
  for (int i = 0; i < SIZE; i++)
   {
      cout << "Score " << i + 1 << ": ";
      cin >> scores[i];
	  cout<<"More? :";
	  cin>>more;
	  
   }


Remove return from the else branch and place on a line before the closing brace of main.
Last edited on
The problem was not what iHutch105 said but because in line 40 when you use i, it has not been given a sensible value and is possibly indexing out of bounds of your array.

i must have a value before this line is executed.


i does have a value at this point. It's assigned 0 at the first iteration of the loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

int main()
{
   int i;

   for (i=0; i<5; i++) // i initialised in the first statement
   {
      cout << "i is: " << i << endl;
   }

   cout << "Final value of i is: " << i << endl;
}


Results
i is: 0
i is: 1
i is: 2
i is: 3
i is: 4
Final value of i is: 5
Last edited on
Yes but in OPs code i is not assigned before it is used in line 40.

Redeclaring i in for (int i = 0; i < SIZE; i++) is no problem since it is a different variable.
Yes but in OPs code i is not assigned before it is used in line 40.

Oh, I see what you mean. I didn't spot that scores[i] at line 40, I was looking at the one on line 55. Yeah, this will cause problems. It should compile fine, but at that point i will hold nothing but junk.

Redeclaring i in for (int i = 0; i < SIZE; i++) is no problem since it is a different variable.

However, this isn't true. If i already exists in the scope of this loop then this would be considered a redefinition of i. Should result in a compilation error.
Last edited on
However, this isn't true. If i already exists in the scope of this loop then this would be considered a redefinition of i. Should result in a compilation error.


Did some searching on this and it may be depend on the compiler. I had assumed it was a different variable due to it only having scope in the loop.

The following code compiled in my version of CodeBlocks (gcc).

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

using namespace std;

int main (int argc, char *argv[])
{
    int i;
    cout<<i<<" "<<&i<<endl;
    for (int i=0;i<5;i++){cout<<i<<" "<<&i<<endl;}
    cout<<i<<" "<<&i<<endl;
    return 0;
}
Last edited on
Did some searching on this and it may be depend on the compiler. I had assumed it was a different variable due to it only having scope in the loop

Most likely, yeah. VS throws a compile-time error.

I thought the scope of a loop is what's between the { }? Maybe that's open to compiler interpretation.
Topic archived. No new replies allowed.