我编写了该程序,该程序从文件计算总量并将其存储到数组,并在相邻索引中存储总数的两倍。该程序在尝试编译时不会给出任何错误,但不会显示任何内容。我认为我的内循环是错误的,但不确定。任何帮助将非常感激。
这里是文件中的内容
John Wilder: 33 44 45 80
Ron Carter: 27 14 55 23
John Wilder: 1 23 34 55
Ron Carter: 22 34 53 43
Test Line: 32 34 24 22
这是我编写的代码
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void storeArray(ifstream & file)
{
int storeJohn[10] = {0};
int storeRon[10] = {0};
int totalNum = 0, eachNum = 0, counterJohn = 0, counterRon = 0;
string firstLast; //store first name and last name
//Logic
while (!getline(file,firstLast,':').eof()) //Read the text from each line until it find ":"
{
if (firstLast == "John Wilder")
{
while (file >> eachNum) //runs until end of line
{
totalNum += eachNum; //adds all the numbers
}
storeJohn[counterJohn] = totalNum; //Store the total
storeJohn[counterJohn + 1] = totalNum *2; //store twice the total
totalNum = 0; //resetting for next line
eachNum = 0; //resetting for next line
++counterJohn;
if (counterJohn >=10)
{
continue; //Go to next line if the size is reached
}
}
else if (firstLast == "Ron Carter")
{
while (file >> eachNum) //runs until end of line
{
totalNum += eachNum;
}
storeRon[counterRon] = totalNum; //store the total
storeRon[counterRon+ 1] = totalNum *2; //store twice the total
totalNum = 0; //resetting for next line
eachNum = 0; //resetting for next line
++counterRon; //After each loop counter goes up by one
if (counterRon >= 10)
{
continue; //Go to next line if the size is reached
}
}
else
{
continue;
}
}
for (int i = 0; i<10; i++)
{
cout <<storeJohn[i] <<" "; //Displaying the array
}
cout <<endl;
for (int i = 0; i <10; i++)
{
cout <<storeRon[i] <<" "; //displaying the array
}
cout <<endl;
}
int main()
{
ifstream file;
file.open("records.txt");
storeArray(file);
file.close();
return 0;
}
void storeArray(ifstream & file)
{
int storeJohn[10] = {0};
int storeRon[10] = {0};
int totalNum = 0, eachNum = 0, counterJohn = 0, counterRon = 0;
string firstLast; //store first name and last name
//Logic
while (!getline(file,firstLast).eof()) //Read the text from each line until it find ":"
{
istringstream buffer(firstLast);
string nom;
getline(buffer,nom,':');
if (nom == "John Wilder")
{
while (buffer >> eachNum) //runs until end of line
{
totalNum += eachNum; //adds all the numbers
}
storeJohn[counterJohn] = totalNum; //Store the total
storeJohn[counterJohn + 1] *= totalNum; //store twice the total
totalNum = 0; //resetting for next line
eachNum = 0; //resetting for next line
++counterJohn;
}
else if (nom == "Ron Carter")
{
while (buffer >> eachNum) //runs until end of line
{
totalNum += eachNum;
}
storeRon[counterRon] = totalNum; //store the total
storeRon[counterRon+ 1] *= totalNum; //store twice the total
totalNum = 0; //resetting for next line
eachNum = 0; //resetting for next line
++counterRon; //After each loop counter goes up by one
}
else
{
continue;
}
}
for (int i = 0; i<10; i++)
{
cout <<storeJohn[i] <<" "; //Displaying the array
}
cout <<endl;
for (int i = 0; i <10; i++)
{
cout <<storeRon[i] <<" "; //displaying the array
}
cout <<endl;
}