Checking input value using loop

Joined
Feb 13, 2023
Messages
4
Reaction score
0
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.
looping1.png
 
Joined
Feb 14, 2023
Messages
9
Reaction score
0
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() ;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

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.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top