lundi 20 juillet 2020

Testing input in an Array

Im practising coding with arrays and I got a problem trying to test the input from the user.

This code lets the user input a binary sequence and do different operations with it. I´ve accomplished most of it, my problem is that I cant find a way to limit the input of the user.

He should not be able to input more than 8 Bits and the input must only be 1s and 0s. I can imagine that I could use a for loop for this testing but I cant find a way to implement it so that it works properly.

I wuld be very thankful if someone could help me further. Thanks in advance.

This is the code that I wrote:


#include <stdio.h>
#include <windows.h> 

void bitCheck(int byte[], int position);
void bitSet(int byte[], int position);
void bitDelete(int byte[], int position);
void bitNegation(int byte[], int position);


int main( void )
{
    int byte[7];
    int position;
    int selection = 0;
    char c, i;

    printf("%s\n======================\n\n", "Binary Operations");
    printf("Byte in an 8-Bit sequence: ");

    //My problem is in the code section below:

    while (scanf("%1d%1d%1d%1d%1d%1d%1d%1d", &byte[7],&byte[6],&byte[5],&byte[4],&byte[3],&byte[2],&byte[1],&byte[0]) != 8 || byte[] != 0 || byte[] != 1){ 
        printf("\nWrong input. Please write a valid 8-Bit sequence: ");
        fflush(stdin);
    }

    while (selection!=5){

        printf("\nAt which bit-position should the operation be made (7...0)?: ");

        while (scanf("%d", &position) != 1 || position < 0 || position > 7){
            printf("\nWrong input. Please write a valid position: ");
            fflush(stdin);
        }

        printf("\n1: Bit check\n2: Bit set\n3: Bit delete\n4: Bit negation\n5: End\n\n");

        while (scanf("%d", &selection) != 1 || selection < 1 || selection > 5){
            printf("Wrong input. Please write a valid option: ");
            fflush(stdin);
        }

        switch(selection){                           
        case 1: bitCheck(byte, position);     
            break;
        case 2: bitSet(byte, position);
            break;
        case 3: bitDelete(byte, position);
            break;
        case 4: bitNegation(byte, position);
            break;
        case 5: return 0;  
        }
    }

    getchar(); 
}
    
//Sorry for the german language in the code below, i think this code is not relevant for my question, but I still wanted to post it

void bitCheck (int byte[], int position){
    
    if(byte[position]==1)
    {
        printf("\nDas Bit an der Position %d ist gesetzt.\n", position);
    } 
    else if(byte[position]==0)
    {
        printf("\nDas Bit an der Position %d ist nicht gesetzt.\n",position); 
    }
  }
  

void bitSet(int byte[], int position) 
{
    if(byte[position]==1) 
    {
        byte[position]=0;
        printf("\nNach der Bitoperation ergibt sich folgendes Bitmuster für das Byte: %d%d%d%d%d%d%d%d\n",byte[7],byte[6],byte[5],byte[4],byte[3],byte[2],byte[1],byte[0]);
    }
    else if(byte[position]==0) 
    {
        byte[position]=1;
        printf("\nNach der Bitoperation ergibt sich folgendes Bitmuster für das Byte: %d%d%d%d%d%d%d%d\n",byte[7],byte[6],byte[5],byte[4],byte[3],byte[2],byte[1],byte[0]);
    }    
}

void bitDelete(int byte[], int position) 
{
    if (byte[position]==1)
    {
        byte[position]=0;
        printf("\nNach der Bitoperation ergibt sich folgendes Bitmuster für das Byte: %d%d%d%d%d%d%d%d\n",byte[7],byte[6],byte[5],byte[4],byte[3],byte[2],byte[1],byte[0]);
    } 
    else if (byte[position]==0)
    {
        printf("\nDas Bit ist schon auf 0 gesetzt!\n");
        printf("\nDas Byte hat selbes Bitmuster wie davor: %d%d%d%d%d%d%d%d\n",byte[7],byte[6],byte[5],byte[4],byte[3],byte[2],byte[1],byte[0]);
    }
}

void bitNegation (int byte[], int position)
{
    if (byte[position]==1)
    {
        byte[position]= 0;
        printf("\nNach der Bitoperation ergibt sich folgendes Bitmuster für das Byte: %d%d%d%d%d%d%d%d\n",byte[7],byte[6],byte[5],byte[4],byte[3],byte[2],byte[1],byte[0]);
    } 
    else if (byte[position]==0)
    {
        byte[position]=1;
        printf("\nNach der Bitoperation ergibt sich folgendes Bitmuster für das Byte: %d%d%d%d%d%d%d%d\n",byte[7],byte[6],byte[5],byte[4],byte[3],byte[2],byte[1],byte[0]);
    }
}

Aucun commentaire:

Enregistrer un commentaire