0

I am fairly new to C and programming and I keep on getting stuck. I'm practising by having one program and just adding more complexity to it.

The program is fairly simple, when compared to some of the other questions asked here. All I want to do is to input a number and then say if it is less than or greater than five. I've recently added a menu and a Do While loop. This is the code below.

#include <stdio.h>

void main ()
{
    int ANumber;
    bool Determine = 1; 
    int MenuChoice; 

    printf("1) Enter a number." "\n2) Exit.\n");
    printf("\nPlease choose an option from the menu above - ");

    scanf("%d", &MenuChoice);       

    if (1 == MenuChoice) {  

        do {
            printf("\nPlease enter a number that is between 0 and 10 - ");
            scanf("%d", &ANumber);          

            if (ANumber == 5)   
                printf("The number you entered is 5.\n");           

            if (ANumber >= 6)
                printf("The number you entered is larger than 5.\n");

            if (ANumber <= 4)
                printf("The number you entered is smaller than 5.\n");

            getchar();
            printf("Would you like to continue? 1 = Yes OR 0 = No - ");     
            scanf("%d", &Determine);    
            return;

        } while (true == Determine);

        if (false == Determine) {   
            return;
        }

    }

    if (2 == MenuChoice)
        return;
}

The main problem is that the majority of the code works fine.
The problem occurs when I want to exit the loop :

while (true == Determine);

if (false == Determine) {   
    return;
}

When I enter 0 into the program this error comes up :

Run-Time Check Failure #2 - Stack around the variable 'Determine' was corrupted.

May I please have some help indicating what is wrong and what this error message means?

Thanks

TheCodeArtist
  • 21,479
  • 4
  • 69
  • 130
Reemahs
  • 5
  • 2
  • 5

3 Answers3

1

scanf() cannot read boolean data type. There is no proper format specifier for reading a bool by using scanf().
Note that while using scanf() if there is a mismatch between the format specifier and actual data type then it results in Undefined Behavior.

You will have to use an int.

change:

bool Determine = 1; 

to

int Determine = 1; 
Alok Save
  • 202,538
  • 53
  • 430
  • 533
0

Ditch the yoda conditionals and just roll with

while(Determine)

this will work exactly like you written because C doesn't have booleans, they are ints. false is 0 and true is nonzero.

while, if, for and all of those will fire if the argument inside is GREATER THAN zero (non-false).

Also, try to use printf's between blocks of code so that you know where you got stuck. I think you'll notice the bug (well one of em) yourself if you format the code a bit better ;)

Read this for more info Using boolean values in C

EDIT: oh well.

bool Determine = 1; //not even true or false here you're using it as an int

then later

scanf("%d", &determine);

bool is supposed to be just one byte, integers are four bytes; you jam 4 bytes into a 1byte region, bam stack gets corrupted.

int Determine = 1;

You need to take care of data sizes, play with the sizeof() to get a feel how big things are. Things can't automagically cast themselves to other things even though this is C :)

Community
  • 1
  • 1
Shark
  • 6,513
  • 3
  • 28
  • 50
-1

0 is not equals to false and true does not mean 1.

Change the data type of determine into int from bool

check the Determine as Determine == 0 or 1.

tausun
  • 2,154
  • 2
  • 24
  • 36
  • Who downvoted the answer, can you please let me know the reason...it is important for me to know if my knowledge is wrong. – tausun Sep 07 '12 at 03:10