0

I wrote this code to open a file and store everything into a global char array team [800]

void readfile(char usrinput[]) // opens text file
{
    char temp;
    ifstream myfile (usrinput);
    int il = 0;
    if (myfile.is_open())
    {
      while (!myfile.eof())
      {
        temp = myfile.get();
        if (myfile.eof())
        {
          break;
        }
        team[il] = temp;
        il++;
      }
      myfile.close
    }       
    else
    {
      cout << "Unable to open file. (Either the file does not exist or is formmated incorrectly)" << endl;
      exit (EXIT_FAILURE);
    }
    cout << endl;
}

The user is required to create a input file that is formatted where the first column is a name, second column is a double, and third column is also a double. Something like this:

Trojans, 0.60, 0.10
Bruins, 0.20, 0.30
Bears, 0.10, 0.10
Trees, 0.10, 0.10
Ducks, 0.10, 0.10
Beavers, 0.30, 0.10
Huskies, 0.20, 0.40
Cougars, 0.10, 0.90

I want to add a check currently, where if the user only enters 7 teams, it with exit out of the program, or if the user enters more than 8 teams, or double numbers.

Ive tried creating an if statement using a counter (counter != 8 and you break out of the loop/program) in another function where I split this into three different arrays but that did not work. I am now trying to accomplish this check within this function and if its possible could someone guide me in the right direction? I appreciate all the help, and please let me know if i can provide more information to make things less vague.

EDIT: we are not allowed to use vectors or strings

1 Answers1

0

I would recommend switching to a vector instead of an array, and getting a line at a time using getline. Also I'm not sure how you're returning the data from the file in your code.

pseudocode:

void readfile(char usrinput[], std::vector<string>& lines) // opens text file
{
    ifstream myfile (usrinput);
    if (!myfile.good()) {
      cout << "Unable to open file. (Either the file does not exist or is formmated incorrectly)" << endl;
      exit (EXIT_FAILURE);
    }

    std::string line;
    while (myfile.good()) {
      getline(myfile, line);
      lines.push_back(line);
    }
    myfile.close();

    // it would be safer to use a counter in the loop, but this is probably ok
    if (lines.size() != 8) {
      cout << "You need to enter exactly 8 teams in the file, with no blank lines" << endl;
      exit(1);
    }
}

Call it like this:

std::vector<string> lines;
char usrinput[] = "path/to/file.txt";
readfile(usrinput, lines);

// lines contains the text from the file, one element per line

Also, check this out: How can I read and parse CSV files in C++?

Community
  • 1
  • 1
Avi Tevet
  • 778
  • 1
  • 7
  • 13
  • oh my bad i forgot to mention we are not allowed to use vectors – user3255966 Feb 22 '14 at 06:57
  • In that case, I would recommend creating lines as an 8 element array of fixed size char[] instead of a vector (`char lines[8][100]`). Then, use a counter in the loop, maybe something like `for (int i = 0; i < 8 && myfile.good(); ++i)`. Also, check out istream::getline (http://www.cplusplus.com/reference/istream/istream/getline/) and strcpy (http://www.cplusplus.com/reference/cstring/strcpy/). – Avi Tevet Feb 22 '14 at 07:07
  • yeah i think that would work, im just not sure ho to implement that into my current code to store everything into team[il] – user3255966 Feb 22 '14 at 07:29
  • Do you have to use `char team[800]`? In terms of storing data from a CSV it's pretty much the most primitive method you have. Since you aren't allowed to use vectors I assume you can't use any STL other container like queue/list/etc. And since you can't use std::string you have to use char arrays. So a 2 dimensional array like I described above is probably your best option because it gives you exactly one line in each element, which simplifies your code later on when you need to get the team name & numbers out of the line. – Avi Tevet Feb 22 '14 at 07:42