?
Solved

Run-time Check Failure #2

Posted on 2009-05-03
4
Medium Priority
?
263 Views
Last Modified: 2012-05-06
I'm new to C++ and my program works, but I keep getting the following error:

Run-time check failure #2 - stack around the variable "ranNumArray" was corrupted.

I'm not sure why this is happening, can some please help!  Thanks a lot.
int main (void)
{
 
int ranNumber;
int ranNumArray[100];
int i;
 
int numFind;
int arrayPos;
int foundNum = 0;
 
  /* initialize random seed: */
  srand (1);
 
  /* generate secret number: */
for(i=0; i< 101; i++) {
  ranNumber = rand() % 100 + 1;
  ranNumArray[i] = ranNumber;
  cout<<i<<" "<<ranNumArray[i]<<endl;	
}
 
     int max = ranNumArray[0]; 
 
     for(int i = 1; i<101; i++)
     {
          if(ranNumArray[i] > max)
                max = ranNumArray[i];
     }
 
 cout<<"The Max is "<<max<<endl;
 
 cout<<"Please enter a number to find:";
 cin>>numFind;   
 
     for(int x = 0; x<101; x++)
     {
          if(ranNumArray[x] == numFind) {
          		cout<<"The position of your number is "<<x<<endl;
                foundNum = 1;
          }
 
     }
  
  if(foundNum == 0){
  	cout<<"Not Found";
    }
     
return 0; 
}

Open in new window

0
Comment
Question by:trifecta2k
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 2000 total points
ID: 24292547
Your problems are the for loops, looping 1 too many. It goes past the end of the array. Array of 100 means 0 - 99 in C++
for(i=0; i< 101; i++)  // 101 slots, one too many
 
for(int x = 0; x<101; x++) // 101 slots, one too many
 
 
Instead use
//
for(i=0; i< 100; i++)  // 100 slots
 
for(int x=0; x< 100; x++)  // 100 slots

Open in new window

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24294096
Alternatively to that mrjoltcola said, you could do


     for(int i = 1; i<=100; i++)
     {
          if(ranNumArray[i-1] > max)
          ...

That way your loop counter is 1-based what might give some benefits if using it as an ID-number.

But actually this is more error-prone than that mrjoltcola suggested.

>>>>      for(int i = 1; i<101; i++)

Never use 101 as upper boundary if the size is 100. That is very error-prone even if using a 1-based counter.

Generally, if using fixed-sized arrays you might consider the following:

 for(int i = 0; i < sizeof(ranNumArray)/sizeof(ranNumArray[0]); i++)

With that you could change the size and type of array without having to change the for loop as well.

0
 

Expert Comment

by:emilmgeorge
ID: 24295905
int main (void)
{
 
int ranNumber;
int ranNumArray[100];
int i;
 
int numFind;
int arrayPos;
int foundNum = 0;
 
  /* initialize random seed: */
  srand (1);
 
  /* generate secret number: */
for(i=0; i< 100; i++) {
  ranNumber = rand() % 100 + 1;
  ranNumArray[i] = ranNumber;
  cout<<i<<" "<<ranNumArray[i]<<endl;  
}
 
     int max = ranNumArray[0];
 
     for(int i = 1; i<100; i++)
     {
          if(ranNumArray[i] > max)
                max = ranNumArray[i];
     }
 
 cout<<"The Max is "<<max<<endl;
 
 cout<<"Please enter a number to find:";
 cin>>numFind;  
 
     for(int x = 0; x<100; x++)
     {
          if(ranNumArray[x] == numFind) {
                        cout<<"The position of your number is "<<x<<endl;
                foundNum = 1;
          }
 
     }
 
  if(foundNum == 0){
        cout<<"Not Found";
    }
     
return 0;
}
0
 
LVL 5

Author Closing Comment

by:trifecta2k
ID: 31577439
Thanks, that worked perfectly.  
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question