putting numbers behind eachother


 
Thread Tools Search this Thread
Top Forums Programming putting numbers behind eachother
# 1  
Old 09-19-2011
Error putting numbers behind eachother

I want to make a program where you have to insert binary numbers like this:

Code:
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.
# 2  
Old 09-19-2011
Have you considered firstly storing them in a char array and then printing them out.
# 3  
Old 09-19-2011
can you explain it to me :P
I've considered using chars but not really sure how to use it.
I still need to do a calculation with the whole value.
# 4  
Old 09-19-2011
To make an array:

Code:
char buf[10];

To put something in it:

Code:
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.
# 5  
Old 09-19-2011
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
# 6  
Old 09-19-2011
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.
Code:
#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);
}

This User Gave Thanks to Corona688 For This Post:
# 7  
Old 09-19-2011
thank you so very much
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Hardware

Putting an old hd in a new computer

What are the steps you need to take when you put an old HD in a new computer? I just did this. Every time it makes it to the windows boot screen then restarts. I have a bunch of old engineering software that is not compatible with the newer versions of windows. I figured this out after I bought... (6 Replies)
Discussion started by: cokedude
6 Replies

2. UNIX for Beginners Questions & Answers

Decimal numbers and letters in the same collums: round numbers

Hi! I found and then adapt the code for my pipeline... awk -F"," -vOFS="," '{printf "%0.2f %0.f\n",$2,$4}' xxx > yyy I add -F"," -vOFS="," (for input and output as csv file) and I change the columns and the number of decimal... It works but I have also some problems... here my columns ... (7 Replies)
Discussion started by: echo manolis
7 Replies

3. Solaris

Guest LDOMS on same subnet cant ping eachother

Hi all, New to this forum. I have just been reading through a historical thread about some issues with IPMP. Some tips from "Peasant" where very useful. Please see below "Just couple of more hints regarding VM. For VDS, use one VDS - one guest LDOM, don't put everything in primary-vds.... (9 Replies)
Discussion started by: selectstar
9 Replies

4. UNIX for Dummies Questions & Answers

Putting $$ before filename

Hello , I am searching a directory for a file and have to assign the filename to a variable . The variable must have form $$filename So my code is echo "'$$filename='`ls -lrt *PreMatch*.csv| head -1 | nawk '{print $9}'`" however $$ is converting to a number . How could I make it $$... (3 Replies)
Discussion started by: Pratik4891
3 Replies

5. Shell Programming and Scripting

Help awk/sed: putting a space after numbers:to separate number and characters.

Hi Experts, How to sepearate the list digit with letters : with a space from where the letters begins, or other words from where the digits ended. file 52087mo(enbatl) 52049mo(enbatl) 52085mo(enbatl) 25051mo(enbatl) The output should be looks like: 52087 mo(enbatl) 52049... (10 Replies)
Discussion started by: rveri
10 Replies

6. UNIX for Dummies Questions & Answers

Print numbers and associated text belonging to an interval of numbers

##### (0 Replies)
Discussion started by: lucasvs
0 Replies

7. Shell Programming and Scripting

the smallest number from 90% of highest numbers from all numbers in file

Hello All, I am having problem to find what is the smallest number from 90% of highest numbers from all numbers in file. I am having file with thousands of lines and hundreds of columns. I am familiar mainly with bash but I am open to whatever suggestion witch will lead to the solutions. If I... (11 Replies)
Discussion started by: Apfik
11 Replies

8. Shell Programming and Scripting

read numbers from file and output which numbers belongs to which range

Howdy experts, We have some ranges of number which belongs to particual group as below. GroupNo StartRange EndRange Group0125 935300 935399 Group2006 935400 935476 937430 937459 Group0324 935477 935549 ... (6 Replies)
Discussion started by: thepurple
6 Replies

9. IP Networking

Link two sockets to eachother

Hi, I'm programming a system in C++ which will send messages from a server to an ip-camera (both are connected to the system). What it does now is take the message from the server-socket and puts it in the camera-socket. The thing is, after that the camera is going to send a video stream... (1 Reply)
Discussion started by: Eckhardt
1 Replies

10. UNIX for Dummies Questions & Answers

Putting pC on network

How do I put a my PC with linux 7.0 on a class B network. Can someone give me info or text that will guide me? (1 Reply)
Discussion started by: Rush
1 Replies
Login or Register to Ask a Question