C question console application windows

Joeado123

Well-Known Member
OP
Member
Joined
Mar 25, 2017
Messages
105
Trophies
0
Age
34
XP
483
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
34
XP
483
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
35
XP
4,301
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
  • Skelletonike @ Skelletonike:
    Guess things were worse than they wanted to let others know
  • BigOnYa @ BigOnYa:
    @salazarcosplay When it shuts off on its own, does the power supply light show red or orange light? If its red, most likely bad power supply. Orange is normal off, green when powered on, red is a fault.
  • K3Nv3 @ K3Nv3:
    Everyone's giving that game repo attention and it just feels like an among us clone
  • BigOnYa @ BigOnYa:
    Even after turning on, then wait for it to power off on its own, then what color? If orange then normal. I'd start with repaste. Most likely just overheating.
  • K3Nv3 @ K3Nv3:
    Damn this sounds good https://a.co/d/062jnbn
  • BigOnYa @ BigOnYa:
    @K3Nv3, Dates, butter, n salt be better. Of course you've prob never had a date tho.
  • BigOnYa @ BigOnYa:
    @K3Nv3, You never answered, get your PS3 yet? Luckily those controllers I found, charged after all, were just so dead, didn t show at first they were actually charging.
  • Skelletonike @ Skelletonike:
    The switch 2 should be releasing around June or July.
  • Skelletonike @ Skelletonike:
    In the new Nintendo app, the calender has Nintendo switch 2 experience events up until may.
  • Skelletonike @ Skelletonike:
    Back when I went to a 3ds experience event, the console was released the following month or so.
  • Skelletonike @ Skelletonike:
    Last event in the calender is 31st May in Seoul... So maybe July? HM
  • BigOnYa @ BigOnYa:
    Yea rumors are saying June 15th. Edit- June 19th I mean sorry. Guess we will know for sure next Wednesday.
  • Skelletonike @ Skelletonike:
    That would be a nice Timing.
  • Skelletonike @ Skelletonike:
    Apparently they will post more specific details on the app after the direct. Well, one week to go.
  • BigOnYa @ BigOnYa:
    I was gonna get but now think I'm gonna wait till a exploit/ modchip comes. Looking at getting a ROG Aly Z1 or Xbox handheld coming this summer. I'm not a big Nintendo fan.
  • Skelletonike @ Skelletonike:
    I mostly enjoy Nintendo games when it comes to first party.
    +1
  • BigOnYa @ BigOnYa:
    Yea same, Zelda, Metroid, Mario games, usually just exclusives can't play on other consoles.
  • BigOnYa @ BigOnYa:
    That n think i'll wait n get a Switch 2 OLED model when they come few years later, I don't want a LCD. I have LCD and OLED Switch1, and OLED so much better.
  • Kirbydogs @ Kirbydogs:
    TOMODACHI LOFE LIVING THE DREAM
  • Kirbydogs @ Kirbydogs:
    Honestly, though, kinda disappointing- looks like an avarage indie game. And the music sucks!
  • Kirbydogs @ Kirbydogs:
    Even the trailer sucks!!
  • Xdqwerty @ Xdqwerty:
    I don't care about the nintendo direct
    BigOnYa @ BigOnYa: Looks cool: https://youtu.be/t7gULP9ZEpE?si=NlSaa4qPml6Lo97W