Hi, i want to make a program that lets the user input as many numbers as they like and terminates when the user enters "EXIT" and prints the largest & smallest input, heres my code:

#include<stdio.h>
#include<string.h>
#include <stdlib.h>

int main (){
	int input;

	int min,max,a=0;
	char exit[] = "EXIT";
	char exit2[100];
	min = INT_MAX;
	max = INT_MIN;

	do{
		
		printf ("Insert any value[type EXIT to end]: ");
		scanf("%d",&input);
		gets (exit2);
		fflush(stdin);
		
		if (strcmp(exit2,exit)==0){
			break;
		}
			
		if (max < input){
			max = input;
			a++;
		}
		if (min > input){
			min = input;
		}
		
	}while (strcmp(exit2,exit)!=0);
		if (a==0){
			return 0;
		}else{
	printf ("Smallest :%d\n",min);
	    printf ("Biggest : %d\n",max);

getchar();
		}
		return 0;
	}

now this code works fine except when the first input is not a number it prints some weird number for some reason(-858993460)...so i added this to line 24

if(scanf("%d",&input)!=1 && strcmp(exit2,exit)!=0){
			//blablabla
		}

but its still not working, can anyone tell me why and how to fix it please thanks

You can just always parse the input yourself by getting input as string then converting it to integer via atoi(). Perhaps its one of the quirks of scanf(). scanf() continuously scans the input buffer until it finds a space or nextline. From my testing, it also completely stops reading when it encounters a non-integer. Perhaps what it returns to the variable is undefined.

You could just have a single gets() in there then initially check for 'EXIT' then proceed if false. Also, fflush() is only for output streams. Using it on input streams is undefined. If you want to clear the buffer, just use getchar() but only if you're sure there's something in there. This problem is averted when you use string inputs instead of scanf()(fgets, gets).

Also, running the code I couldn't tell which was from gets() or scanf() which sort of bothered me. lol

can you maybe translate that into a code since I don't really get what you're saying. sorry I'm new to this :(

Don't use gets() Here's why.

What he's saying is
1) Input the information as a string with

fgets()

2) Test each character to make sure it's all digits or 'EXIT'
3) If all digits, convert to integer with

atoi()

can you maybe translate that into a code since I don't really get what you're saying. sorry I'm new to this :(

Try it. Don't ask us to do it for you. You learn more when you do it.

check out the isalpha() and the isdigit() functions from the ctype library.

store user input in string variable...

and check every character of string..

for example if user enters "EXIT" ( without quotes)

then char *str will be "EXIT";

now,

if(strlen(str)==4 && str[0]=='E' && str[1]=='X' && str[2]=='I' && str[3]=='T')
{
///// code
}


i tried and it works.......

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.