473,385 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

hi can any1 help me

hi im creating a program in visual studios.net 2003 to input 40 temperature readings and stop when the number "-999" is entered. however I keep getting this error when i enter -999: Run-Time Check Failure #2 - Stack around the variable 'temp_readings' was corrupted.

could any1 please tell me whats wrong?

heres my code so far:

#include <stdio.h>

int main(void)
{
//initialise variables
int temp_readings[41];
int count = 0;
int t = 0;
int i, g;
int h=1;

//set all variables in array to 0
for(g=0; g<=41; g++)
{
temp_readings[g] = 0;
}

//while the number entered is not -999 take it and put it into the array
while(t != -999)
{
scanf("%d", &t);
if(t != -999)
{
temp_readings[count]= t;
count++;
}
}

//print all numbers that are above 30
for(i=0; i<41; i++)
{
if (temp_readings[i] >=30)
h++;

}
}
Nov 23 '06 #1
2 1164
horace1
1,510 Expert 1GB
temp_readings is defined as
Expand|Select|Wrap|Line Numbers
  1. int temp_readings[41];
  2.  
which has element index values from temp_readings[0] to temp_readings[40]

the following loop indexes from 0 to temp_readings[41] and is corrupting memory locations after temp_readings[40]
Expand|Select|Wrap|Line Numbers
  1. for(g=0; g<=41; g++)
  2. {
  3. temp_readings[g] = 0;
  4. }
  5.  
try changing the for to and it should (may) fix the problem
Expand|Select|Wrap|Line Numbers
  1. for(g=0; g<41; g++)
  2.  
programs with errors like this can apparently run OK for years and then crash when the program is modified or a different data set or a compiler is used.
Nov 23 '06 #2
You can also try modifyimg the following linesin your code
while(t != -999)
{
scanf("%d", &t);
if(t != -999)
{
temp_readings[count]= t;
count++;
}

as

scanf("%d", &t);
while(t != -999)
{
temp_readings[count]= t;
count++;
scanf("%d", &t);
}
Nov 23 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Martin Nadoll | last post by:
Hello i have a form, where my users can choose a postal code and a city to see a table result page with only entries of their choice. this results have to be shown in a new window : var...
2
by: billiejoex | last post by:
Hi. I'm trying to use pcapy module on Windows XP prof sp2 but it doesn't work. The example source reported on the site (http://oss.coresecurity.com/impacket/sniff.py) works only on *unix...
15
by: Peter Olcott | last post by:
Does anyone know how to do this?
6
by: boogeracky | last post by:
does anybody play any musical instruments here? im a drummer, have been playing for 11 years now, and loving it...
6
by: abctech | last post by:
Hi all, I am a fresher in Java/J2EE, Can anyone recommend some books/sites for J2EE-beginners which will help clear the concepts and fundamentals and also on J2EE-mini-projects/source-codes...
8
by: toffee | last post by:
Hi all, which php script(s) would you guys recommend to create a site similar to myspace ? preferably any free open source script but i guess commercial is fine too kind regards
0
by: Saurov | last post by:
To execute/open a file(say a.XML) in IE I use System.Diagonistic.Process.Open("explorer.exe",file). But the user can use any browser like opera, konquerror, mozilla, netscape etc. So I wanna to...
1
by: pavankumar106 | last post by:
can any1 tell me how to access label values of datalist in javascript?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.