программа останавливается при компиляции и чтении данных из файла в массив - PullRequest
0 голосов
/ 21 апреля 2020

Я написал эту программу, которая вычисляет общую сумму из файла, сохраняет ее в массив и сохраняет вдвое больше в соседнем индексе. Программа не выдает никаких ошибок при попытке компиляции, но ничего не отображает. Я думаю, что мой внутренний l oop не прав, но не уверен. Любая помощь приветствуется.

Вот что в файле

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



1 Ответ

0 голосов
/ 21 апреля 2020
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 name;

        getline(buffer,name,':');

        if (name == "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 (name == "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;
}
...