Advertising (This ad goes away for registered users. You can Login or Register)

I need help with something simple (C)

Discuss about your favorite (gaming...or not) devices here. The most popular ones will end up getting their own categories
Programming discussions for your favorite Device
Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
Post Reply
andfoy
Posts: 20
Joined: Fri Nov 19, 2010 4:11 pm
Location: Somewhere in my dreams

I need help with something simple (C)

Post by andfoy » Tue Apr 19, 2011 11:31 pm

I'm coding in C an app for converting decimal numbers into binary ones, but i got a problem, when you input a letter the console freezes into an infinite loop, so what i can do? Here's the code:

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
     int mask = 2147483648; 
     int numInput;

     printf:  Decimal a Binario (Convertir)

     do
    {
        printf ("\n\nIngresar un numero Decimal (o 999999 para salir): "); 
          scanf ("%d", &numInput);

        printf("\nEl equivalente en binario de (%d) es: \t", numInput);

    for (int i = 0; i < 32; i++)
    {

      if ((numInput & mask) == 0)
                {
                     printf("0");
             }

    else
               {
                    printf("1");
               }

    if (numInput == 999999 )
                    {
                        return(0);
                    }

    numInput = numInput << 1;

               }
        }

    while(1)

}
Advertising
"Die Luft der Freiheit weht"
"El hombre no piensa como vive sino que vive como piensa"
PSP 3003 -> 6.20 PRO-XX
PC: Dual boot 7/Ubuntu
Linux FTW!

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: I need help with something simple (C)

Post by m0skit0 » Tue Apr 19, 2011 11:57 pm

Check what value numInput gets when you input a character and try to filter that.
Advertising
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

andfoy
Posts: 20
Joined: Fri Nov 19, 2010 4:11 pm
Location: Somewhere in my dreams

Re: I need help with something simple (C)

Post by andfoy » Wed Apr 20, 2011 12:32 am

All the characters returned 0 so can i do this?:

Code: Select all

if numInput=0
{ puts ("\n\n ERROR!\n");
  return (0);
}
"Die Luft der Freiheit weht"
"El hombre no piensa como vive sino que vive como piensa"
PSP 3003 -> 6.20 PRO-XX
PC: Dual boot 7/Ubuntu
Linux FTW!

electrosheep
Posts: 97
Joined: Tue Jan 11, 2011 2:50 am

Re: I need help with something simple (C)

Post by electrosheep » Wed Apr 20, 2011 12:57 am

Well, I think m0squit0 already nailed it, but imo, I think it would be best for you to figure out whats wrong on your own.
A few tips: your code seems fairly scattered about. If you need a good text editor, notepad ++ will help you organize your code so it's easier for other people to read.
If you honestly have no idea where to start, I'll help you: You have a loop whose terminating conditions are never met, so it never ends. It's fairly obvious what the problem is once you know what to look for.

EDIT: actually, your code is poorly written. For example, you have something like this:

Code: Select all

do{
     ...
     if (condition)
          return 0; // thus ending the loop
} while (1);
Why do that when you can simply do this:

Code: Select all

do{
     ...
} while (condition);
Last edited by electrosheep on Wed Apr 20, 2011 7:59 pm, edited 1 time in total.
Let me see...your grandmother's name was Beatrice?

andfoy
Posts: 20
Joined: Fri Nov 19, 2010 4:11 pm
Location: Somewhere in my dreams

Re: I need help with something simple (C)

Post by andfoy » Wed Apr 20, 2011 1:12 am

Wow, thanks. Sorry i'm starting to learn C, so i use all
"Die Luft der Freiheit weht"
"El hombre no piensa como vive sino que vive como piensa"
PSP 3003 -> 6.20 PRO-XX
PC: Dual boot 7/Ubuntu
Linux FTW!

electrosheep
Posts: 97
Joined: Tue Jan 11, 2011 2:50 am

Re: I need help with something simple (C)

Post by electrosheep » Wed Apr 20, 2011 8:01 pm

So did you get it figured out?
Let me see...your grandmother's name was Beatrice?

Post Reply

Return to “Programming and Security”