简体   繁体   中英

How to Assign a value to an increasing Variable like an array but without using array

So I am making a calculator that could store a number inside a variable .But the thing is, the variable will increase if the user put an input 'y' for char compare .when the variable increases from 1 to 2 , the user can put a new input . I want to assign the new input to variable 2 . The variable could go ten times if the user put input 'y' for char compare ten times. It is like an array but I cannot use array for my project and I want to compare the number. I want the highest and the lowest number between the ten variables.

I tried to store the value, but then I don't know what to do after that.

char compare = 'Y';
int value = 1; // Value is a variable that increases
while (compare == 'y' || compare == 'Y') {
    cout << " enter value#" << value;
    cin >> num;
    cout << "do you want to compare?";
    cin >> compare;
    value++;
}

I expect the num will stick with the value and then compare it, but it seems that I cannot do it.

For example, user enter 100 for num and value 1, then user wanted to continue and put 300 for num and value 2.

Seems like you'd want an array like entity of variable size. In that case make use of std:vector and populate the 'num's in it.

Edit: The non usage of arrays was not mentioned earlier. Here's what you can do in that case:

char compare = 'Y';
int value = 1; // Value is a variable that increases
int num;
bool assigned = false;
int minNum = 0, maxNum = 0;
while (compare == 'y' || compare == 'Y') {
    cout << " enter value#" << value;
    cin >> num;
    if (!assigned || (num < minNum)) {
        minNum = num;
    }
    if (!assigned || (num > maxNum)) {
        maxNum = num;
    }
    if (!assigned)
        assigned = true;
    cout << "do you want to compare?";
    cin >> compare;
    value++;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM