0

Having trouble sorting the data in the input file into the designated array. The input file contains a string, double, and int.

  • TN 54.5 7
  • KY 65.6 23
  • PA 123.3 30
  • etc.

There are 14 lines of type string, double, int data types. The data is to be printed to screen in the same format as the file. I have been able to print the data to the screen using one 'string array' but the data does not print in the order specified above. I have tried using getline() and through research saw many talking of using simply 'input >> variable[max];' for each individual variable. This only prints a huge number and the number 53 niether of which are contained in the input file. I feel like i am making this harder than it is. I know that my array is to small to read the amount of data needed (that i plan to fix). Not asking for someone to figure this out for me. Just need to be pointed in the right direction. Is there an easier way to sort the data to desired array?

The code below is what i used with one array to read all data types.

#include <iostream>
#include <fstream>

using namespace std;

void readData(ifstream& input, string []);
int main()
{
   string data[14];
   char filename[256];
   fstream input;

   cout << "Enter file name: ";
   cint >> filename;

   input.open(filename);
   if (input.fail())
   {                                  
      cout << "opening file fail." << endl;
   }

   readData(input, data);

   input.close();
   return(0);
}             

void readData(ifstream& input, string data[14])
{
    int count;

    count = 0;
    while (count < 14 && !input.eof())
    {
       input >> data[count];
       count << data[count] << endl;
       count++;
    }
}                
4

2 回答 2

1

为什么不是一个循环?(在这里看到它)

#include <iostream>
#include <sstream>
#include <vector>

void read_data(std::istream& input, std::vector<std::string>& strings, std::vector<double>& doubles, std::vector<int>& ints) {
  std::string s; double d; int i;
  while( input >> s >> d >> i ) {
    strings.push_back(s);
    doubles.push_back(d);
    ints.push_back(i);
  }
}

int main() {
  std::istringstream i { "FN 3.2 22\nBB 3.48 48\nXX 2.03 172\n" };
  std::vector<std::string> strings;
  std::vector<double> doubles;
  std::vector<int> ints;

  read_data(i, strings, doubles, ints);
  std::cout << "strings:\n";
  for(auto s: strings) std::cout << "  " << s << "\n";
  std::cout << "doubles:\n";
  for(auto d: doubles) std::cout << "  " << d << "\n";
  std::cout << "ints:\n";
  for(auto i: ints) std::cout << "  " << i << "\n";
}
于 2014-04-09T23:02:33.223 回答
0
ifile.open(filename);

    if (ifile.is_open()){


        int indexValue = -1; 

        while ( ifile.getline(buffer, 100) ){


            indexValue++;
            counter = 0;


            token = strtok(buffer, " ");

             while (token){



        if(counter == 0){

            strcpy(stringArray[indexValue],token);
            cout<< "string " <<stringArray[indexValue] << endl; 

        }

        counter++;

        stringstream ss;
        int value =0;
        if(counter == 1){
            token = strtok(NULL, " ");
            ss << token;
            ss >> value;
            intArray[indexValue] = value;
            cout << "int " << intArray[indexValue] << endl;

        }
            }

    counter++;

    stringstream ss;
  double value1 =0.0;
    if(counter == 2){
      token = strtok(NULL, " ");
      ss << token;
      ss >> value;
      doubleArray[indexValue] = value1;
      cout << "Score " << doubleArray[indexValue] << endl;

    }
            token = strtok(NULL, " ");
        counter = 0;
            }
    }
于 2014-04-10T00:33:53.433 回答