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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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

 

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: 

 

PROBLEMS
OUTPUT
DEBUG CONSOLE
TERMINAL
Hansson,Măns[M]
Bössgränd
373,90, [M]
RAMDALA
Lindberg,0lliver[M]
Sandviken
710,76,[M]
DYLTABRUK
Johnsson, Eskil[M]
5.
Transcribed Image Text:PROBLEMS OUTPUT DEBUG CONSOLE TERMINAL Hansson,Măns[M] Bössgränd 373,90, [M] RAMDALA Lindberg,0lliver[M] Sandviken 710,76,[M] DYLTABRUK Johnsson, Eskil[M] 5.
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY