for 循环,允许变量相互覆盖
For loop that allows for variables to overwrite each other
我的问题是我如何添加到我的for循环中,以便在它进行多次迭代时,变量会相互覆盖?
我正在尝试获取一个具有三个共享的输入文本文件,如下所示:
*谷歌(GOOG)
522.01 2 100
520.66 1.5 80
苹果 (AAPL)
389.27 2 150
401.82 1.8 150
Microsoft (MSFT)
25.06 2.5 100
25.07 2 80*
然后稍后将其打印/写入输出文本文件。
现在,当它运行其迭代 3 次时,它只显示库存 1 及其正确的数字,但随后显示错误的数字,并且没有其他数字的名称。那么我将如何去做,以便下一次迭代将覆盖并打印另一组答案呢?
以下是我拥有的 for 部分代码:
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;
}
我得到什么作为输出
我需要得到什么
变量只要
不是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;
相关文章:
- C++ 成员变量被非成员函数覆盖
- 使用CIN时,字符串变量不会被覆盖
- 通过使用全局变量的函数访问在给定范围内被覆盖的变量
- 每个方法调用上都覆盖了本地变量指针
- for 循环,允许变量相互覆盖
- C 的变量覆盖
- C 覆盖成员变量
- 覆盖运算符<< 对于控制台输出不适用于成员变量
- C 多重继承,虚拟方法覆盖问题和协变量返回类型
- 提取运算符 (>>) 可以覆盖变量吗?
- C++ 在超类构造函数中运行依赖于子类覆盖的大量变量的代码的正确方法是什么?
- 如何强制 gcc 查找自己的 c++ 头文件,而不覆盖 CXX 变量
- C++ fscanf() 返回 -1 并且不覆盖变量
- 为什么引用成员变量会相互覆盖
- 变量赋值覆盖其他变量的值
- c++变量在每次循环迭代期间被覆盖
- 使用new时将覆盖变量
- 为什么作用域解析对覆盖的变量不起作用?
- 模板类函数中的静态变量被全局静态变量覆盖
- c++ std条件变量覆盖了很多共享变量