Gmenfan83
Gmenfan83

Reputation: 2595

Turn multiple input statements into a loop?

Ok, i have this function with 5 variables. score1-score5. Instead of repeating the prompt for user input 5 times in my code is there a way i can use a loop to store the user input and go through each iteration as well? This is homework but it doesn't really matter anymore since i have the program working fine, i would just like to clean up the code a bit. I cannot use an array here either. Thanks in advance for any suggestions.

void getScore(double &score1, double &score2, double &score3, double &score4, double &score5)
{   
    cout << "Enter a score: ";
    cin >> score1;
    cout << "Enter a score: ";  
    cin >> score2;
    cout << "Enter a score: ";  
    cin >> score3;
    cout << "Enter a score: ";  
    cin >> score4;
    cout << "Enter a score: ";
    cin >> score5;
    cout << "-------------------" << endl;

Upvotes: 0

Views: 1044

Answers (2)

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361802

Why don't you use std::vector<double> as:

std::vector<double> readScore(int count)
{
    std::vector<double> v;
    for (int i = 0; i < count ; ++i)
    {
        double score;
        std::cout << "Enter a score: ";
        std::cin >> score;
        v.push_back(score);
    }
    return v;
}

std::vector<double> scores = readScore(5);

If you cannot change the function parameters, and have to stick to 5 parameters,then you can do something like this:

void getScore(double &score1, double &score2, double &score3, double &score4, double &score5)
{
     //store the address of each parameter in an array of pointers
     double *input[] = {&score1,&score2,&score3,&score4,&score5};
     for(int i = 0 ; i < 5 ; ++i)
     {
          std::cout << "Enter a score : ";
          std::cin >> *input[i]; //it reads into parameters!
     }
}

In this solution, you first store the address of each parameter in an array of pointers, then you use the array of pointers in a loop, to read the scores. Since input[i] is an address of one of the parameter, depending on the value of i, so std::cin >> *input[i] will read user-input and store it at the same address where input[i] is pointing to, which is nothing but one of the parameter.

Upvotes: 3

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 154045

Without the user of a data structure holding the values you won't be able to use a loop. However, you can factor the actual reading into a function to avoid some level of code duplication:

bool getScore(double& score)
{
    std::cout << Enter a score: ";
    return std::cin >> score;
}

bool getScore(double &score1, double &score2, double &score3, double &score4, double &score5)
{
    return getScore(score1)
        && getScore(score2)
        && getScore(score3)
        && getScore(score4)
        && getScore(score5);
}

This example also adds some error checking: all inputs to a program need to be checked. Without this, you'll end up processing random data upon any input error.

Upvotes: 3

Related Questions