samedi 27 novembre 2021

C++ Adresses line don't show up entirly when I try to read from file, Why?

I'm trying to read from one file which contains around 4000 fictional people. Looks like this in the reading file but with many more people following.

Justin Biber 
199403012345
London second street 12 33444, Miami

my task was to swap the names with the last name to look like this...

Lastname Firstname [M] //  Male  // Part one 
Bieber     Justin     // Part two 
London second street 12 33444, Miami  // part three

I have succeeded to solve part One and part Two but not part Three parts out of three. Some of the addresses from the reading file contain more lines and street names than I expected, I don't know how to handle it. For example, look at these two addresses examples...

Lastname Firstname [M] //  Male  // Part one 
Bieber     Justin     // Part two 
London second street 12 33444, Miami  // part three


Lastname Firstname [F] // female  // Part one
Roberts Julia  // Part Two
London second street 13 33676, lontime ev second  // Part Three 

As you can see the second address is longer. I guess my question is. How can I read the entire line no matter how long the addresses are?

This is my code...

Note: I have solved Part One and Two, However for the address part, I do not know how to get the whole line.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <climits>
#include <string>
#include <sstream> 
#include <algorithm>
#include <vector>

std::string swap_first_last_name(std::string first_name, std::string last_name){  // Swap function
    std::string swap_names{};
    swap_names = first_name;
    first_name = last_name;
    last_name = swap_names;
    return swap_names;
}

int main(){
 std::ifstream input; 
 std::ofstream output;  


 input.open("labb-2.txt");  // 
 if(!input){
  std::cerr << "The file failed to be loaded" << std::endl;
  // std::exit(EXIT_FAILURE);
return 1; // Exit the program
 }



  output.open("output.txt");  // skriv ut resultatet i en annan fil.
 if(!output){
  std::cerr << "The file failed to be loaded" << std::endl;
  // std::exit(EXIT_FAILURE);
return 1; // Exit the program
 }

// All my variables
std::string  first_name{}, last_name{};
const int field_width {10};
const int field1_width {5};
const int field2_width {20};
std::string persoonalnumber{};
std::string adress{};




while( input >> last_name >> first_name >> persoonalnumber >> adress) {
  // // look if the person is male or female. 
   char per =   persoonalnumber[ persoonalnumber.length() - 2];  // find the second last 
      character from the string
   std::string man_eller_kvinna =  (per % 2 == 1) ? "[M]" : "[K]";  // Tenary operator  ? :
   output << std::setw(field2_width) << std::left <<  "Lastname," 
   << std::setw(15) << "Firstname" << std::setw(field_width)  <<  man_eller_kvinna;

  // output << std::setw(field2_width)<< std::left <<  persoonalnumber<< std::endl; 

 output << std::endl;  // new line

  // Swap the first_name with the last_name
  output << std::setw(field2_width)<< std::left << first_name
  << std::setw(field1_width)  << last_name << std::endl;
  swap_first_last_name(last_name, first_name);  // call the function
  // first_name.swap(last_name);
  output << std::endl;


// Part three addresses, I don't know what to do! 


   output << std::endl;


      
          
           
}

 std::cout << std::endl;  // Give me new lines with finish message 
 std::cout << "File transformation is Done!" << std::endl;
 std::cout << std::endl;

 input.close();
 output.close();

 return 0;
}


Aucun commentaire:

Enregistrer un commentaire