2017-10-10 32 views
0

我的問題是我如何添加到我的for循環,以便當它進行多次迭代時,變量將相互覆蓋?For循環允許變量相互覆蓋

我試圖把一個輸入文本文件與三元股份像這樣:

*谷歌(GOOG)

522.01 2 100

520.66 1.5 80

蘋果公司(AAPL)

389.27 2 150

401.82 1.8 1 50

微軟(Microsoft)

25.06 2.5 100

25.07 2 80 *

再後來打印/寫到輸出文本文件。

現在,當它重複運行3次時,它只顯示股票1及其正確的數字,但是錯誤的數字和其他名稱都沒有。那麼,我將如何去做,以便下一次迭代將覆蓋並打印另一組答案?

下面

是對部分代碼,我有:

for (int x = 1; x <= 3; x++) // for loop to run this part of program 3 times 

{ 

    getline(dataIn, stockName);//Gets the whole first line 


    dataIn >> buyingAMT;//These just store whatever is in the line before every space, and sets it with a name 
    dataIn >> buyingComm;//So like it takes the characters leading up to the first space and stores it as buyingAMT. 
    dataIn >> numberBought; 
    dataIn >> sellingAMT; 
    dataIn >> sellingComm; 
    dataIn >> numberSold; 



    //writing to a file 


    buyingComm = buyingComm * buyingAMT;//Mathematical Calculations 

    buyingAMT = buyingAMT * numberBought; 

    sellingComm = (sellingComm/100) * numberSold; 

    sellingComm = sellingComm * sellingAMT; 

    sellingAMT = sellingAMT * numberSold; 

    profit = (sellingAMT - sellingComm) - (buyingAMT + buyingComm); 

    //Displaying the calculated answers 
    fout << setw(20) << left << stockName; 

    fout << setprecision(2) << fixed << setw(15) << right << buyingAMT; 

    fout << setprecision(2) << fixed << setw(15) << right << buyingComm; 

    fout << setprecision(2) << fixed << setw(15) << right << sellingAMT; 

    fout << setprecision(2) << fixed << setw(15) << right << sellingComm; 

    fout << setprecision(2) << fixed << setw(15) << right << profit << endl; 

    //Storing the results into the overall variables to be used later 
    /*totalBuyingAMT += buyingAMT; 
    totalBuyingComm += buyingComm; 
    totalSellingAMT += sellingAMT; 
    totalSellingComm += sellingComm; 
    grandProfit += profit; 
    */ 

} 
getline(dataIn, junk);//Cleans the remaining unread data 

dataIn.close();//closes the input file, so it cant be read any longer 

fout.close(); 

cout << "congrats you are either now screwed or rich!"; 

return 1; 

}

what I get as output

what i need to get

+1

這可以幫助你。 https://stackoverflow.com/questions/20080255/mixing-formatted-input-extractors-with-getline-causes-cout-display-to-stick-toge P.S.請刪除C#標籤,它們不是一回事。 –

+0

從'main'返回非零表示失敗。 –

回答

1

變量,只要它們不const總是可以重寫或複製。從代碼中看起來像是如何提取文本的問題。

dataIn >> buyingAMT;//These just store whatever is in the line before every space, and sets it with a name 
dataIn >> buyingComm;//So like it takes the characters leading up to the first space and stores it as buyingAMT. 
dataIn >> numberBought; 
dataIn >> sellingAMT; 
dataIn >> sellingComm; 
dataIn >> numberSold; 

那麼什麼樣的類型dataIn和其他變量呢?好像你沒有正確地分割文本,或者沒有辦法從dataIn中做到這一點。

這是一條線從標準輸入取(雖然它可以是任何流)的一個小例子

#include <iostream> 
#include <string> 

int main() { 
    int i; 
    std::string input; 
    for(i=0;i<3;i++) 
    { 
    std::cout << "Please give me some input:"; 
    std::cin >> input; 
    std::cout << "You gave me:" << input << std::endl; 
    } 
} 

ofstream fout;//data type used to write the data taken from input file and put it on output file 
fout.open(output_File_Name); 

if (fout.fail()) 
{ 
    fout << "Input file not found: " << output_File_Name << endl; 
    system("pause"); 
    return 0; 
} 

fout << setw(20) << left;//Setting the labels 
fout << "STOCK"; 

fout << setw(15) << right; 
fout << "BUYING AMT"; 

fout << setw(15) << right; 
fout << "BUYING COMM"; 

fout << setw(15) << right; 
fout << "SELLING AMT"; 

fout << setw(15) << right; 
fout << "SELLING COMM"; 


fout << setw(15) << right; 
fout << "PROFIT" << endl; 
+0

我有dataIn as ifstream dataIn。其他變量也是雙變量。 –

+0

是每個股票名稱,它的價值都在一條線上,並且每個股票都是連續的線? –

+0

你的意思是當它被寫入文件或讀取?如果你的意思是書面的,那麼每一行的股票名稱都不會下降。每個值雖然在purchaseAMT,BuyingComm等方面都在一行 –