basic Data Base troubles

I have been using this site as reference for months, but I stumbled upon a problem I cant fix, so I need some help.

Keeping in mind this is a mere test, and it will be better commented and cleaner, but I need to find out whats going wrong now.

It crashes my system for some reason, I think im overstepping my bounds somewhere but I'm not sure.

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
#include <cstdlib>
#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

struct customerDataBase{    //basic customer information struct
     string lastName;
     string firstName;
     string addr1;
     string addr2;
     string eMail;
} Customer;



void TheScreen(customerDataBase customerInfo){  
     //this SHOULD print out all the information in the array
     cout<<"Last name: "<<customerInfo.lastName<<endl;
     cout<<"First name: "<<customerInfo.firstName<<endl;
     cout<<"Addr1: "<<customerInfo.addr1<<endl;
     cout<<"Addr2: "<<customerInfo.addr2<<endl;
     cout<<"E-Mail: "<<customerInfo.eMail<<endl;
}


int main(int argc, char *argv[])
{
     int maxCustomerNumber;  //array size
     int k; //counter
     maxCustomerNumber = 1; 
     customerDataBase customerArray[maxCustomerNumber];
     //declaring an array of size maxCustomerNumber of type customerDataBase
     for (k = 1; k <= maxCustomerNumber; k++){ //fills all info with blanks
         customerArray[k].lastName = " ";
         customerArray[k].firstName = " ";
         customerArray[k].addr1 = " ";
         customerArray[k].addr2 = " ";
         customerArray[k].eMail = " ";
     } 
     TheScreen(customerArray[1]);
     system("PAUSE");
     return EXIT_SUCCESS;
}
You are using your for loop to iterate through an array with only one value. Remember that array elements always start their numbering with 0, change line #35 to:

for (k = 0; k < maxCustomerNumber; k++) {

If you over-run the end of your array as in your code, you get unexpected results.

~psault
Last edited on
Also, you shouldn't need to include <stdlib.h> because you already included <cstdlib>.
Thank you very much, side note, changing the line
TheScreen(customerArray[1]);

into

TheScreen(customerArray[0]);

was needed
Last edited on
Topic archived. No new replies allowed.