help me out with structs

This is a discussion on help me out with structs within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream.h> struct CustomerInfo{ char name[100]; char address[100]; int lawnsize; char daytobecut[10]; }; //--------------------------------------------- void AddCustomer() { cout<<"ADD"<<endl; cout<<"Enter ...

  1. #1
    Unregistered
    Guest

    help me out with structs

    Code:
    #include <iostream.h>
    
    struct CustomerInfo{
    	char name[100];
    	char address[100];
    	int lawnsize;
    	char daytobecut[10];
    };
    //---------------------------------------------
    void AddCustomer()
    {
    	cout<<"ADD"<<endl;
    	cout<<"Enter name to be added: ";
    	cin>>CustomerInfo.name;
    	cout<<"Enter address: ";
    	cin>>CustomerInfo.address;		
    	cout<<"Enter lawn size: ";
    	cin>>CustomerInfo.lawnsize;
    	cout<<"Enter day to be cut: ";
    	cin>>CustomerInfo.daytobecut;
    }
    //---------------------------------------------
    void FindCustomer()
    {
    }
    //---------------------------------------------
    void RemoveCustomer()
    {
    }
    //---------------------------------------------
    int main()
    {
    	char menuchoice;			
    	cout<<"Enter Display, Add, dElete, Quit: ";
    	cin>>menuchoice;
    	if(menuchoice=='A')
    		AddCustomer();
    	else if(menuchoice=='D')
    		FindCustomer();
    	else if(menuchoice=='E')
    		RemoveCustomer();
    	
    	return (0);
    }
    its a shop interface they will eventually be 2d arrays but for now 1d i just want to be able to input one individual for now so help me out plz

  2. #2
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Code:
    void AddCustomer()
    {
    	cout<<"ADD"<<endl;
    	cout<<"Enter name to be added: ";
    	cin>>CustomerInfo.name;
    	cout<<"Enter address: ";
    	cin>>CustomerInfo.address;		
    	cout<<"Enter lawn size: ";
    	cin>>CustomerInfo.lawnsize;
    	cout<<"Enter day to be cut: ";
    	cin>>CustomerInfo.daytobecut;
    }
    You can't fill CustomerInfo with information, you need to declare an instace of it first...

    Code:
    void AddCustomer(CustomerInfo Cust)
    {
    	cout<<"ADD"<<endl;
    	cout<<"Enter name to be added: ";
    	cin>>Cust.name;
    	cout<<"Enter address: ";
    	cin>>Cust.address;		
    	cout<<"Enter lawn size: ";
    	cin>>Cust.lawnsize;
    	cout<<"Enter day to be cut: ";
    	cin>>Cust.daytobecut;
    }
    Main...

    Code:
    int main()
    {
    	char menuchoice;
    	CustomerInfo MyCustomer;			
    	cout<<"Enter Display, Add, dElete, Quit: ";
    	cin>>menuchoice;
    	if(menuchoice=='A')
    		AddCustomer(MyCustomer);
    	else if(menuchoice=='D')
    		FindCustomer(MyCustomer);
    	else if(menuchoice=='E')
    		RemoveCustomer(MyCustomer);
    	
    	return (0);
    }

  3. #3
    Unregistered
    Guest
    Code:
    #include <iostream.h>
    
    struct CustomerInfo{
    	char name[100];
    	char address[100];
    	int lawnsize;
    	char daytobecut[10];
    };
    //---------------------------------------------
    void AddCustomer(CustomerInfo &Customer)
    {
    	cout<<"ADD"<<endl;
    	cout<<"Enter name to be added: ";
    	cin.getline(Customer.name,100);
    	cout<<"Enter address: ";
    	cin.getline(Customer.address,100);		
    	cout<<"Enter lawn size: ";
    	cin>>Customer.lawnsize;
    	cout<<"Enter day to be cut: ";
    	cin>>Customer.daytobecut;
    }
    //---------------------------------------------
    void FindCustomer(CustomerInfo &Customer)
    {
    	cout<<Customer.name<<endl;
    	cout<<Customer.address<<endl;
    	cout<<Customer.lawnsize<<endl;
    	cout<<Customer.daytobecut<<endl;
    }
    //---------------------------------------------
    void RemoveCustomer()
    {
    }
    //---------------------------------------------
    int main()
    {
    	char menuchoice;			
    	CustomerInfo Customer;
    	cout<<"Enter Display, Add, dElete, Quit: ";
    	cin>>menuchoice;
    	if(menuchoice=='A')
    		AddCustomer(Customer);
    	else if(menuchoice=='D')
    		FindCustomer(Customer);
    	else if(menuchoice=='E')
    		RemoveCustomer();
    	
    	FindCustomer(Customer);
    	return (0);
    }
    wow thanks but could you help on this now why does the input mess up? it skips the enter the name and adresse part

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    After every cin.getline call, write cin.ignore(50,'\n'). This ignores the 1st 50 characters in the cin buffer or up to the newline character, whichever comes first.

    Basically, what is happening is that the info from the previous input is still in the buffer so the computer thinks that the user already inputted info.

  5. #5
    Unregistered
    Guest
    Code:
    //---------------------------------------------
    void AddCustomer(CustomerInfo &Customer)
    {
    	cout<<"ADD"<<endl;
    	cout<<"Enter name to be added: ";
    	cin.getline(Customer.name,50);
    	cin.ignore(50,'\n');
    	cout<<"Enter address: ";
    	cin.getline(Customer.address,50);		
    	cin.ignore(50,'\n');
    	cout<<"Enter lawn size: ";
    	cin>>Customer.lawnsize;
    	cout<<"Enter day to be cut: ";
    	cin>>Customer.daytobecut;
    }
    ok i did that but now i have another problem where customer.name never gets a value and it waits for 2 enters before going to lawn size

  6. #6
    Unregistered
    Guest
    ok wtf! i got it fixed but im not sure why or how it work
    Code:
    //---------------------------------------------
    void AddCustomer(CustomerInfo &Customer)
    {
    	cout<<"ADD"<<endl;
    	cout<<"Enter name to be added: ";
    	cin.ignore(50,'\n');
    	cin.getline(Customer.name,50);	
    	cout<<"Enter address: ";
    	cin.getline(Customer.address,50);	
    	cout<<"Enter lawn size: ";
    	cin>>Customer.lawnsize;
    	cout<<"Enter day to be cut: ";
    	cin>>Customer.daytobecut;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 09:30 AM
  2. Multidimentional structs + memcpy() == FAIL
    By Viper187 in forum C Programming
    Replies: 8
    Last Post: 06-18-2008, 03:46 AM
  3. packed structs
    By moi in forum C Programming
    Replies: 4
    Last Post: 08-20-2002, 02:46 PM
  4. ArrayLists + Inner Structs
    By ginoitalo in forum C# Programming
    Replies: 5
    Last Post: 05-09-2002, 06:09 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 01:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21