// ConsoleApplication6.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <limits>
int getInput() {
while (true) {
std::cout << "Indtast fire point-tal mellem 0 og 100: ";
int number = 0;
int number2 = 0;
int number3 = 0;
int number4 = 0;
std::cin >> number;
std::cin >> number2;
std::cin >> number3;
std::cin >> number4;
int sum;
sum = (number + number2 + number3 + number4) / 4;
std::cout << "\n";
if (std::cin.eof()) {
std::cout << "Fejl, prøv igen!\n";
std::cin.clear();
continue;
}
if (std::cin.bad() || std::cin.fail()) {
std::cout << "Ugyldigt input (fejl i input af tal).\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
continue;
}
if (sum < 0 || sum > 100) {
std::cout << "Ugyldigt input, det skal være mellem 0 og 100!.\n";
continue;
}
return sum;
}
// unreachable
return 0;
}
int main() {
int sum = getInput();
std::cout << "Gennemsnittet af antal point: ";
std::cout << sum << std::endl;
if (sum >= 90) {
puts("Karakter: \n");
puts("12");
}
else if (sum >= 80) {
puts("Karakter: \n");
puts("10");
}
else if (sum >= 70) {
puts("Karakter: \n");
puts("7");
}
else if (sum >= 60) {
puts("Karakter: \n");
puts("4");
}
else if (sum >= 50) {
puts("Karakter: \n");
puts("02");
}
else {
puts("Karakter: \n");
puts("00");
}
}
Currently, my code processes negative inputs the same way as positive inputs. But I would like to implement an "if" that filters negative numbers and come back with an error and maybe also restart the loop. How would I do that?
getInput() retrieves 4 numeric inputs, with an error if letters are submitted as input and if the input isn't between 0-100 then it does the same.