for循环允许变量相互覆盖

时间:2017-10-10 04:10:33

标签: c++

我的问题是如何添加到我的for循环中,这样当它进行多次迭代时,变量会相互覆盖?

我正在尝试使用三个共享输入文本文件,如下所示:

* Google(GOOG)

522.01 2 100

520.66 1.5 80

Apple(AAPL)

389.27 2 150

401.82 1.8 150

Microsoft(MSFT)

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 个答案:

答案 0 :(得分: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中做到这一点。

这是一个小例子,其中一行是从stdin中获取的(虽然它可以是任何流)

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