Search code examples
cprintfscanf

How can I read the scanf without changing the printf positions?


I am very new into programming and have a problem with the scanf. I have to write a program with a structured output but I really don't know how I can read this scanf without changing the output. It's German language used in the output so I have to explain that I'm writing a calculator with bitwise operators and got the hint that I should use the getNumber function but I haven't found anything to understand how it works.

The first scanf should read the input which is the first number in the calculator while the second scanf should be nothing more than only some bit operators so I declared them in the main function. Here is my code if it helps:

    #include <stdio.h>
#include "escapesequenzen.h"



void dummy(){

    char Dummy;
    
    do
        scanf("%c", &Dummy);
    while(Dummy != '\n');

}


/*

Block zum Kopieren:

printf(
"\n|----------------------------------------------------------|"
"\n| Bitoperatoren-Rechner                                    |"
"\n|                                                          |"
"\n| Eingabe Zahl 1:                                          |"
"\n| Operator:                                                |"
"\n| Eingabe Zahl 2:                                          |"
"\n|                                                          |"
"\n|----------------------------------------------------------|"
"\n|                                                          |"
"\n|          |  dez.  |  okt.  |  hex.  | Binaerdarstellung  |"
"\n|  Zahl 1  |        |        |        |                    |"
"\n| Operator |        |        |        |                    |"
"\n|  Zahl 2  |        |        |        |                    |"
"\n| -------------------------------------------------------- |"
"\n| Ergebnis |        |        |        |                    |"
"\n|                                                          |"
"\n|----------------------------------------------------------|");

*/


void print1(short Zahl1, short Zahl2, int Eingabe, char Dummy){

printf(
"\n|----------------------------------------------------------|"
"\n| Bitoperatoren-Rechner                                    |"
"\n|                                                          |"
"\n| Eingabe Zahl 1:                                          |"
"\n| Operator:                                                |"
"\n| Eingabe Zahl 2:                                          |"
"\n|                                                          |"
"\n|----------------------------------------------------------|"
"\n|                                                          |"
"\n|          |  dez.  |  okt.  |  hex.  | Binaerdarstellung  |"
"\n|  Zahl 1  |        |        |        |                    |"
"\n| Operator |        |        |        |                    |"
"\n|  Zahl 2  |        |        |        |                    |"
"\n| -------------------------------------------------------- |"
"\n| Ergebnis |        |        |        |                    |"
"\n|                                                          |"
"\n|----------------------------------------------------------|");

}

int main(){

    short Zahl1;
    short Zahl2;
    int Eingabe;
    short Operator;
    char Dummy;

    print1(Zahl1, Zahl2, Eingabe, Operator, Dummy);
    getNumber();

    return 0;

}

Edit: If it helps, here is the "escapesequenzen" heather:

#ifndef escapesequenzen_h
   #define escapesequenzen_h escapesequenzen_h

   #define POSITION(Ze, Sp)     printf("\033[%d;%dH",Ze,Sp)
   #define HOME                 printf("\033[H")
   #define UP(Anz)              printf("\033[%dA",Anz)
   #define UP_LINE              printf("\033[A")
   #define DOWN(Anz)            printf("\033[%dB",Anz)
   #define DOWN_LINE            printf("\033[B")
   #define RIGHT(Anz)           printf("\033[%dC",Anz)
   #define ONE_POS_RIGHT        printf("\033[C")
   #define LEFT(Anz)            printf("\033[%dD",Anz)
   #define ONE_POS_LEFT         printf("\033[D")

   #define STORE_POS            printf("\033[s")
   #define RESTORE_POS          printf("\033[u")
   #define ACT_POS              printf("\033[6n")

   #define CLEAR                printf("\033[2J")
   #define CLEAR_LINE           printf("\033[K")

   #define ATTRIBUTE_OFF        printf("\033[0m")
   #define BOLD                 printf("\033[1m")
   #define UNDERSCORE           printf("\033[4m")
   #define BLINK                printf("\033[5m")
   #define INVERSE              printf("\033[7m")
   #define INVISIBLE            printf("\033[8m")

   #define FORECOLOR_BLACK      printf("\033[30m")
   #define FORECOLOR_RED        printf("\033[31m")
   #define FORECOLOR_GREEN      printf("\033[32m")
   #define FORECOLOR_YELLOW     printf("\033[33m")
   #define FORECOLOR_BLUE       printf("\033[34m")
   #define FORECOLOR_VIOLETT    printf("\033[35m")
   #define FORECOLOR_KOBALT     printf("\033[36m")
   #define FORECOLOR_WHITE      printf("\033[37m")
   #define BACKCOLOR_BLACK      printf("\033[40m")
   #define BACKCOLOR_RED        printf("\033[41m")
   #define BACKCOLOR_GREEN      printf("\033[42m")
   #define BACKCOLOR_YELLOW     printf("\033[43m")
   #define BACKCOLOR_BLUE       printf("\033[44m")
   #define BACKCOLOR_VIOLETT    printf("\033[45m")
   #define BACKCOLOR_KOBALT     printf("\033[46m")
   #define BACKCOLOR_WHITE      printf("\033[47m")

   #define TEXT_BW_25_40        printf("\033[=0h")
   #define TEXT_COLOR_25_40     printf("\033[=1h")
   #define TEXT_BW_25_80        printf("\033[=2h")
   #define TEXT_COLOR_25_80     printf("\033[=3h")
   #define GRAFIC_COLOR_320_200 printf("\033[=4h")
   #define GRAFIC_BW_320_200    printf("\033[=5h")
   #define GRAFIC_BW_640_200    printf("\033[=6h")
   #define WRAP_MODE_ON         printf("\033[=7h")
   #define WRAP_MODE_OFF        printf("\033[=7l")
