Program fails to initialize and display values entered to struct

Hello programmers. I would like the below program to initialize the struct below at runtime and then display the values entered.

I have two functions as:

EnterDetails () -----------this should take the values entered and initialize the struct.
PrintEnteredDetails () ---------should display the details entered .

At first i wrote an overloaded constructor (you can ignore this constructor or advise appropriately) to initialize the struct but when it failed i decided to write the EnterDetails () function.


The problem is, the program does not display the entered values.

Kindly correct my mistakes.


The following is the program:



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



struct LandDetails { // declaring a struct

string Surname;
string OtherNames;
string LandNum;
string KraPin;
string OwnerIdCard;


string address;

string LandCounty;
string LandDist;
string LandDiv;
string Landloc;
string LandSubloc;










};


void EnterDetails (LandDetails &enter)

{ // i doubt whether this function is initializing the struct above ----may there is problem here
//function to instantiate the struct using values passed at runtime


enter.Surname ="";

enter.OtherNames ="";

enter.LandNum ="";

enter.KraPin ="";

enter.OwnerIdCard ="";;

enter.address ="";

enter.LandCounty ="";

enter.LandDist ="";

enter.LandDiv ="";

enter.Landloc ="";

enter.LandSubloc="";




}

void PrintEnteredDetails (LandDetails &show );



void main ()
{

string Surname;
string OtherNames;
string LandNum;
string KraPin;
string OwnerIdCard;


string address;

string LandCounty;
string LandDist;
string LandDiv;
string Landloc;
string LandSubloc;


LandDetails show;
LandDetails enter;







cout<<"Welcome to the new lands management information system\n\n";

{
cout<<"Enter Surname\n";
getline (cin,Surname);

cin.clear ();




cout<<"Enter other names\n";

getline (cin, OtherNames);
cin.get ();

cin.clear ();

cout<<"Enter Land number\n";
getline (cin,LandNum);


cin.clear ();

cout<<"Enter owner KRA pin\n";
getline (cin,KraPin);

cin.clear ();


cout<<"Enter Owner id card number\n";
getline (cin, OwnerIdCard);

cin.clear ();

cout<<"Enter owner address\n";
getline (cin, address);


cin.clear ();


cout<<"Enter Land county\n";

getline (cin, LandCounty);

cin.clear ();


cout<<"Enter Land district\n";
getline (cin, LandDist);

cin.clear ();


cout<<"Enter Land division\n";
getline (cin, LandDiv);

cin.clear ();

cout<<"Enter land location\n";
getline (cin, Landloc);

cin.clear ();


cout<<"Enter Land sub-location\n";
getline (cin, LandSubloc);


cin.clear ();


}

EnterDetails (enter);



PrintEnteredDetails (show);

system ("pause");

EXIT_SUCCESS;

}








void PrintEnteredDetails (LandDetails &show)

{// problem is here
//function to display the values input by user at runtime (this function is not returning/displaying the values entered by user)

cout<<"\n------------------------------------------------\n"<<endl;
cout<<"Surname:"<<show.Surname<<endl;

cout<<"OtherNames:"<<show.OtherNames<<endl;

cout<<"Land number"<<show.LandNum<<endl;

cout<<"KRA pin:"<<show.KraPin<<endl;

cout<<"Owner Id Card number:"<<show.OwnerIdCard<<endl;



cout<<"Postal address:"<<show.address<<endl;

cout<<"County:"<<show.LandCounty<<endl;
cout<<"District:"<<show.LandDist<<endl;
cout<<"Division:"<<show.LandDiv<<endl;
cout<<"Location:"<<show.Landloc<<endl;
cout<<"Sub-location:\n\n"<<show.LandSubloc<<endl;

}


Last edited on
At first, i had included this overloaded constructor instead of EnterDetails constructor but it failed:

LandDetails(string& a, string& b,string &c,string &d, string &e,string &f,string &g, string &h,string &j, string &k,string &l)
{
Surname = a;
OtherNames = b;
LandNum = c;
KraPin = d;
OwnerIdCard = e;
address = f;
LandCounty = g;
LandDist =h;
LandDiv =j;
Landloc =k;
LandSubloc =l;


}
Somehow I don't understand your logic at all. Why don't you store the input in main in your struct and pass it to PrintEnteredDetails ?

1
2
3
4
5
6
7
8
9
10
int main()
{
   LandDetails input;  
   cout<<"Welcome to the new lands management information system\n\n";
   cout<<"Enter Surname: ";
   getline (cin,input.Surname);
  // get the other fields as well
  PrintEnteredDetails(input);
}
wah... Thanks Thomas1965, i didnt have the slightest idea, am good now.
@Mwangi Elihah:

Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

You've been asked this several times before.
Topic archived. No new replies allowed.