Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

C++ Read numbers from a file with characters

I want to be able to add all of the numbers from the input file to find the sum, but I am not sure how to really go about this? I could use a few pointers. This is what the input text file contains:

Mark Cyprus 21
Elizabeth Monroe 45
Tom McLaugh 82
Laura Fairs 3
Paul Dantas 102

This is what my code is so far

#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <stdlib.h>
#include <sstream>
using namespace std;

class Age
{
    //Create the variables
    string first_name, last_name;
    int age, sum_age, average_age;

public:
    bool isblank(const std::string& s)            
    {    //True if s is empty or only contains space and/or TABs.          
         return s.find_first_not_of(" ")==std::string::npos;            
    }
    void readFile ()            
    {   
          ifstream file;           
          file.open("input_age.txt");     
          string line;                
          // file opened?                
          if (! file) {                    
              cerr << "can’t open input file." << endl; 
              exit(EXIT_FAILURE);                
           }
          // getline is true for as long as end of file is not reached            
          while(std::getline (file,line)){                    
              if (! isblank(line)){                        
                   // now the variable string "line" contains the line content  
                   file >> first_name;
                   file >> last_name;
                   file >> age;              
              }                
          }                
          file.close();            
          }    

Is there a way to only catch the age instead of reading the names? I don't believe I actually need the names for this. So it might be best to delete those first_name and last_name variables, but I am unsure how to only read the numbers in the file.

question from:https://stackoverflow.com/questions/65839370/c-read-numbers-from-a-file-with-characters

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is a problem in:

while(std::getline (file,line)){                    
    if (! isblank(line)){                        
    // now the variable string "line" contains the line content  
    file >> first_name;
    file >> last_name;
    file >> age;              
}             

Once you complete the getline you already have the 3 values in variable line but you never use it. You are doing a second read... so you are ignoring half the lines in your input.

What you should do instead is:

while(file >> first_name >> last_name >> age)
{
    //do whatever you want to do with age in here
}

This loop will end if you are unable to read a sequence 2 strings and 1 number, either due to reaching the end of file or illformed input.

By reading like this you don't need the isblank method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...