what's the undeclared identifier here?

I am writing a simple for loop on my Mac with Xcode, and I've seem to run into the problem of (of course, very noobish probably) an undeclared identifier. Could someone explain to me what it is here?

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
// Nathan Ward
// Homework #5
// Obtain information about person and store them in database. figure bills for money amount entered

#include <iostream>
#include <string>
using namespace std;

int main ()
{
    const int arraysize = 20;
    string firstname[arraysize];
    string lastname[arraysize];
    string phonenumber[arraysize];
    string email[arraysize];
    
    for (i = 0; i < 20; i++)
    {
        cout << "What is your first name?" << endl;
        cin >> firstname[i];
        
        cout << "What is your last name?" << endl;
        cin >> lastname[i];
        
        cout << "Email address?" << endl;
        cin >> email[i];
        
        cout << "Phone number?" << endl;
        cin >> phonenumber[i];
    }
    
    return 1;
}


The undeclared identifier is supposedly the variable "i" inside the loop.
Last edited on
Well, the compiler needs to allocate some place in memory to hold the variable i, and it also need to know what sort of code to generate when dealing with it. In order for it to do that, it needs to know what type of thing i represents. For example is it a string, or a character, or an integer, or a float, or a double, or ...

Now I can't see that information stated anywhere. I can guess that you most likely intend for it to be an int. But the compiler can't guess, it needs to be told.

So either put int i; somewhere before line 17. Or better, change line 17 to read for (int i = 0; i < 20; i++)
OH right! I completely overlooked the fact that I didn't represent what datatype that i was. Thanks!
Topic archived. No new replies allowed.