Need help with a basic program, please help ASAP

This is a discussion on Need help with a basic program, please help ASAP within the C Programming forums, part of the General Programming Boards category; Hey I've got an assignment and whatever I do it seems to not work. I don't know what's the problem, ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    8

    Question Need help with a basic program, please help ASAP

    Hey
    I've got an assignment and whatever I do it seems to not work. I don't know what's the problem, I tried many different fixes but I need to finish it today so I have to ask for help.
    The program is kinda self explanatory in the printf sections, if you need any further info tell me! Oh and I can't use arrays and such, what's in the program is all I have...
    Thanks in advance!

    Edit: Sorry all, I'll make myself clear, thanks DeadPlanet for the notice, I would change the topic name if I could (I can't, can I?)
    I am supposed to write a program that asks if the user wants to "translate" binary to Gray code or the other way around.

    In order to understand what I am meant to do, you need to know how to translate numbers between Gray code and Binary code, if you need that, I'll post what I understand about it.

    In the first choice - Binary to Gray, the program is supposed to take a number, and check if that number + the last input number (in the case of the first one, it's always the same number) is 0 or 1 by the use of the mod 2. (or XoR). The program just does smileys when I use %c or Putchar() or other stuff that are not what I want. Also, if I use %d for Integer, it gives me long numbers, or sometimes a few 000's, which is not meant to happen, aswell?

    The second case, Gray to Binary, the program counts how many 1's there are before the current input. if the number is dual (like 0,2,4, etc) then the output char should be the same as the input, otherwise, it's !input of the char.

    I'm not supposed to use any advanced stuff, and mostly only what I already used.

    Code:
    int main()
    {
      int userinput=0, lastinput=0, userchoice, MSB=(userinput+lastinput)%2, countduality=0, notinput=!userinput;
      printf("Select operation to perform (enter 'a' to convert BINARY to GRAY, or 'b' to convert GRAY to BINARY): ");
      userchoice=getchar();
      fflush(stdin);
      switch(userchoice)
        {
          case 'a' :
            printf("Converting BINARY to GRAY. Enter number to convert: ");
            while (userinput!='\n')
              {
                userinput=getchar();
                printf("%d", MSB);
                lastinput=userinput;
              }
            break;
          case 'b' :
            printf("Converting GRAY to BINARY. Enter number to convert: ");
            while (userinput!='\n')
              {
                userinput=getchar();
                if (!countduality%2)
                  printf("%d", userinput);
                else
                  printf("%d", notinput);
                if (userinput)
                  countduality=countduality+1;
              }
            break;
          default :
            printf("Invalid operation was selected.\n");
            break;
        }
      printf("\nGoodbye!");
      return 0;
    }
    Last edited by MrPriest; 04-25-2010 at 08:22 AM.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    You won't get help any faster than anyone else by posting that your problem is urgent. Please, next time post a title related to what you're actually posting about.

    You don't give any context for your program, this means that only people who can see what you are trying to do can truly help you.

    Flushing stdin is undefined. There are a bunch of different ways to eat the newline character after input. If you know input will only be one character and then the simplest way is just to add another getchar call.

    Some of your variables are being initialised at the beginning of main, and then not being changed before you print them. I doubt this is the desired behaviour. For example, 'notinput' will always be 1, because you don't change it when 'userinput' gets changed. if( userinput ) will almost always be true, because entering 0 on the keyboard must be done using OS specific control characters (I think). 0 is not the same as ASCII '0'.

    There are probably other errors but I'm part of the group I mentioned above, I don't understand the context of your program.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    > fflush(stdin);
    Cprogramming.com FAQ > Why fflush(stdin) is wrong

    > MSB=(userinput+lastinput)%2
    C isn't prolog, where you can just list all the commands up front, and they will automatically re-evaluate whenever some data changes.

    In C, this happens just once, before you read any input.
    You need to do the calculation between where you have the input, and where you use the output.
    > userinput=getchar();
    > printf("%d", MSB);

    I see I'm 0 for 2 against DeadPlanet today
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    8
    I can't use what we did not have in class yet, and fflush is all I know, thanks for the head up though, I'll read it.
    I changed a bit of the code but the program does not function yet...
    The thing I am most puzzled about is the fact that the numbers I am receiving are weird - when I input just '1' I get 490! '0' I get 0, (that is if I use %d). I thought maybe I should flush after every getchar but then the ending of the while loop will need to press enter twice, and all it does is really just get rid of one of the 0's (if I input '1' I get 49 and if I input '0' I get space...)

    EDIT:
    The first part (case a) works, but it still adds a number at the end (1 or 0...?) I can't manage to get rid of it...
    the 49 thing only appears at case b, however I noticed it only happens for the first input, and only if it is '1', if it is '0' it give 48...

    Code:
    int main()
    {
      int userinput, lastinput, userchoice, countduality=0;
      printf("Select operation to perform (enter 'a' to convert BINARY to GRAY, or 'b' to convert GRAY to BINARY): ");
      userchoice=getchar();
      getchar();
      switch(userchoice)
        {
          case 'a' :
            printf("Converting BINARY to GRAY. Enter number to convert: ");
            while (userinput!='\n')
              {
                userinput=getchar();
                lastinput=0;
                printf("%d", ((userinput+lastinput)%2));
                lastinput=userinput;
              }
            break;
          case 'b' :
            printf("Converting GRAY to BINARY. Enter number to convert: ");
            while (userinput!='\n')
              {
                userinput=getchar();
                if (!countduality%2)
                  printf("%d", userinput);
                else
                  printf("%d", !userinput);
                if (userinput)
                  countduality++;
              }
            break;
          default :
            printf("Invalid operation was selected.\n");
            break;
        }
      printf("\nGoodbye!");
      return 0;
    }
    Last edited by MrPriest; 04-25-2010 at 09:14 AM. Reason: Gotten rid of the fflush in favour of getchar().

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So '0' is 48, and '1' is 49, ..., and '9' is 57, internally inside the machine. If you want to see a character you need to specify %c in your format string: "print as character".

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    8
    Well when I tried %c I got smileys and such =/ I'll try again now, I did not try after I "improved" the proggie... Thanks for the tip.

    Well now I get only a single number - if I input something that starts with '1' I receive only '1', same with '0'
    (input= 100001010 output= 1, input= 010101010 output= 0)
    Last edited by MrPriest; 04-25-2010 at 09:36 AM.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    getchar() only reads one character from stdin at a time. If you wish to read a binary number then that will be more difficult.

    Assuming you can only use getchar, you could create a loop which reads in a char, bitshifts your variable that holds the input left by one, and then bitwise ORs the character with it (after subtracting '0' from it, to get the actual number that is).

    There may be a standard C function that could be used instead, but I'm personally not aware of one.

    Also, 'lastinput' is uninitialised when you use it, so it could be anything. EDIT: No it isn't, my bad.
    Last edited by DeadPlanet; 04-25-2010 at 09:50 AM.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    8
    I can go and use scanf, should I? It might fix the problem at case b...

    "bitshifts your variable that holds the input left by one, and then bitwise ORs the character with it (after subtracting '0' from it, to get the actual number that is)."

    I don't know any of that stuff, I am in Introduction to Computer Science at the university, it's all basic stuff... Can you explain?

    Edit: No wait, I don't think I can use scanf, nevermind that. It won't work since I wont to input a number at a time.
    Last edited by MrPriest; 04-25-2010 at 10:01 AM.

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    8
    Okay so now I'm down to two last problems.
    1. In the case a, Binary to Gray, it keeps adding one more number at the end, 1 or 0, don't know why.
    2. In the case b, Gray to Binary, It seems to give me the ASCII number for the first number I give it, and adds a few zero's for some reason.

    EDIT:
    I just realised, after following the algorithm myself, that the added number at the end of case a is actually "(enter + last input)mod2" and only then it quits the program, why does it do the final loop? Should'nt it break before the loop?
    Code:
    int main()
    {
      int userinput, lastinput=0, userchoice, countduality=0;
      printf("Select operation to perform (enter 'a' to convert BINARY to GRAY, or 'b' to convert GRAY to BINARY): ");
      userchoice=getchar();
      getchar();
      switch(userchoice)
        {
          case 'a' :
            printf("Converting BINARY to GRAY. Enter number to convert: ");
            while (userinput!='\n')
              {
                userinput=getchar();
                printf("%d", ((userinput+lastinput)%2));
                lastinput=userinput;
              }
            break;
          case 'b' :
            printf("Converting GRAY to BINARY. Enter number to convert: ");
            while (userinput!='\n')
              {
                userinput=getchar();
                if (!countduality%2)
                  printf("%d", userinput);
                else
                  printf("%d", !userinput);
                if (userinput)
                  countduality++;
              }
            break;
          default :
            printf("Invalid operation was selected.");
            break;
        }
      printf("\nGoodbye!");
      return 0;
    }
    Thanks in advance!
    Last edited by MrPriest; 04-26-2010 at 02:36 AM.

  10. #10
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    You're getting your input after it is being tested for '\n'. Then you are doing operations on it. The problem with this is that when you receive '\n', the operations are performed on it before the loop condition is evaluated.

    To remedy this I suggest you get input once before the loop, then once you enter the loop, you can get input after you do your operations.

    For example:
    Code:
    get_input
    while( input_gotten isn't newline )
            operate_on_input
            get_input
    repeat

  11. #11
    Registered User
    Join Date
    Apr 2010
    Posts
    8
    I just thought about using
    Code:
    if(userinput=='\n') break;
    Just before the print command, and it works, and then I saw your comment, I think it would be much more... ehh... estatic? Thanks, I'll use it!
    Now the only problem left is with case b, I'll see if that method somehow changes anything, and comment, thanks again DeadPlanet!

    Edit: No... I don't know why but your way does not work, it for some reason does not count the input before 'enter'.

  12. #12
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    For your second question, you aren't translating the input you're getting from characters into numbers. When you get a character as input it is actually a special numeric code which represents that character (probably an ASCII code, but it could be something else).

    The reason that this isn't a problem in your 'a' case is that luckily for you, the code for 0 is a multiple of 2, so your '0' % 2 is 0 and '1' % 2 is 1.

    However with the second one you're trying to print the characters as numbers. To get the appropriate number from a character, you can simply subtract '0' (not, that's the character 0, not the number) from it.

  13. #13
    Registered User
    Join Date
    Apr 2010
    Posts
    8
    Thanks a lot DeadPlanet, it works perfectly now!
    I think I need to do the same treatment for case a to make sure that every machine with it's ASCII (incase it's different from mine) would work correctly.

    Code:
    int main()
    {
      int userinput, lastinput=0, userchoice, countduality=0;
      printf("Select operation to perform (enter 'a' to convert BINARY to GRAY, or 'b' to convert GRAY to BINARY): ");
      userchoice=getchar();
      getchar();
      switch(userchoice)
        {
          case 'a' :
            printf("Converting BINARY to GRAY. Enter number to convert: ");
            while (userinput!='\n')
              {
                userinput=getchar();
                if(userinput=='\n')
                  break;
                printf("%d", ((userinput+lastinput)%2));
                lastinput=userinput;
              }
            break;
          case 'b' :
            printf("Converting GRAY to BINARY. Enter number to convert: ");
            while (userinput!='\n')
              {
                userinput=getchar();
                if(userinput=='\n')
                  break;
                else if (!(countduality%2))
                  printf("%d", (userinput-'0'));
                else
                  printf("%d", !(userinput-'0'));
                if (userinput-'0')
                  ++countduality;
              }
            break;
          default :
            printf("Invalid operation was selected.");
            break;
        }
      printf("\nGoodbye!");
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Problem with basic encryption program.
    By mmongoose in forum C++ Programming
    Replies: 5
    Last Post: 08-27-2005, 04:41 AM
  4. IDEA: A basic drawing program
    By ygfperson in forum Contests Board
    Replies: 0
    Last Post: 08-12-2002, 11:15 PM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21