2017-01-31 12 views
-2

それはMakeData関数内にあるようですが、これは実行を中断させるためです。私はインストラクターと私のクラスメートの多くがほぼ同じ執行力を持っているので、これがうまくいかない理由は非常に分かりません。私は、Windowsファイル名を持つこのコードとほとんど同じバージョンがウィンドウ上で実行されないという事実を知っています。私はコードをコンパイルしました。私はデバッガを実行しました。何も表示されません。私が実行したデバッガは、非常に不明確なエラーが出るか、またはプロセスがある種の無限ループにあることを示すまで、コードを実行しています。どんな助けもありがとう!C++コードが永久に実行され、メモリを食べる

/* 
*Program Description:A program to sort a series of strings and scores from a file. 
* 
*Programmer:Timothy A. Gass 
*Date:01/17/17 
*/ 

#include <iostream> 
#include <string> 
#include <math.h> 
#include <fstream> 
#include <vector> 
#include <ctime> 

using namespace std; 

void makeData(string); 
void getData(vector<string> &, vector<int> &, string); 

int main(){ 
    srand(time(0)); 
    string fname = "/home/tim/dev/c++/chpt9/data.txt"; 
    vector<string> name; 
    vector<int> score; 
    makeData(fname); 
    getData(name, score, fname); 
    for(int i = 0; i < score.size(); i++){ 
    cout << score[i] << endl; 
    cout << name[i] << endl; 
    } 
    cout << "Press enter to exit." << endl; 
    cin.ignore(); 
    cin.get(); 
    return 0; 
} 

void makeData(string fname){ 
    int rand1, rand2, rand3; 
    const int SCORE_MAX_SIZE = 100; 
    ofstream make(fname); 
    const int PEOPLE_NUM = 50; 
    vector<string> firstNames = { 
    "Gus", 
    "Taneka", 
    "Shane", 
    "Rosella", 
    "Bennett", 
    "Filiberto", 
    "Khadijah", 
    "Mafalda", 
    "Rusty", 
    "Janiece", 
    "Shavonne", 
    "Azalee", 
    "Enedina", 
    "Heidy", 
    "Lavelle", 
    "Darleen", 
    "Ashton", 
    "Glynis", 
    "Gale", 
    "Norene", 
    "Madaline", 
    "Elvin", 
    "Jacqueline", 
    "Kristofer", 
    "Zachary", 
    "Lorretta", 
    "Jim", 
    "Shanelle", 
    "Tonja", 
    "Alethia", 
    "Kasha", 
    "Katheleen", 
    "Joyce", 
    "Kirstin", 
    "Neil", 
    "Belkis", 
    "Maisha", 
    "Doretha", 
    "Eliseo", 
    "Rhiannon", 
    "Annamarie", 
    "Latoria", 
    "Jerica", 
    "Betsey", 
    "Delinda", 
    "Pamula", 
    "Porsha", 
    "Fredia", 
    "Wilda", 
    "Belen" 
    }; 

    vector<string> lastNames = { 
    "Best", 
    "Shields", 
    "Finley", 
    "Blankenship", 
    "Hobbs", 
    "Nichols", 
    "Mcneil", 
    "Robles", 
    "Moyer", 
    "Hays", 
    "Elliott", 
    "Ruiz", 
    "Ritter", 
    "Gamble", 
    "Zamora", 
    "Cole", 
    "Larson", 
    "Ibarra", 
    "Choi", 
    "Santana", 
    "Gray", 
    "Crane", 
    "Campos", 
    "Wright", 
    "Morris", 
    "Flores", 
    "Newman", 
    "Santos", 
    "Li", 
    "Archer", 
    "Chavez", 
    "Avery", 
    "Mora", 
    "Liu", 
    "Lutz", 
    "Miles", 
    "Stewart", 
    "Austin", 
    "Wu", 
    "Turner", 
    "Brennan", 
    "Ferrell", 
    "Mcmillan", 
    "Whitney", 
    "Odonnell", 
    "Conley", 
    "Maxwell", 
    "Stafford", 
    "Carlson", 
    "Peck" 
    }; 

    for(int i = 0; i < PEOPLE_NUM; i++){ 
    rand1 = rand()%50; 
    rand2 = rand()%50; 
    rand3 = rand()%(SCORE_MAX_SIZE+1); 
    make << firstNames.at(rand1) + " " + lastNames.at(rand2) << endl; 
    make << rand3 << endl; 
    } 
} 

void getData(vector<string> &name, vector<int> &score, string fname){ 
    ifstream get(fname); 
    string str; 
    int num; 
    if(get.fail()){ 
    cout << "File could not be opened!" << endl; 
    } 
    else 
    { 
    while(!get.eof()) 
    { 
     getline(get, str); 
     get >> num; 
     cin.ignore(); 
     name.push_back(str); 
     score.push_back(num); 
    } 
    } 
} 
+3

このような問題を解決する適切なツールは、デバッガです。スタックオーバーフローを尋ねる前に、コードを一行ずつ進める必要があります。詳しいヘルプは、[小さなプログラムをデバッグする方法(Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)を参照してください。最低限、問題を再現する[最小、完全、および検証可能](http://stackoverflow.com/help/mcve)の例と、その問題を再現するためのデバッガ。 –

+7

_「C++コードは永遠に実行されます」_どのように知っていますか? –

+5

@MikelF:なぜそれが '' // home // tim // def // C++ // chpt9 // data.txt "'でしょうか? –

答えて

0

Xin Huangのコメントは正しいです。 getlineとcinを使用すると、コンピュータが最終的にクラッシュするまでメモリを消費する無限ループが発生することが判明しました。なぜこのような解決策がないのか、あるいはcinとgetlineを一緒に使ってみると、実際にはエラーコードがなかったことを考えると、このようなひどい結果を招くのはなぜか分かりません。 getData関数でgetlineをcinに置き換えてからintegerに変換すると、きれいなプログラムになります。

関連する問題