loop will not stop!!!

D

D.j.

#include <stdio.h>
#include <stdlib.h>
#include <time.h> /* contains prototype for function time */

/* function main beins program execution */
void menu(void);
void add(void);
int main()
{
char choice;
srand( time( NULL ) ); /* randomize number generator using current time */

menu();

while ( choice != 'X' )
{printf( "Your Choice " );
scanf( "%c", &choice );
switch (choice)
{
case 'A':

add();
break;
}
}

return 0;
}

void menu(void)
{
printf( "Addition===========>A\n" );
printf( "Subtractin=========>S\n" );
printf( "Multiplication=======>M\n" );
printf( "Division===========>D\n" );
printf( "Remainder=========>R\n" );
printf( "Exit==============>X\n" );

return;
}

void add(void)
{
int num1;
int num2;
int answer;
int ranswer;
int counter = 1;

num1 = 1 + ( rand() % 999);
num2 = 1 + ( rand() % 999);
answer = num1 + num2;
ranswer = num1 + num2;
printf( "%d + %d ", num1,num2 );
scanf( "%d%d", num1,num2 );

while ( answer != ranswer )
{
printf( "Incorrect, try again." );

printf( "%d + %d == ", num1,num2 );
scanf( "%d", &answer );
if ( counter ==3 )
printf( "%d + %d = %d" );
}



return;
}
 
K

Kevin Goodsell

D.j. said:
int main()
{
char choice;

choice is declared, but not initialized.
srand( time( NULL ) ); /* randomize number generator using current time */

menu();

while ( choice != 'X' )

Evaluating an initialized variable gives undefined behavior.

That is sufficient to explain the program's behavior (undefined behavior
means anything can happen - including an infinite loop), but you seem to
have another problem. Even if you did give 'choice' a value, you never
*change* it's value, therefore you can never terminate the loop (or, if
the initial value you give it is 'X', you can never enter the loop).

Also, in the future please use sane indenting when you post code here.
If people can't read your code, they're likely to just skip your post,
and your questions won't get answered.

-Kevin
 
V

Vijay Kumar R Zanvar

#include <ctype.h>

[..]
int main()
{
char choice = 0;
srand( time( NULL ) ); /* randomize number generator using current time */

menu();

while ( choice != 'X' )

while ( toupper ( choice ) != 'X' )
{printf( "Your Choice " );
scanf( "%c", &choice );
switch (choice)
{
case 'A':

add();
break;
}
}

return 0;
}
[..]
 
V

Vijay Kumar R Zanvar

[..]
Evaluating an initialized variable gives undefined behavior.

Probably you meant evaluating an _uninitialized_ variable gives
undefined behaviour.

[..]
 
N

nobody

Vijay Kumar R Zanvar said:
#include <ctype.h>

[..]
int main()
{
char choice = 0;
srand( time( NULL ) ); /* randomize number generator using current time */

menu();

while ( choice != 'X' )

while ( toupper ( choice ) != 'X' )
Read Kevin's post about undefined behaviour. Last time I've
checked, N869 didn't state that calling toupper() will "convert"
it into defined (behaviour). Maybe on *your* platform undefined
behaviour produces expected results, but I wouldn't offer it
as a "solution".
[snip]
 
M

Mike Wahler

D.j. said:
so how would I initialize choice and would that help?



Choose an initial value and use initialization syntax.

char choice = 0; /* initialize with zero */

char choice = 42; /* initialize with 42 */

/* etc */

This is such a fundamental issue, I'm prompted to ask
which C book(s) are you learning from? If you don't
have any, get one (better yet, two or three). See
www.accu.org for reviews and recommendations.

BTW please don't top post.

-Mike
 
C

CBFalconer

D.j. said:
so how would I initialize choice and would that help?

Don't toppost. Do indent. Do snip. Anything after a sig marker
"-- " is normally to be discarded in quoting.
 
K

Kevin Goodsell

Kevin said:
Even if you did give 'choice' a value, you never
*change* it's value, therefore you can never terminate the loop (or, if
the initial value you give it is 'X', you can never enter the loop).

Woops, I somehow missed the scanf call. You do need an initial value in
any case, though.

While the behavior is undefined, and thus a loop that never terminates
is a wholly acceptable result, I doubt that realistically the endless
loop is a result of the uninitialized variable. I think the additional
problem is that you don't handle unexpected characters in the input,
something like what is described in this FAQ entry:

http://www.eskimo.com/~scs/C-faq/q12.19.html

-Kevin
 
C

Christopher Benson-Manica

^^^^

Where did you get this? choice isn't initialized in the original
post... It's misleading to change quoted text without making a note.
 
M

Mark McIntyre

so how would I initialize choice

Buy a C book. Read it. Initialising variables is such a basic concept
that without it, you are hopelessly lost
and would that help?

it would certainly help. Otherwise it has a random value, say '2', and
naturally your while condition cannot be met...
 
B

Barry Schwarz

#include <stdio.h>
#include <stdlib.h>
#include <time.h> /* contains prototype for function time */

/* function main beins program execution */
void menu(void);
void add(void);
int main()
{
char choice;
srand( time( NULL ) ); /* randomize number generator using current time */

menu();

while ( choice != 'X' )
{printf( "Your Choice " );
scanf( "%c", &choice );

In addition to all the other advice you've received, remember that
scanf will leave any additional characters (including the '\n' from
the ENTER key) in the stream. On the next iteration through the loop,
it will obtain the first residual character which is probably not what
you want it to get.
switch (choice)
{
case 'A':

add();
break;
}
}

return 0;
}

void menu(void)
{
printf( "Addition===========>A\n" );
printf( "Subtractin=========>S\n" );
printf( "Multiplication=======>M\n" );
printf( "Division===========>D\n" );
printf( "Remainder=========>R\n" );
printf( "Exit==============>X\n" );

return;
}

void add(void)
{
int num1;
int num2;
int answer;
int ranswer;
int counter = 1;

num1 = 1 + ( rand() % 999);
num2 = 1 + ( rand() % 999);
answer = num1 + num2;
ranswer = num1 + num2;
printf( "%d + %d ", num1,num2 );
scanf( "%d%d", num1,num2 );

while ( answer != ranswer )
{
printf( "Incorrect, try again." );

printf( "%d + %d == ", num1,num2 );
scanf( "%d", &answer );
if ( counter ==3 )
printf( "%d + %d = %d" );
}



return;
}



<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top