#endif

Solution

  • I am not familiar with the "getNumber" function, but here is one possible method of using the "scanf" function to input the two integers and a one-character operator (e.g. "+", "-", "*", or "/") following is an updated version of your program code for your consideration and experimentation.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    /*
    
    Block zum Kopieren:
    
    printf(
    "\n|----------------------------------------------------------|"
    "\n| Bitoperatoren-Rechner                                    |"
    "\n|                                                          |"
    "\n| Eingabe Zahl 1:                                          |"
    "\n| Operator:                                                |"
    "\n| Eingabe Zahl 2:                                          |"
    "\n|                                                          |"
    "\n|----------------------------------------------------------|"
    "\n|                                                          |"
    "\n|          |  dez.  |  okt.  |  hex.  | Binaerdarstellung  |"
    "\n|  Zahl 1  |        |        |        |                    |"
    "\n| Operator |        |        |        |                    |"
    "\n|  Zahl 2  |        |        |        |                    |"
    "\n| -------------------------------------------------------- |"
    "\n| Ergebnis |        |        |        |                    |"
    "\n|                                                          |"
    "\n|----------------------------------------------------------|");
    
    */
    
    void print1(short Zahl1, short Zahl2, char* Oper)
    {
        char Zahl_okt1[6], Zahl_okt2[6], Zahl_hex1[16], Zahl_hex2[16], Er_okt[6], Er_hex[16];
        
        int Ergebnis = 0;
        
        if ((strcmp(Oper, "+")) == 0)
            Ergebnis = Zahl1 + Zahl2;
    
        if ((strcmp(Oper, "-")) == 0)
            Ergebnis = Zahl1 - Zahl2;
    
        if ((strcmp(Oper, "*")) == 0)
            Ergebnis = Zahl1 * Zahl2;
    
        if ((strcmp(Oper, "/")) == 0)
            Ergebnis = Zahl1 / Zahl2; /* This will be an integer result as is */
    
        sprintf(Zahl_okt1, "%o", Zahl1); /* To get the octal equivalent */
        sprintf(Zahl_okt2, "%o", Zahl2);
        sprintf(Er_okt, "%o", Ergebnis);
        sprintf(Zahl_hex1, "%02x", Zahl1); /* To get the hexadecimal equivalent */
        sprintf(Zahl_hex2, "%02x", Zahl2);
        sprintf(Er_hex, "%08x", Ergebnis);
    
        /* You will probably need to search for a good decimal to binary conversion function. */
        /* I did not spend any time looking for one.*/
    
        printf(
            "\n|----------------------------------------------------------|"
            "\n| Bitoperatoren-Rechner                                    |"
            "\n|                                                          |"
            "\n| Eingabe Zahl 1: %8d                                 |"
            "\n| Operator......: %s                                        |"
            "\n| Eingabe Zahl 2: %8d                                 |"
            "\n|                                                          |"
            "\n|----------------------------------------------------------|"
            "\n|                                                          |"
            "\n|          |  dez.   |  okt.  |  hex.  | Binaerdarstellung |"
            "\n|  Zahl 1  |%8d |%8s|%8s|                   |"
            "\n| Operator | %s       |        |        |                   |"
            "\n|  Zahl 2  |%8d |%8s|%8s|                   |"
            "\n| ---------------------------------------------------------|"
            "\n| Ergebnis |%8d |%8s|%8s|                   |"
            "\n|                                                          |"
            "\n|----------------------------------------------------------|\n\n",
            Zahl1, Oper, Zahl2, Zahl1, Zahl_okt1, Zahl_hex1, Oper, Zahl2, Zahl_okt2, Zahl_hex2, Ergebnis, Er_okt, Er_hex);
    
    }
    
    int main()
    {
        short Zahl1;
        short Zahl2;
        char Operator[16];
    
        printf("Geben Sie Variable 1 ein: "); /* Sorry if my German is rusty */
        scanf("%hu", &Zahl1);
        printf("\nGeben Sie Variable 2 ein: ");
        scanf("%hu", &Zahl2);
        printf("\nGeben Sie den Betreiber ein ( + - * /): ");
        scanf("%s", Operator);
        printf("\n\n"); /* Just to keep things looking clean */
        
        print1(Zahl1, Zahl2, Operator);
        
        return 0;
    
    }
    

    In testing this code out here is a sample of the input and output on my terminal (I requested 55 * 84).

    Geben Sie Variable 1 ein: 55
    
    Geben Sie Variable 2 ein: 84
    
    Geben Sie den Betreiber ein ( + - * /): *
    
    |----------------------------------------------------------|
    | Bitoperatoren-Rechner                                    |
    |                                                          |
    | Eingabe Zahl 1:       55                                 |
    | Operator......: *                                        |
    | Eingabe Zahl 2:       84                                 |
    |                                                          |
    |----------------------------------------------------------|
    |                                                          |
    |          |  dez.   |  okt.  |  hex.  | Binaerdarstellung |
    |  Zahl 1  |      55 |      67|      37|                   |
    | Operator | *       |        |        |                   |
    |  Zahl 2  |      84 |     124|      54|                   |
    | ---------------------------------------------------------|
    | Ergebnis |    4620 |   11014|0000120c|                   |
    |                                                          |
    |----------------------------------------------------------|
    

    When using the "scanf" function, it is usually proceeded by a "printf" function that prompts the user as to what type of data should be entered. Also, I included the "<string.h>" file reference in order to utilize various string functionality such as the "sprintf" function.

    I hope that provides you with some ideas and helps you progress.

    Regards.