Run-Time Check Failure #2 - Stack was corrupted.

Have seen this error posted but no solutions that work for me.Here is what I have and it goes thru the debug but doesn't compile without this error message usually with the error marker around line 20. The call stack says !main() Line 20 + 0xfbytes, and That's greek to me. Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
using namespace std;

	int main ()
	{
	    const int NUM_LETTERS = 11;
	    char letters [NUM_LETTERS];
	       
	    cout << "Please enter up to 10 letters." << endl;
	    cin >> letters [NUM_LETTERS];
	     
	   for (int i = 0; i < NUM_LETTERS; i++)
	    {
		if((letters[i]>=97) &&(letters[i]<=122)) // checking if character is lowercase.
	         letters[i] - 32; // convert to uppercase.
			
	      cout << "This is the changed letters. " << letters[i] << endl;
			}    
	    return 0;
	    }
cin >> letters [NUM_LETTERS];

You're writing a single character and it's going outside the array, hence the stack corruption. You meant to say this:

cin >> letters;

Also, you should use std::islower() (found in <cctype>) to test for lower case and std::toupper() (found in the same standard header) to change them.

And this

letters[i] - 32;

doesn't even do anything. You surely meant letters[i] -= 32;, but still, use std::toupper().
Thanks for the reply.
I would use the code you suggest however we haven't been shown the easy way to do stuff yet. I suppose it's to develop our troubleshooting skills as well as teach an alternate coding method. It sure does get frustrating at times.
I made the changes and added a pause and it works, kind of. It shows blank statements if you only put in a few letters filled with garbage. How would I add a statement to let the user know they entered more than 10 letters or unusable data (numbers or special characters) to keep the program from showing a stack error like it does? or is it feasible? better a dialogue box than an error message is how I'm thinking (doesn't mean I'm correct tho).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
using namespace std;

	int main ()
	{
	    const int NUM_LETTERS = 10;
	    char letters [NUM_LETTERS];
	       
	    cout << "Please enter up to 10 letters." << endl;
	    cin >> letters;
	     
	   for (int i = 0; i < NUM_LETTERS; i++)
	    {
		if((letters[i]>=97) &&(letters[i]<=122)) // checking if character is lowercase.
	         letters[i] -= 32;					 // convert to uppercase.
			
	      cout << "These are the changed letters. " << letters[i] << endl;
		  if (letters[i] < 97 || > 122)
			}  
	   system ("pause");
	    return 0;
	    }
I'm ending this thread with the corrected version. Thanks to all that made it work.

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

/*
Kelly Handley
Chap7_1
October 20, 2010
*/
/*
This program will ask the user to enter 10 numbers then display the largest and
smallest.
*/

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

const int input = 10;
double numbers[input];
double Max, Min;

int main()
{
			//ask user for input
		  for (int i = 0; i < input; i++)
   {
      cout << "Enter ten numbers " << (i + 1) << endl;
      cin >> numbers[i];
   }
			//calculate maximum value
		  		   Max = numbers[0];
   for (int i= 0; i< input; i++)
   {

	   if (numbers[i] > Max)
	   {
		   Max = numbers[i];
	   }
   }
			//calculate minimum value
	   Min = numbers[0];
   for (int i= 0; i< input; i++)
   {
	   if (numbers[i] < Min)
	   {
		   Min = numbers[i];
	   }
   }
				//Display results
   cout << "The largest number is: \t\t" << Max << endl;
   cout << "The smallest number is: \t" << Min << endl;
			//pause program to see results
system("pause");

	return 0;
}
Topic archived. No new replies allowed.