I want to make a program where you have to insert binary numbers like this:
do
{
iBinary = getche();
}while(iBinary == 1 || iBinary == 0);
after you get the numbers I want them to be placed behind eachother so you will get:
input:
1
1
0
1
output:
1101
I really need some help with it because I dont know how to do it.
Have you considered firstly storing them in a char array and then printing them out.
can you explain it to me
I've considered using chars but not really sure how to use it.
I still need to do a calculation with the whole value.
To make an array:
char buf[10];
To put something in it:
buf[n]=value;
For an array of 10 elements, you should use elements 0 through 9 inclusive.
UNIX doesn't have "getche". Try 'getc'. Unless you put your terminal in noncanonical mode, it will wait until the user presses enter to receive data.
ok got that but how can i get them behind eachother.
lets say i get this
buf[0] = 1
buf[1] = 1
buf[2] = 0
buf[3] = 1
to this
value = 1101
An array of characters is a string. Stick a '\0' on the end and you can print it just like that with puts, printf, and so forth, as well as strcpy it, etc.
#include <stdio.h>
int main(void)
{
char buf[10];
int pos=0;
while(pos < 9)
{
int c=getc(stdin);
if(c < 0) break; // End of file
if(c == '\n') break; // end of line
if((c < '0') || (c > '1')) continue; // Ignore everything but 0/1
buf[pos++]=c;
buf[pos]='\0'; // NULL terminator
}
printf("You typed: %s\n", buf);
}
1 Like
Null terminate the char array and then just printf("%s\n", buf)...and if you dont know howto null terminate a char array readup on the C language first.