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);
}

Your problem looks like this line: scanf("%s/n", &ans); , since ans is a char. But you are reading it as a string. It is not. You perhaps want to do this scanf("%c",ans); .

The reason you get a crash, is that your text entry on the command line, is always at least two characters long. The first is the answer (y,n or whatever) and the second is the null character (zero) that is added to the end of the string to indicate it has finished. But you only have memory allocated for one character. So something else gets over written.

Your problem looks like this line: scanf("%s/n", &ans); , since ans is a char. But you are reading it as a string. It is not. You perhaps want to do this scanf("%c",ans); .

The reason you get a crash, is that your text entry on the command line, is always at least two characters long. The first is the answer (y,n or whatever) and the second is the null character (zero) that is added to the end of the string to indicate it has finished. But you only have memory allocated for one character. So something else gets over written.

Thanks now it won't print the same response again, but it omits the question
Would you like to continue(y/n)?
It automatically goes back to asking for width and height. Any idea why it is doing that?

This is how it looks now, except it reprints the last width and height and then asks for a new one. This is the output of the code below, as you can see it kinda loops through the previous values then it clears the memory for new ones.

-----Fence-o-Matic-----

This program will draw and compute the area of a fence that you specify.

Please enter a width: 2
Please enter a height: 2
+--+
| |
| |
+--+
Area = 4 Perimeter = 8
Would you like to continue(y/n): y

Please enter a width: Please enter a height: +--+
| |
| |
+--+
Area = 4 Perimeter = 8
Would you like to continue(y/n):
Please enter a width:

// 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 =='n'))
    {      //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){     //Make sure it is not negative
        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){     //Make sure it is not Negative
        printf("this is an invalid number. please enter a positive height\n");
        scanf("%d", &height);
    }
    
    printf("+");
    for(i=1; i<=width; i++)     //Builds 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++)     //Prints the Area.
    {
        printf("-");
    }
    printf("+\n");
    printf("Area = %d", area(width,height));     //Returns the Value for Area
    printf("\tPerimeter = %d\n", perimeter(width,height));     //Returns the Value for Perimeter
    printf("Would you like to continue(y/n):\t");     //Asks the users to continue or stop.
        scanf("%c/n", &ans);     //Scans user input for ans
    if (ans =='n')
        printf("\nThank you for using Fence-o-matic, GoodBye!\n");
    }     //End of While
    //return 0;
    system("pause");
}

int perimeter(int x, int y)
{
    return(x*2+y*2);
}
int area(int x, int y)
{
    return(x*y);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.