C question console application windows

Joeado123

Well-Known Member
OP
Member
Joined
Mar 25, 2017
Messages
105
Trophies
0
Age
33
XP
473
Country
Canada
Hi, I am doing a plus or minus game (I'm actually learning how to code in c) and I had a problem. When I was typing letter instead of numbers, (The menu is all right it's covered with a switch) I was trying to guess the number I got "It's Bigger!" (this is what I wrote it's normal) but written a thousand times surely. Why is it doing this and how can I write letters without the need to close the program?
 

Joeado123

Well-Known Member
OP
Member
Joined
Mar 25, 2017
Messages
105
Trophies
0
Age
33
XP
473
Country
Canada
The case 1 of my switch (might help to know what's missing)


switch(Menu)
{
case 1:
printf("\nPlus or Minus level 1: between 1 and 100");
int MAX = 100, MIN = 0;
srand(time(NULL));
number1 = (rand() % (MAX - MIN + 1)+ MIN);
printf("\nType a number between 1 and 100:");
do
{
scanf("%d", &number2);


if (number1 < number2)
{
printf("It's less!");
meter++;
}

else if (number1 > number2)
{
printf ("It's more!");
meter++;
}

else
{
printf("You found the mystery number in %d times", );
meter = 0;
printf("\nDo you want to do another game?\nIf yes type 1, or 0 to quit: ");
scanf("%d", &Replay);
}
}while(number1 != number2);

break;
 

GerbilSoft

Well-Known Member
Member
Joined
Mar 8, 2012
Messages
2,395
Trophies
2
Age
34
XP
4,249
Country
United States
If you enter a letter, it fails the "%d" specifier, so scanf() returns 0 and doesn't set number2 to anything. You might want to try this instead:

Code:
char buf[128];
fgets(buf, sizeof(buf), stdin);
number2 = (int)strtol(buf, NULL, 10);
This uses fgets() to get a string from stdin (up to 127 characters plus NULL terminator), and then uses strtol() to convert it to a number.

Note that strtol() will return 0 if it's not a valid number.
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • Xdqwerty @ Xdqwerty:
    Wut
  • wolffangalchemist @ wolffangalchemist:
    I love the thrift stores around me I just got a switch power supply, a new power supply, a xbox duke controller, and a raspberry pi 3b all for $6.
    +2
  • BigOnYa @ BigOnYa:
    hella deal there
  • BigOnYa @ BigOnYa:
    each one of those worth atleast $6 each really
  • wolffangalchemist @ wolffangalchemist:
    I have been looking for a duke that was in decent shape for a while. I have huge hands so I like it better than the s controller.
    +2
  • BakerMan @ BakerMan:
    wow, what the hell did I miss? (my phone was low and company was over, so i couldn't charge it)
  • SylverReZ @ SylverReZ:
    @BakerMan, You missed not a lot.
  • BigOnYa @ BigOnYa:
    It was triple XP day, but you missed it, it's over now.
  • BigOnYa @ BigOnYa:
    Xdqwerty installed a few new games, then got bored of them after 5 min, that's about it.
    +1
  • K3Nv2 @ K3Nv2:
    Then his brother broke his ds
  • Xdqwerty @ Xdqwerty:
    no, he broke it way before i installed the games
  • Xdqwerty @ Xdqwerty:
    also im gonna finally install the steam client
  • BigOnYa @ BigOnYa:
    Then uninstall it five minutes later, jk
    +1
  • Xdqwerty @ Xdqwerty:
    ok steam clint still works for some reason
  • Xdqwerty @ Xdqwerty:
    client*
  • Xdqwerty @ Xdqwerty:
    my steam friend id is 1625764420
  • Xdqwerty @ Xdqwerty:
    good night
  • BigOnYa @ BigOnYa:
    Nighty night
  • Sonic Angel Knight @ Sonic Angel Knight:
    Darkness is my friend, I become invisible in the night.
  • Sonic Angel Knight @ Sonic Angel Knight:
    Best camouflaged
  • Psionic Roshambo @ Psionic Roshambo:
    I fear not the dark... the darkness fears me... lol
  • BakerMan @ BakerMan:
    shoot, i'll send you a friend request ig
    BakerMan @ BakerMan: shoot, i'll send you a friend request ig