Thread: More Help

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    18

    More Help

    Can someone explain what is wrong with my code?
    Code:
    //explation: Writing a database to hold friends and family, just as a test of
    //my programming skills. It should ask you to select a catagory in the beginning
    //then you will go to the relavent section. The problem is, it has alot of syntax
    //errors, there are still errors and most sections which cause the errors have been
    //commented out. Please help me fix the code.
    
    #include <iostream>
    #include <string>
    
        using std::string;
        using std::cout;
        using std::endl;
    
        struct database
        {
    
        int age;
        string name;
        string name2;
        };
    
        int main()
    
        {
        database family;                        //will be known as catagory 2
    	
    	family.age=11;
    	family.name = "Katherine";
        family.name2 = "Lau";
    
        }
    
        database friends;                       //catagory 1
    
    	friends.age=14;
    	friends.name = "Richard";
    	friends.name2 = "Reader";
    	
    	cout<<"Select Catagory(1 or 2)";
    	
    	//int cat;
    	
        //cin<<cat;
        
        //IF(cat==1)
    	
        {
    	
    	cout << "First Name :" << friends.name << endl;
        cout << "Surname :" << friends.name2 << endl;
    	cout << "Age :" << friends.age << endl;
    	
        }	
    	
    	//else if(cat==2)
    	
        {
    	
    	//cout << "First Name :" << family.name << endl;
        //cout << "Surname :" << family.name2 << endl;
    	//cout << "Age :" << family.age << endl;
    	
        }
    	
        return 0;
        
        {

  2. #2
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    //cin<<cat;

    //IF(cat==1)


    try

    cin>>cat;

    and if (cat ==1)

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    18
    why is there a parse error when I add
    else if(cat==2)
    and even if I type 1 or 2 in my proggie, it comes up with a messed up version,
    Code:
    //explation: Writing a database to hold friends and family, just as a test of
    //my programming skills. It should ask you to select a catagory in the beginning
    //then you will go to the relavent section. The problem is, it has alot of syntax
    //errors, there are still errors and most sections which cause the errors have been
    //commented out. Please help me fix the code.
    #include <iostream>
    #include <string>
    
    using std::string;
    using std::cout;
    using std::endl;
    
    struct database{
    
    int age;
    string name;
    string name2;
    
    };
    
    int main()
    
    {
        database family;    
    	
    	family.age=11;
    	family.name = "Katherine";
        family.name2 = "Lau";
    
        database friends;
    
    	friends.age=14;
    	friends.name = "Richard";
    	friends.name2 = "Reader";
    	
    	cout<<"Please input 2,family or 1,friends";
    	
    	int cat;
    	
    	cin>>cat;
    
    	
    	if(cat==1)
    	
        cout << "First Name :" << friends.name << endl;
        cout << "Surname :" << friends.name2 << endl;
    	cout << "Age :" << friends.age << endl;
    	
    	//else if(cat==2)
    	
    	cout << "First Name :" << family.name << endl;
        cout << "Surname :" << family.name2 << endl;
    	cout << "Age :" << family.age << endl;
    	
    	return 0;
    
    }
    Last edited by Zophixan; 11-01-2002 at 06:07 PM.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Well here you go. You have a if with more than one line so you need brackets.

    Code:
    //explation: Writing a database to hold friends and family, just as a test of
    //my programming skills. It should ask you to select a catagory in the beginning
    //then you will go to the relavent section. The problem is, it has alot of syntax
    //errors, there are still errors and most sections which cause the errors have been
    //commented out. Please help me fix the code.
    #include <iostream>
    #include <string>
    #include <conio.h>
    using std::string;
    using std::cout;
    using std::endl;
    
    struct database{
    
    int age;
    string name;
    string name2;
    
    };
    
    int main()
    
    {
        database family;    
    	
    	family.age=11;
    	family.name = "Katherine";
        family.name2 = "Lau";
    
        database friends;
    
    	friends.age=14;
    	friends.name = "Richard";
    	friends.name2 = "Reader";
    	
    	cout<<"Please input 2,family or 1,friends";
    	
    	int cat;
    	
    	cin>>cat;
    
    	
    	if(cat==1)
    	{ // MUST USE
        cout << "First Name :" << friends.name << endl;
        cout << "Surname :" << friends.name2 << endl;
    	cout << "Age :" << friends.age << endl;
    	} // MUST USE
    	else if(cat==2)
    	{ //MUST USE
    	cout << "First Name :" << family.name << endl;
        cout << "Surname :" << family.name2 << endl;
    	cout << "Age :" << family.age << endl;
    	} //MUST USE
       getch();
    	return 0;
    
    }
    It should work good now.

    NOTE: Switch statement may be easier to use!!

Popular pages Recent additions subscribe to a feed