2012-03-21 52 views
0

私は完全に立ち往生しています。これは私の不完全なプログラムです。プログラムはthe.atで動かなくなったり、母音の行があるかどうかを判断したりします。私はプログラムを修正するためのアドバイスが必要です。どのようなプログラムが想定していることは、これらの行を含むテキストファイルを取る行うには:ここにあるhttp://tinypic.com/r/24fhbfb/5ファイルへのC++の読み込みと書き込み

:それはこのリンクでのように見えるので、私は、ファイルにいくつかの計算およびエクスポートを行うことになっています

 
C++ Programming is fun 
Winter is here, will it ever end? 
The style requirements document is so useful 
Functions really make programming easier 
Spring must be coming soon... 
I wish this #$##$$!!## program was done 
What comes after C++, D--? 

これまでの私のコード。私が間違っていることについての指針は、非常に高く評価されます。私は機能のすべてをしなければならない。

#include <iostream> 
#include <iomanip> 
#include <string> 
#include <fstream> 

using namespace std; 

void OpenFiles (ifstream& inFile, ofstream& outFile); 

void OutputDivider (ofstream& outFile, char symbol, int num); 

void SetFileMessage (ifstream& inFile, bool& fileFound, string& message); 

void OutputFileMessage (bool& fileFound, ofstream& outFile, string& message); 

void InitializeFileCounters (int& lineCounter, int& totalOfAs, int& characterTotal); 

void OutputHeading (ofstream& outFile, string information, string name, int width); 

string ReadALine (ifstream& inFile, int& lineLength); 

void InitializeLineCounters (int& vowelCounter, int& charactersOfA, int& blankCounter); 

void CalculateLineStats (ifstream& inFile, string readLine, int& vowelCounter, int& lineLength, 
         int& blankCounter, int& charactersOfA); 



//Header to display in output file 
const string INFORMATION = "CST 133 - Exercise 4"; 
const string MY_NAME = "Tom Hangler"; 

//Width of screen for dividers 
const int SCREEN_WIDTH = 110; 

int main(void) 
{ 
    ifstream fin; 
    ofstream fout; 

    bool fileFound; 

    string message; 
    string lineCopy; 

    int lineCounter; 
    int totalOfAs; 
    int characterTotal; 
    int vowelCounter; 
    int charactersOfA; 
    int blankCounter; 
    int lineLength; 

    //Open input and output text files 
    OpenFiles(fin, fout); 

    //Set message based on if file was found or not 
    SetFileMessage(fin, fileFound, message); 

    //Output message to text file if file was not found 
    OutputFileMessage(fileFound, fout, message); 

    if (fileFound) 
    { 
     InitializeFileCounters(lineCounter, totalOfAs, characterTotal); 

     OutputHeading(fout, INFORMATION, MY_NAME, SCREEN_WIDTH); 

     InitializeLineCounters(vowelCounter, charactersOfA, blankCounter); 

     ReadALine(fin, lineLength); 

     CalculateLineStats(fin, lineCopy, vowelCounter, lineLength, blankCounter, charactersOfA); 

     ReadALine(fin, lineLength); 

    } 

    return 0; 
} 

void OpenFiles (ifstream& inFile, ofstream& outFile) 
{ 
    inFile.open("LinesEx4.txt"); 
    outFile.open("Ex4Out.txt"); 
} 

void OutputDivider (ofstream& outFile, char symbol, int num) 
{ 
    //Ouput to text file a divider with symbol '-' and specified length in 'num' 
    outFile << setfill(symbol) << setw(num) << symbol << endl; 
} 

void SetFileMessage (ifstream& inFile, bool& fileFound, string& message) 
{ 
    if (!inFile) 
    { 
     //Set message string to file not found. 
     message = "Input file was not found."; 

     //Set the file found flag 
     fileFound = false; 
    } 
    else 
    { 
     //Set the file found flag 
     fileFound = true; 

     //Set message string to file not found. 
     message = "Processing continues, file successfully opened."; 
    } 
} 

void OutputFileMessage (bool& fileFound, ofstream& outFile, string& message) 
{ 
    if (fileFound == false) 
    { 
     //Output divider to text file 
     OutputDivider(outFile, '-', SCREEN_WIDTH); 

     //Output message to text file. 
     outFile << message << endl; 

     //Output divider to text file 
     OutputDivider(outFile, '-', SCREEN_WIDTH); 
    } 
    else 
    { 
     //Output divider to text file 
     OutputDivider(outFile, '-', SCREEN_WIDTH); 

     //Output message to text file. 
     outFile << message << endl; 

     //Output divider to text file 
     OutputDivider(outFile, '-', SCREEN_WIDTH); 
    } 
} 

void InitializeFileCounters (int& lineCounter, int& totalOfAs, int& characterTotal) 
{ 
    //Initialize variables for total counting 
    lineCounter = 0; 
    totalOfAs = 0; 
    characterTotal = 0; 
} 

void OutputHeading (ofstream& outFile, string information, string name, int width) 
{ 
    outFile << '\n' << endl; 

    OutputDivider(outFile, '*', width); 

    outFile << setfill(' ') << setw((width + information.length())/2) << information << endl; 

    outFile << setfill(' ') << setw((width + name.length())/2) << name << endl; 

    outFile << "Line" << setw(50) << "Length" << setw(15) << "# of Vowels" << 
    setw(15) << "# of As" << setw(15) << "# of Blanks" << endl; 

    OutputDivider(outFile, '*', width); 
    } 

string ReadALine (ifstream& inFile, int& lineLength) 
{ 
    string readLine; 

    getline(inFile, readLine); 
    lineLength = readLine.length(); 

    return readLine; 
} 

void InitializeLineCounters (int& vowelCounter, int& charactersOfA, int& blankCounter) 
{ 
    //Initialize variables for line analysis 
    vowelCounter = 0; 
    charactersOfA = 0; 
    blankCounter = 0; 
} 

void CalculateLineStats (ifstream& inFile, string readLine, int& vowelCounter, int& lineLength, 
           int& blankCounter, int& charactersOfA) 
{ 
    int index; 

    for (index = 0; index < lineLength; index++) 
    { 
     if (readLine.at(index) == 'A' || readLine.at(index) == 'E' || readLine.at(index) == 'I' 
        || readLine.at(index) == 'O' || readLine.at(index) == 'U') 
     { 
      vowelCounter++; 
     } 
     if (readLine.at(index) == ' ') 
     { 
      blankCounter++; 
     } 
     if (readLine.at(index) == 'A') 
     { 
      charactersOfA++; 
     } 
    } 

}

答えて

1

ReadALine()の戻り値は格納されませんが、lineLength > 0からCalculateLineStats()に渡されます。これによりat()にはstd::out_of_range例外が発生し、呼び出し元にキャッチされず、プログラムが異常終了することになります。

変更:

lineCopy = ReadALine(fin, lineLength); 

別の変数にlineLengthを保存する理由はありません。

0

それはyoureの第二パラメータとしてあなたの関数CalculateLineStatsに空の文字列lineCopyを渡すように見えるし、その後.at()を呼び出します。この関数を呼び出す前に、その行を読む必要があります。

関連する問題