简体   繁体   中英

C++ code runs forever and eats memory

It seems to be in MakeData function, as that is what breaks the execution. I am very unsure as to why this is not working, as my instructor and many of my classmates have almost identical execution and it is fine. I know for a fact that an almost identical version of this code, with windows file name, also does not run on windows. I have compiled the code. I have run debuggers. Nothing turns up. The debuggers I have run have just run the code until either very obscure errors turn up or it essentially indicates that the process is in some kind of an infinite loop. Any help would be appreciated!

/*
 *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);
     }
  }
}

The comment made by Xin Huang was correct. It turns out the use of getline and cin resulted in some form of infinite loop that would eat memory, until the computer would eventually crash. I still have no idea why there is no solution to this, or why using cin and getline together could have such terrible consequences, especially considering there were really no error codes. Even still, replacing cin with getline in the getData function and then converting back to integer yields a clean program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM