My code generate a Hollow Square with user given width and Height but when the user is prompted to continue or stop it generates a Run-Time Check Failure #2 - Stack around the variable 'ans' was corrupted.
Here is my code, any help much appreciated.
// SadaMiguel_RectangleFinal.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
int area(int,int); //prototype for area
int perimeter(int,int); //prototype for perimeter
int main()
{
int width;
int height;
int i;
int j;
char ans='y';
printf("-----Fence-o-Matic-----\n");
printf("\nThis program will draw and compute the area of a fence that you specify.\n");
while(ans =='y'){ //start of outside while loop
printf("\nPlease enter a width:\t"); //asking for width
scanf("%d", &width); //scanning in the width value
while(width<0){ //while loop to make sure width value is not negitive
printf("this is an invalid number. please enter a positive width\n");
scanf("%d", &width);
}
printf("Please enter a height:\t"); //asking for height
scanf("%d", &height); //scanning in height value
while(height<0){ //while loop to make sure width value is not negitive
printf("this is an invalid number. please enter a positive height\n");
scanf("%d", &height);
}
printf("+");
for(i=1; i<=width; i++) //start building the area
{
printf("-");
}
printf("+\n");
i=1;
for(j=1; j<=height; j++)
{
printf("|");
for(i=1; i<=width; i++)
{
printf(" ");
}
printf("|\n");
}
printf("+");
for(i=1; i<=width; i++) //Printing the area after this.
{
printf("-");
}
printf("+\n");
printf("Area = %d", area(width,height)); //Will print the area
printf("\tPerimeter = %d\n", perimeter(width,height)); //Will print the perimeter
printf("Would you like to continue(y/n):\t"); //Asks the users to continue or stop.
scanf("%s/n", &ans); //scanning in ans
if (ans =='n')
printf("\nThank you for using Fence-o-matic, GoodBye!\n");
} //end of outside while loop
return 0;
//system("pause");
}
int perimeter(int x, int y)
{
return(x*2+y*2);
}
int area(int x, int y)
{
return(x*y);
}