0

I'm trying to assign new User Input values into a struct in DevC++. But whenever it gets to the point when the user values are being assigned to the struct, the program crashes and Windows will say it stopped working.

Here's the struct:

struct users{ string uCode; string lName; string fName; string mInit; char type; char gender; } users_t[10]; 

And here's the function that performs the assignment:

void createAccount(){ string newUCode; string newLName; string newFName; string newMInit; char newGender; char newType; system("CLS"); showBordersMain(); gotoxy(30, 6); cout << "<!-- ACCOUNT CREATION -->"; gotoxy(20, 10); cout << "USER CODE : "; cin >> newUCode; if (newUCode.length() > 5){ gotoxy(20, 17); cout << "USER CODE MUST BE 5 CHARACTERS IN LENGTH.\n"; system("PAUSE"); createAccount(); } for (int i = 0; i < uCount - 1; i++){ if (newUCode == users_t[i].uCode){ gotoxy(20, 17); cout << "USER CODE ALREADY EXISTS!\n"; system("PAUSE"); createAccount(); } } gotoxy(20, 11); cout << "LAST NAME : "; gotoxy(20, 12); cout << "FIRST NAME : "; gotoxy(20, 13); cout << "MIDDLE INITIAL : "; gotoxy(20, 14); cout << "GENDER [M/F] : "; gotoxy(20, 15); cout << "ACCOUNT TYPE [A/C]: "; gotoxy(40, 11); cin >> newLName; gotoxy(40, 12); cin >> newFName; gotoxy(40, 13); cin >> newMInit; gotoxy(40, 14); cin >> newGender; gotoxy(40, 15); cin >> newType; //Problem starts here users_t[uCount].uCode = newUCode; //Program crashes before it reaches this point users_t[uCount].lName = newLName; users_t[uCount].fName = newFName; users_t[uCount].mInit = newMInit; users_t[uCount].gender = newGender; users_t[uCount].type = newType; gotoxy(20, 17); cout << "NEW USER ADDED!\n"; system("PAUSE"); } 

Another thing, I had gotoxy() defined, in case you're wondering, and I'm using Orwell's DevC++ 5.10.

7
  • 1
    Where is uCount defined? Commented Mar 27, 2015 at 14:34
  • 3
    "I'm using Orwell's DevC++ 5.10"... you poor poor thing Commented Mar 27, 2015 at 14:34
  • 1
    Since you're looping from 0 to uCount-1, I can only assume that uCount is 10, making users_t[uCount] an out of bounds access. Lots of guessing here, since the relevant portions of code are not included. Commented Mar 27, 2015 at 14:36
  • 1
    Please post minimal example that represents the problem is self contained (compiles as posted). Commented Mar 27, 2015 at 14:41
  • 1
    Please don't take my comment as extremist or offensive... but is there any special reason for you to use DevC++? There are many IDE's out there that provide great interface, nice debuggers, etc. I will not advertise anyone specific here, but I sincerely suggest you to install another one. Many of them are also open source, and you can follow its development, bugfixes, etc. Commented Mar 27, 2015 at 14:51

1 Answer 1

1

I guess that uCount is set to 10? If so the following line will most likely lead to a segmentation fault and crash your program

users_t[uCount].uCode = newUCode; 

As users_t is an array of 10 elements, you can only access the ones between the indexes 0 and 9 included.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.