Jump to content

Stack around the variable 'equation' was corrupted.

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Airplaneman19

Airplaneman19

    Newbie

  • Members
  • Pip
  • 7 posts
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.


#2
Airplaneman19

Airplaneman19

    Newbie

  • Members
  • Pip
  • 7 posts
Nevermind, I fixed it.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users