Hey guys i'm new to C and i've got a little problem.
I would like to write a program that gives you a few default values. Now you can keep these values by pressing the enter key or you can change them by just typing in new ones and confirm the new values by pressing the enter key again.
Thats as far as i got on my own but it doesn't really work. I would appreciate it if one of you guys could tell what i can do to fix this.
Code:
#include<stdio.h>
#include <conio.h>
int getInt(int min, int max);
int main ()
{
int a[3] = {3,4};
int b;
int code;
int loop;
for (b = 0 ; b < 2 ; b++){
printf ("a[%d] = ",b);
do{
code = _getch();
switch (code){
case 13:
printf("%d\n", a[b]);
loop = 1;
break;
default:
a[b] = getInt(0,6);
loop = 1;
break;
}
}while (loop != 1);
}
}
int getInt(int min, int max)
{
int num;
char ch;
int error = 1;
do {
fflush(stdin);
//Error if you enter a character
if (scanf("%d%c", &num, &ch) != 2)
{
printf("Error you entered a character\n");
}
//Error if you enter an incorect value
else if ((ch != 10) || (num > max) || (num < min)) //
{
printf("Error, inncorect range of values\n");
}
//No Error, exit loop
else
{
error = 0;
}
} while (error == 1);
return num;
}