What went wrong?

Please help me fix this.

The output should be like this


Welcome to deal or no deal.
Cases are 1 - 14
Enter case number: 1

Banker's offer is 10000
D = Deal N = No Deal
Deal or no deal? d

The case contains 3000
You win


When I press D (Deal) I get the right response. But when I press N (no deal) I get random or not the right response. Also I tried putting last else

else
{
printf ("Invalid choice");
}

So that if I press other letters it will say invalid choice, but it's not working. Please help me fix this. Thanks.


The code:

#include <stdio.h>
#include <iostream>
#include <time.h>
#include <conio.h>

int a,b;

main()
{
short unsigned int num = 0;
short unsigned int num2 = 0;
char test;
srand(time(NULL));

loot:

num = 1 + rand() % (14 - 1 + 1);
num2 = 1 + rand() % (14 - 1 + 1);


printf ("Welcome to deal or no deal.\nCases are 1 - 14");
printf ("\nEnter case number: ");
scanf ("%d", &a);
a=num*1000;
printf ("\nBanker's offer is %d", a);
printf ("\nD = Deal N = No Deal\nDeal or no deal? ");
scanf ("%",&test);
b=num2*1000;
printf ("\nThe case contains %d",b);
switch (test)

{
case 'd': case 'D': break;
case 'n': case 'N': break;
case 'c': case 'C': return (0); break;

}

if ('d' || 'D')

if (a > b)
{
printf ("\nYou win");
}
else if (a < b)
{
printf ("\nYou lost");
}
else
{
printf ("\nBanker's offer and case is just the same");
}

else if ('n' || 'N')

if (a < b)
{
printf ("\nYou win");
}
else if (a > b)
{
printf ("\nYou lost");
}
else
{
printf ("\nBanker's offer and case is just the same");
}

getch ();
}
scanf ("%",&test);

You are missing the letter after the %.
1st
Your case-statements do not make sense. You forget the

test == 'D' to assign a new value to test.

2md
If ('d' || 'D')

does not make sense.
You need to compare your variable "test" with the letters:

--> f (Test =='d')

int main

[QUOTE]
scanf ("%",&test);


You are missing the letter after the %.[/QUOTE]
Whenever I put the ("%c",&test); it won't let me type.


1st
Your case-statements do not make sense. You forget the

test == 'D' to assign a new value to test.

2md
If ('d' || 'D')

does not make sense.
You need to compare your variable "test" with the letters:

--> f (Test =='d')

int main


Can you show me?
I tried to put

if (test == 'd' || test 'D')

but the program was made worst. Please show me already. I need to run this program badly.
Will somebody please help me fix this already? PLEASE!
Can you show me?
I tried to put

if (test == 'd' || test 'D')"

Should be if(test == 'd' || test == 'D')


Also, you could replace your line with scanf() with:

1
2
  test = (char) fgetc(stdin);
  fflush(stdin);


Also remember this is a forum, not quite an active conversation, and your peers are volunteering information. Have patience, or eventually nobody will want to respond.
Last edited on

#include <stdio.h>
#include <iostream>
#include <time.h>
#include <conio.h>

int a,b;

main()
{
short unsigned int num = 0;
short unsigned int num2 = 0;
char test;
srand(time(NULL));

num = 1 + rand() % (14 - 1 + 1);
num2 = 1 + rand() % (14 - 1 + 1);

printf ("Welcome to deal or no deal.\nCases are 1 - 14");
printf ("\nEnter case number: ");
scanf ("%d", &a);
a=num*1000;
printf ("\nBanker's offer is %d", a);
printf ("\nD = Deal N = No Deal\nDeal or no deal? ");
scanf ("%c",&test);
b=num2*1000;
printf ("\nThe case contains %d",b);
switch (test)

{
case 'd': case 'D': break;
case 'n': case 'N': break;
case 'c': case 'C': return (0); break;
}

if (test == 'd' || test == 'D')
{
if (a > b)
{
printf ("\nYou win");
}
else if (a < b)
{
printf ("\nYou lost");
}
else
{
printf ("\nBanker's offer and case is just the same");
}
}
else if (test == 'n' || test == 'N')
{
if (a < b)
{
printf ("\nYou win");
}
else if (a > b)
{
printf ("\nYou lost");
}
else
{
printf ("\nBanker's offer and case is just the same");
}
}
getch ();
}


That is my latest code. See how it works for yourself. Well, at least try it before you tell me what is wrong.

And I can't use other code aside from scanf printf switch case and if else. That is what my professor said. And now I can't even pull it off.

Please try it before you tell me what is wrong. Thanks.
because the user enters a newline (hits enter) just before your scanf goes off, the scanf is picking up the newline character rather than the character you type in. Add a second character to the scanf, pull in the first one as trash, and then pull in the second one as test, it should work after that.
I think what jpeg is trying to say is that when you first use scanf(), it reads the number from stdin, but the newline character is still there.

Anyway, that is what the problem is. What you should do is put

fflush(stdin);

right after each scanf(). Reading extra %c's within the scanf() would be less clean.
Topic archived. No new replies allowed.