Home > Net >  Negative number input help-how-to
Negative number input help-how-to

Time:09-07

// 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.

CodePudding user response:

you could make a inputPositive function that reads the input and sets a boolean to true if the input number was negative:

void inputPositive(int& number, bool& negative) {
    std::cin >> number;
    if (number < 0) {
        negative = true;
    }
}

then you can call these functions for the 4 numbers. If negative flag is set we say continue; to return the start of the while(true):

while (true) {

    std::cout << "Indtast fire point-tal mellem 0 og 100: ";

    bool negative = false;

    int number = 0;
    int number2 = 0;
    int number3 = 0;
    int number4 = 0;

    inputPositive(number, negative);
    inputPositive(number2, negative);
    inputPositive(number3, negative);
    inputPositive(number4, negative);

    if (negative) {
        std::cout << "Invalid. Input only positive numbers\n\n"; 
        continue;
    }

    //...
}

output:

Indtast fire point-tal mellem 0 og 100: 10 20 30 -40
Invalid. Input only positive numbers

Indtast fire point-tal mellem 0 og 100: 10 20 30 40

Gennemsnittet af antal point: 25

you should use std::array for your function. It will shorten your code length, and allows to use features like std::accumulate which calculates the sum:

bool negative = false;

std::array<int, 4> numbers;
for (auto& i : numbers) {
    inputPositive(i, negative);
}
        
auto sum = std::accumulate(numbers.begin(), numbers.end(), 0) / numbers.size();
  •  Tags:  
  • c
  • Related