# Checking input value using loop

#### kyalzin

I want to write a code for evaluating average result of exam marks. In my program, I want to include one function which will repeatedly ask for the correct range of marks defined by the principles rather the impossible. In this case, marks mustn't be smaller than zero or greater than 100. However if a user types a character accidentally, the code will keep on going. That is why I try to fix mine to actively respond when a letter or whatever is entered. But, the looping is somewhere at one point must be wrong, since I keep getting the result of infinite loop. That is why I am here asking for some help in guiding me. Thank you so much for your help. #### fluffycorleone

What is 'not_float' defined as?

#### kyalzin

What is 'not_float' defined as?
I want to do something like keeping a loop until a number is entered instead of a character. The problem is I don't know how to do it. So, I am just testing if it works.

#### fluffycorleone

1) If you're new to coding then you might want to look into python as its syntax is much easier to read/follow.

I want to write a code for evaluating average result of exam marks.

2) So the average of something is as follows: (n1 + n2 + n3) / 3; the sum of all the numbers divided by the amount of numbers there are.

So it sounds like you want a program that asks the user for a series of numbers and returns the average.

I want to include one function which will repeatedly ask for the correct range of marks defined by the principles rather the impossible.

In this case, marks mustn't be smaller than zero or greater than 100.

3) The function asks for a specified range in a loop. The loop will keep going as long as the user inputs an appropriate number. If the user accidentally enters a letter then an error message comes up. Is this accurate?

Here is how I would do it:

Code:
``````#include <stdio.h>

void my_function() {

float result = 0.0f ;

while( result >= 0 && result <= 100 ) { // This loop will keep going as long as 0 <= result <= 100

printf( "Enter exam result (0-100): ") ;
scanf( "%f", &result ) ;  // %d for whole numbers. %f is for floats (decimal numbers)

printf( "You entered: %f\n", result ) ;
}
}

int main() {

my_function() ;
}``````

4) I'm mainly a c++ developer so that's what I'm familiar with. Here is how you would handle errors (if the user entered a letter instead of a number)

Code:
``````#include <iostream>

void my_function() {

float result = 0.0f ;

while( result >= 0 && result <= 100 ) { // This loop will keep going as long as 0 <= result <= 100

std::cout << "Enter exam result (0-100): " ;
std::cin >> result ;

if (std::cin.fail()) {  // If user entered a letter (error) the following block executes
std::cout << "You did not enter a number!!\n" ;
break ;
}

std::cout << "You entered: " << result << '\n' ;
}
}

int main() {

my_function() ;
}``````

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

### Members online

No members online now.