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;
}