Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Stack around the variable 'equation' was corrupted.

stack equation

  • Please log in to reply
1 reply to this topic

#1 Airplaneman19

Airplaneman19

    CC Lurker

  • Just Joined
  • Pip
  • 7 posts

Posted 16 August 2011 - 09:44 AM

Hi, I'm making a calculator in Visual C++ 2010 (Console, not GUI) and I wrote all the code, but whenever it prints the answer I get some weird number which I assume is from another place in my memory, but I don't know how it gets there. Whenever I exit it, the Debuggger says "Stack around the variable 'equation' was corrupted." and I'm not sure why. Here's my code (so far it only supports 2 numbers and one symbol, no spaces (ex. 2+5)):
#include <iostream>
#include <cstdlib>   //needed for system("PAUSE");
#include <ctype.h> //needed for the tolower() function

int main(){
	
	char equation[3];
	int result;
	bool wantsToContinue = true;
	char yesOrNo;

    equationPrompt:
	std::cout << "Enter Equation: " << std::endl;
	std::cin >> equation;

	while(wantsToContinue){

		switch(equation[1]){
		case '+':
			result = int(equation[0]) + int(equation[2]);
			break;
		case '-':
			result = int(equation[0]) + int(equation[2]);
			break;
		case '*':
			result = int(equation[0]) + int(equation[2]);
			break;
		case '/':
			result = int(equation[0]) + int(equation[2]);
			break;
		}
	
	std::cout << std::endl << "Your answer is " << result << std::endl;
    exitPrompt:
	std::cout << "Exit? Y/N: ";
	std::cin >> yesOrNo;

	if(tolower(yesOrNo) == 'n'){
		wantsToContinue = true;
		goto equationPrompt;
	}
	else if (tolower(yesOrNo) == 'y')
		wantsToContinue = false;
	else{
		std::cout << std::endl << "Unknown response." << std::endl;
		goto exitPrompt;
	}
	}
	std::cout << result << std::endl;
	system("PAUSE");
	return 0;
}

Edited by Airplaneman19, 16 August 2011 - 01:13 PM.

  • 0

#2 Airplaneman19

Airplaneman19

    CC Lurker

  • Just Joined
  • Pip
  • 7 posts

Posted 16 August 2011 - 03:38 PM

Nevermind, I fixed it.
  • 0





Also tagged with one or more of these keywords: stack, equation

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download