C++ c++;只有第一行答案正确
我的程序有问题,它只打印正确的第一行答案,这里是我的.txt文件,第一行数字(3)是商店编号,第二行数字(5)是产品编号,其他行是产品价格C++ c++;只有第一行答案正确,c++,C++,我的程序有问题,它只打印正确的第一行答案,这里是我的.txt文件,第一行数字(3)是商店编号,第二行数字(5)是产品编号,其他行是产品价格 3 5 1.27 2.92 3.45 1.09 0.89 1.08 2.25 3.75 1.12 0.69 0.98 2.48 3.62 1.10 0.72 这是我的results.txt文件,里面是每行的所有结果 9.62 8.89 8.90 我的程序代码: #include <iostream> #include <fstre
3 5
1.27 2.92 3.45 1.09 0.89
1.08 2.25 3.75 1.12 0.69
0.98 2.48 3.62 1.10 0.72
这是我的results.txt文件,里面是每行的所有结果
9.62
8.89
8.90
我的程序代码:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
double dienos, results;
int shops;
ifstream fin ("duomenys1.txt");
fin >> shops;
for( int shop = 1; shop <= 3; shop++ ) {
results = 0;
fin >> dienos;
for ( int pricelist = 1; pricelist <= 5; pricelist++ ) {
double price;
fin >> price;
results += price;
}
cout << results << endl;
}
}
#包括
#包括
使用名称空间std;
int main()
{
双dienos,结果;
国际商店;
ifstream fin(“duomenys1.txt”);
fin>>商店;
对于(int shop=1;shop>dienos;
对于(int pricelist=1;pricelist>price;
结果+=价格;
}
我怀疑:
ifstream fin ("duomenys1.txt");
fin >> shops;
for( int shop = 1; shop <= 3; shop++ ) {
results = 0;
fin >> dienos;
ifstream-fin(“duomenys1.txt”);
fin>>商店;
对于(int shop=1;shop>dienos;
fin>>dienos
应该在您的循环之外
此外,两个循环都是硬编码的,而不是使用读取的值。我怀疑:
ifstream fin ("duomenys1.txt");
fin >> shops;
for( int shop = 1; shop <= 3; shop++ ) {
results = 0;
fin >> dienos;
ifstream-fin(“duomenys1.txt”);
fin>>商店;
对于(int shop=1;shop>dienos;
fin>>dienos
应该在您的循环之外
另外,两个循环都是硬编码的,而不是使用读取的值。您可以尝试以下代码:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
double dienos, results;
int shops;
ifstream fin("duomenys1.txt");
fin >> shops;
fin >> dienos;
for (int shop = 1; shop <= shops; shop++)
{
results = 0;
for (int pricelist = 1; pricelist <= dienos; pricelist++)
{
double price;
fin >> price;
results += price;
}
cout << results << endl;
}
}
#包括
#包括
使用名称空间std;
int main()
{
双dienos,结果;
国际商店;
ifstream fin(“duomenys1.txt”);
fin>>商店;
fin>>迪诺斯;
对于(int shop=1;车间价格;
结果+=价格;
}
你能试试这个代码吗
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
double dienos, results;
int shops;
ifstream fin("duomenys1.txt");
fin >> shops;
fin >> dienos;
for (int shop = 1; shop <= shops; shop++)
{
results = 0;
for (int pricelist = 1; pricelist <= dienos; pricelist++)
{
double price;
fin >> price;
results += price;
}
cout << results << endl;
}
}
#包括
#包括
使用名称空间std;
int main()
{
双dienos,结果;
国际商店;
ifstream fin(“duomenys1.txt”);
fin>>商店;
fin>>迪诺斯;
对于(int shop=1;车间价格;
结果+=价格;
}
cout 1.它应该做什么?2.它怎么不正确?它应该计算每一行摘要,第一行摘要是正确的,9.62,其他行是不正确的1.它应该做什么?2.它怎么不正确?它应该计算每一行摘要,第一行摘要是正确的,9.62,其他行是不正确的谢谢,这很有帮助你能告诉我如何计算所有三个答案吗?谢谢,这很有帮助。你能告诉我如何计算所有三个答案吗?这很有效,你能告诉我如何计算所有三个答案吗