2013-04-17 45 views
0

我正在编写一个程序,它将请求用户输入INT,并将其存储在[10]的数组中。我希望能够让用户选择DISPLAY选项并查看数组中的所有数据。我只是想不通,这里是我到目前为止有:关于将输入存储到字符串中,并使用cout来显示它

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

这里是正在调用的函数上面:

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; 
} 

我相信这将会把用户输入到字符串last10withdraws。然后我希望用户能够调用此函数:

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

并且这将有望显示数组的内容。关于我做错什么的想法?

+0

什么的在'doWithdraw'循环的目的是什么?它现在没有意义。 – stardust

+0

我希望它把int amount的参数放入数组last10withdraws中。 – Dolbyover

+0

这并非如此,但可以等待。 **你想放哪?在整个阵列?我的意思是阵列中的所有10个位置?** – stardust

回答

1
last10withdraws[10]; 

这什么都不做。这需要数组的第11个元素的值(不存在),然后将其丢弃。

同样这样的:

last10withdraws[amount]; 

last10withdraws一个元素的值并抛出它扔掉。它不会为其分配任何价值或将其存储在任何地方。

我想你想:

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; 
} 
0

OK从您的评论:

首先,你需要在你的saving称为像nr_of_withdraws一个额外的变量。它将跟踪已经制造了多少withdraws。在课程结束时它应该被分配到零。

然后每次插入到last10withdraws时,您都会增加nr_of_withdraws。如果nr_of_withdraws大于9,那么你的数组已满,你需要做些什么。所以......


// 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; 
} 
+0

好的我继续把它放在我的代码中,但我仍然在调用显示函数时遇到问题。我将如何能够调用它并让它显示数组中的所有数据? – Dolbyover

相关问题