CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2016
    Posts
    29

    Hello. I need some help with my code.

    On this code when i run it i get the error saying that Run-Time Check Failure #2 - Stack around the variable 'heightwatermelonarray' was corrupted. It is also saying that around 'secondarray'. I have tried different things, but i am lost on how to fix this. I have tried searching this error and it tells me some things but i am not understanding them to much. Any help will be great. Thanks in advance.


    Code:
    #include <iostream>
    using namespace std;
    
    
    
    
    int main()
    {
    	//Variables
    	int TIME;
    	int	HeightofBridge;
    	int count;
    	int const seconds = 0;
    	float HeightofWatermelon;
    	float Distancefall;
    	const float gravity = 9.8;
    
    
    
    	cout << "Enter the time the watermelon falls: "; // User is asked to enter the time the watermelon fell in seconds.
    	cin >> TIME;  //User enters time in seconds.
    
    	cout << "nter the height of the bridge: "; //User is asked to enter the height of the bridge in meters.
    	cin >> HeightofBridge; //User enters height of the bridge in meters.
    
    
    	int secondsarray[seconds + 1];
    	float heightwatermelonarray[seconds + 1];
    	for (int count = 0; count < TIME; count++)
    	{
    		HeightofWatermelon = 0.5 * gravity * count * count;
    		secondsarray[count] = count;
    		heightwatermelonarray[count] = HeightofWatermelon;
    	}
    
    
    	cout << "Time Falling (seconds) Distance Falling (meters)\n";//Output title.
    	cout << "-------------------------------------------------\n";
    
    
    	Distancefall = 0.5 * gravity * TIME * TIME; //Calculation of the distnace of the fall.
    
    
    	for (count = 0; count < TIME; count++)
    	{
    		if (Distancefall > HeightofBridge)
    		{
    			cout << "Warning - Bad Data: The distance fallen exceeds the " //If distnace of the fall is greater then the height of bridge then output will be a warning.
    				<< "height of the bridge" << endl;
    			break;
    		}
    		else
    			cout << secondsarray[count] << "\t"
    			<< heightwatermelonarray[count] << endl;
    	}
    
    
    	return 0;
    }

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Hello. I need some help with my code.

    Code:
    int const seconds = 0;
    ...
    int secondsarray[seconds + 1];
    ...
    for (int count = 0; count < TIME; count++)
    ..
        secondsarray[count] = count;
    seconds is a constant and is set to 0. Hence the array secondsarray has a size of 1 element accessed as secondsarray[0]. Within the for loop, count varies from 0 to 1 less than TIME - hence when count is greater than 0 the arrays are being accessed outside of their size and hence the error. The size of the arrays needs to be set at compile time to be the largest required. Or better, use a container such as a vector to store the data which can be added to at run-time without knowing in advance its maximum size. See http://www.cplusplus.com/reference/vector/vector/
    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)

  3. #3
    Join Date
    Jun 2016
    Posts
    29

    Re: Hello. I need some help with my code.

    Okay thank you for the response. So let me ask this when i change the size of my array am i taking the +1 out that I have or do i need to change the count?

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Hello. I need some help with my code.

    Yes, you take the + 1 out. The valid entered value of TIME should be no greater than the value of seconds. So initialise seconds to the required max value and after TIME has been entered check its value is valid.
    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)

  5. #5
    Join Date
    Jun 2016
    Posts
    29

    Re: Hello. I need some help with my code.

    okay so i took that out and my int const seconds i changed to 1. I am still getting the error. I know this is probably something simple but i am very new at this and i just want to make sure i am doing things correctly.

  6. #6
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Hello. I need some help with my code.

    If you have changed seconds to 1, then the max value for TIME is 1. What value for TIME are you entering?
    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)

  7. #7
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Hello. I need some help with my code.

    Consider
    Code:
    ...
    int const seconds = 100;
    const float gravity = 9.8;
    
    int main()
    {
    	//Variables
    	int TIME;
    	int	HeightofBridge;
    	int count;
    	float HeightofWatermelon;
    	float Distancefall;
    
    
    	cout << "Enter the time the watermelon falls: "; // User is asked to enter the time the watermelon fell in seconds.
    	cin >> TIME;  //User enters time in seconds.
    
    	if (TIME < 1 || TIME > seconds) {
    		cout << "Invalid value for TIME. Must be greater than zero and no greater than " << seconds << endl;
    		return 1;
    	}
    
    	cout << "Enter the height of the bridge: "; //User is asked to enter the height of the bridge in meters.
    	cin >> HeightofBridge; //User enters height of the bridge in meters.
    
    
    	int secondsarray[seconds];
    	float heightwatermelonarray[seconds];
    ...
    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)

  8. #8
    Join Date
    Jun 2016
    Posts
    29

    Re: Hello. I need some help with my code.

    Thank you for your help. I finally got it to work. All i did was change int const second to 100 instead of just 1.

  9. #9
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Hello. I need some help with my code.

    Yes, but you should include the range check for TIME. If you enter a value higher than 100 for TIME you're going to have similar problems.
    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)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured