0
#include<stdio.h>
#include <math.h>
#include <stdio.h>
#include <string.h>

int main() {
    int numeris1;
    int numeris2;
    int numeris3;
    int octal;
    int amzius;
    int a=0;
    char veiksmas;
    char name[20];
    printf("Ivesk Varda: ");
    scanf("%s", name);
    printf("Ivesk Amziu: ");
    scanf("%d",&amzius);
    printf("Tavo vardas: %s.\n", name);
    printf ("Tavo Amzius: %d.\n", amzius);
    do{
    printf("\n1. Uzduoties salyga \n2. Uzduoties atlikimas \n3. Uzdaryti programa\n");
    printf("Iveskite pasirinkima: ");
    scanf("%d", &veiksmas);

    switch (veiksmas)
    {
        case 1 :
            printf("\nIvedami trys dyzelinio traukinio vagono ilgiai astuntaineje skaiciavimo sistemoje,\n");
            printf("kuriuos programa sudeda ir rezultata atspauzdina sesioliktaineje skaiciu sistemoje\n");
        break;
        case 2 :
            printf("\nIveskite pirma traukinio ilgi: ");
            scanf("%o",&numeris1);
            printf("\nIveskite antro traukinio ilgi: ");
            scanf("%o",&numeris2);
            printf("\nIveskite trecio traukinio ilgi: ");
            scanf("%o",&numeris3);

            octal=numeris1+numeris2+numeris3;


            printf("\nVisu traukiniu vagonu ilgis...\n");
            printf("%x\n",octal);
        break;
        case 3 :
            return 0;
        break;
        default : printf("%c blogas pasirinkimas \n\n", veiksmas);



    }
    }while(1);}

Im writing code so it converts octal to hexa numbers but while im in my menu ant enter a letter opose to a number my console just starts looping my menu over and over.

For example if i enter a wrong number in my menu i made so it says wrong number choose again. And i want the same to do for when you enter a letter to print blogas pasirinkimas

dude mcree
  • 23
  • 3
  • 1
    You should check the return value of `scanf` to see if it read an item or not. If it did not, the input stream will be positioned at the first invalid character and you should take some appropriate action. – Ian Abbott Apr 08 '22 at 12:33
  • You can't stop users from entering letters and punctuation etc – Jonathan Leffler Apr 08 '22 at 12:51
  • You can't stop users from entering letters and punctuation, etc. You have to deal with the erroneous input when it happens. The first step is checking that `scanf()` was successful. If it wasn't, the bogus data is still waiting to be read, so you need to clean up. One effective way is to read up to and including the next newline before going back for more: `int c; while ((c = getchar()) != EOF && c != '\n') ;`, possibly packaged as a `static inline` function that you can call after spotting that `scanf()` did not read anything that looked like a number. – Jonathan Leffler Apr 08 '22 at 13:39

0 Answers0