If I have a textfile with three lines and i would change in the first two but the third line I want to read it how is written i the file. ex: Text file : FirstName LastName NationeltyNumber StreetName StreetNumber , Postcode CityName My task is to read this file by C++ program and the output should be : To change the place between FirstName and LastName and by algorithem change the nationeltyNumber to Femle or Male by reading before last digit if it's % 2 == 0 it's a female otherwise it's a Male and by that the second Line will be the Address. I tried to write a program but my program reading the Address as first and writing the streetName as femel or Male. My Task should be : LastName FirstName [M] StreetName StreetNumber , Postcode CityName
If I have a textfile with three lines and i would change in the first two but the third line I want to read it how is written i the file. ex:
Text file :
FirstName LastName
NationeltyNumber
StreetName StreetNumber , Postcode CityName
My task is to read this file by C++
To change the place between FirstName and LastName and by algorithem change the nationeltyNumber to Femle or Male by reading before last digit if it's % 2 == 0 it's a female otherwise it's a Male and by that the second Line will be the Address.
I tried to write a program but my program reading the Address as first and writing the streetName as femel or Male.
My Task should be :
LastName FirstName [M]
StreetName StreetNumber , Postcode CityName
But my program reading like that
LastName FirstName [M]
StreetName
Postcode [M]
CityName
How can I fix it and the line there StreetName should be a string .
My program :
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
//Using namespace
using namespace std;
//Defining main()
int main() {
// Declaring variables
string Firstnam, Lastnamn, NationeltyNumber, Gender, Address;
int lstdgt;
string mystring;
string line ;
//File object
ifstream infile("names.txt");
//Checking for file
if (!infile.is_open()) {
//if file not found
cout << "The file does not exists. Please check the names.txt file"<< endl ;
exit(1);
}
//Reading input file
while (infile >> Firstname >> Lastname >> NationeltyNumber>> Address) {
//Getting second last digit
NationeltyNumber = NationeltyNumber[8];
//Converting to int
lstdgt = stoi(NationeltyNumber);
//Finding gender
if(lstdgt % 2 == 0)
Gender = "[F]";
else
Gender = "[M]";
//Printing the output
cout << Lastname << "," << firstname << ""<< Gender << "\n" << endl;
cout << address << endl;
}
//Closing the file
infile.close();
return 0;
}
My output in the image:
Step by step
Solved in 3 steps with 1 images