Add number input.

This commit is contained in:
paulusja 2026-04-16 13:43:54 +02:00
parent 63c0399302
commit 474138d53f
5 changed files with 40 additions and 13 deletions

View File

@ -23,7 +23,7 @@
#include <stdio.h>
#include <ctype.h>
#include "myio.h"
#include "../io/myio.h"
int main()

View File

@ -1,12 +0,0 @@
#include <stdio.h>
#include "myio.h"
unsigned char getSingleChar()
{
unsigned char result = 'a';
result = getchar();
while(result != '\n' && getchar() != '\n') {}
return result;
}

27
io/myio.c Normal file
View File

@ -0,0 +1,27 @@
#include <stdio.h>
#include "myio.h"
unsigned char getSingleChar()
{
unsigned char result = 'a';
result = getchar();
while(result != '\n' && getchar() != '\n') {}
return result;
}
int getNumber(const char *text)
{
int number = 0;
int wasSuccessful = 0;
do
{
printf("%s", text);
wasSuccessful = scanf("%d", &number);
while(getchar() != '\n') {}
} while(!wasSuccessful);
return number;
}

View File

@ -2,5 +2,6 @@
#define MYIO_H
unsigned char getSingleChar();
int getNumber(const char *text);
#endif

11
io/testInput.c Normal file
View File

@ -0,0 +1,11 @@
#include <stdio.h>
#include "../io/myio.h"
int main()
{
int number = getNumber("Geben Sie eine Zahl ein: ");
printf("number: %d\n", number);
return 0;
}