简体   繁体   中英

Regarding storing input into a string, and using cout to display it

I am writing a program that will request a user input of INT, and store it in an array of [10]. I want to be able to get the user to select the option DISPLAY and see all the data within the array. I just cannot figure it out, here is what I have so far :

case 2 : {
                 int SamtW;
                 cout << " Please enter how much you would like to withdraw "<< endl;
                 cin >> SamtW;
                 sa.doWithdraw(SamtW);
                 break;
             }

and here is the function that is being called above :

int saving:: doWithdraw(int amount)
{
    for (int i = 0; i < 10; i++)
{
     last10withdraws[amount];
    }
    if (amount > 1)
    {
    setBalanceW(amount);
    }
    else {
        cout << " ERROR. Number must be greater then zero. " << endl;
    }
    return 0;
}

I believe this will put the user input into the string last10withdraws. I then want the user to be able to call this function :

string saving::display()
{
    last10withdraws[10];
    return 0;
}

and this will hopefully display the contents of the array. Any ideas on what I am doing wrong?

last10withdraws[10];

This doesn't do anything. This takes the value of the 11th element of the array (which doesn't exist) and then throws it away.

Similarly this:

 last10withdraws[amount];

Take the value of an element of last10withdraws and throws it away. It doesn't assign it any value or store it anywhere.

I think you want:

int saving:: doWithdraw(int amount)
{
    if (amount > 0)
    {
        for (int i = 9; i != 0; i--)
        { // move the 9 elements we're keeping up one
            last10withdraws[i] = last10withdraws[i-1];
        }
        last10withdraws[0] = amount; // add the latest
        setBalanceW(amount);  // process the withdraw
    }
    else
    {
        cout << " ERROR. Number must be greater then zero. " << endl;
    }
    return 0;
}

OK From your comments:

First you need one additional variable in your saving called something like nr_of_withdraws . It will keep track of how many withdraws has been made. And it should be assigned zero when the class is constructed.

Then every time you insert in to last10withdraws you increment nr_of_withdraws . If nr_of_withdraws is greater than 9 then your array is full and you need to do something about it. So...


// Constructor.
saving::saving {
    nr_of_withdraws = 0;
}

// doWithdraw
int saving:: doWithdraw(int amount)
{
    // See if you have space
    if(nr_of_withdraws > 9)
       cout << "last10withdraws are done. Slow down."
       return 0;
    }
    // These lines. oh thy are needed.
    last10withdraws[nr_of_withdraws] = amount;
    nr_of_withdraws++;

    if (amount > 1)
    {
        setBalanceW(amount);
    }
    else {
        cout << " ERROR. Number must be greater then zero. " << endl;
    }
    return 0;
}

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