|
-
January 2nd, 2016, 05:06 PM
#16
Re: c++ Structures export data for specific name, country, last name
Well I do it like this:
Code:
char choose[1];
do {
cout << "New check? Y/N \n";
cin >> choose;
if (_stricmp("N", choose) == 0)
{
break;
}
char name_for_check[10];
cout << "\n Name for check:";
cin >> name_for_check;
for (i = 0; i < n; i++)
{
if (strcmp(array[i].first_name, name_for_check) == 0)
{
cout << array[i].first_name << " ";
cout << array[i].last_name << " ";
cout << array[i].country << endl;
}
}
}
while (_stricmp("Y", choose) == 0);
system("pause");
}
The code is working but after I run it it show this message:
Run-Time Check Failure #2 - Stack around the variable 'choose' was corrupted.
-
January 2nd, 2016, 05:21 PM
#17
Re: c++ Structures export data for specific name, country, last name
As choose is defined to be a c-style string and not a char, then
is going to treat choose as a c-style array and terminate it with a null. This makes the string at least 2 chars but you have only allocated space for 1!
This is why using c-style strings in a c++ program is a bad idea Get your teacher to explain using the c++ string type.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|