Pregunta

I have small amount of data in a file that looks like

New Mexico 50.9 10

this repeats with every state and whenever i hit states with two words for their title my program says pretty much says ooops we'll put first word in your string but the second one has no storage for it. With that it discontinues taking the rest of the data once that double worded title is encountered. Is there a way to take both words and put them in one string when reading my file in?

#include <iostream>
#include <string> 
#include <fstream>
#include <cstring>
using namespace std;

struct AccountsDataBase{

    string stateName;
    double miles;
    int rests;

};

#define MAX 80

AccountsDataBase * account = new AccountsDataBase[MAX];


int readIn(ifstream& file){
    int count=0;    

    file >> account[count].stateName;
    file >> account[count].miles;
    file >> account[count].rests;


while( !file.eof() && count<MAX){

    count++;
    file >> account[count].stateName;
    file >> account[count].miles;
    file >> account[count].rests;




}    

    return count;
}




int main(){

   ifstream file;
   file.open("input.txt"); //opens data account records text

   if ( !file.fail() ) {
       int cnt = readIn(file);


       delete[] account;
   }


return 0;

}
¿Fue útil?

Solución

Your question is certainly vague. However, here's one way of doing that:

std::ifstream ifile("filename_and_path"); //Requires <fstream>

//check to see if the file is open or not:
if (!ifile.is_open()) {
    std::cerr << "Something went wrong!" << std::endl;
    exit(1);//stop program execution. Requires <cstdlib>
}

std::string temp;
std::string state;
std::vector <std::string> tokens; //Requires <vector>

//std::getline requires: <string>
while(std::getline(ifile, temp)) {
    std::istringstream iss(temp);//initialize the stream to the contents of the line

    //keep parsing over the stream into tokens separated by ' ' (space) characters
    while(std::getline(iss, temp, ' ')) {
        //store all the tokens:
        tokens.push_back(temp);
    }

    //UPDATED to read ALL states. (I misread the question.)
    //we know that the last two parameters are always numbers, so use this
    //to our advantage:

    //if an even number, then we have two words, get and concatenate them:
    if (tokens.size() % 2 == 0) {
        state = tokens[0] + " " + tokens[1];
    }
    else {
        //this is an odd number of parameters. This means that this is a state
        //with one word (e.g.: Maryland)
        state = tokens[0];
    }

    //this is the end of one line, might as well print out the state name:
    std::cout << state << std::endl;
    state.clear();//empty the string for the next iteration
    tokens.clear();//empty the tokens for the next iteration
}

Otros consejos

You can use std::vector to store all std::string tokens in each line then use iterator to read the value. This solution shall work for general state names with any length not just one with two-word like New Mexico as long as the last two tokens of each line in the file represents double and int value.

int readin(const ifstream& file)
{
  ...

  string val;  
  vector<string> v;

  while (val = file.get() )
  {
     v.push_back(val);     
  }

  //assign concatentaion from element 1st to nth-2
  for(vector<string>::iterator it = v.begin(), it != v.end()-2;it++)
     account[count].stateName += *it + " ";  

  //assign element nth -2
  account[count].miles = atof(*(v.end()-2).c_str());
  //assign element nth -1
  account[count].rests = atoi(*(v.end()-1).c_str());

 ...
}

ok everyone i asked my teacher for the answer and she said all i had to do was use getline() ;,D cracking me up i was about to apply 20 lines of code to solve a one line C function.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top