Thread: Problem with passing strings to a member function

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    1

    Problem with passing strings to a member function

    Ok for some reason when I passed both a string and an integer only the recent value will be called when I use the "GET" command. For example I would type in "SET name=alan" and when I would use the "GET" function I would of course call the value alan. But when I SET another member I cannot get back the "name" member value. I would just get the value of the most recent member that was set. Here is the code, I think I am passing the strings wrong or I need a copy constructor, please help me:

    Code:
    #include <iostream>
    //#include <cstdlib>
    #include <string>
    #include <stdio.h>
    using namespace std;
    
    char * arrbName;
    char * pch;
    //	string input2;
    char * varName;
    
    class Info
    {
    public:
    	Info();
    	string getName();
    	void setName(const string& user_name);
    //	string getStreet();
    //	void setStreet(const string& user_street);
    	int getAge();
    	void setAge(int num);
    //	int getID();
    //	void setID(int num2);
    
    
    private:
    	string Name;
    	string Street;
    	int age;
    	int id;
    
    };
    
    
    
    
    Info::Info()
    {
    	Name="";
    	Street="";
    	age=0;
    //	id=0;
    }
    
    string Info::getName()
    {
    	return Name;
    }
    
    void Info::setName(const string& user_name)
    {
    
    	Name = user_name;
    }
    
    /*string Info::getStreet()
    {
    	return Street;
    }
    
    void Info::setStreet(string user_street)
    {
    
    	Street = user_street;
    }*/
    
    int Info::getAge()
    {
    	return age;
    }
    
    void Info::setAge(int num)
    {
    	age = num;
    }
    
    
    
    
    
    void main()
    {
    	Info information;
    	char command1[] = "SET";
    	char command2[] = "GET";
    	char command[3];
    	char input1[] = "name"
    	char input2[] = "street";
    	char input[30];
    	int index;
    
    
    
    	while(1)
    	{
    		printf(": ");
    		scanf("%s %s", command,input);
    
    		
    		
    		if(strcmp (command1,command) == 0)
    		{
    	
    		
    				pch = strtok(input, " =");
    				arrbName = pch;
    				pch = strtok(NULL, " =");
    				if(pch == NULL)
    				{
    					cout<<"ERROR commandline is incomplete"<<endl;
    					exit(1);
    				}
    				else
    					varName = pch;
    	
    
    
    		
    
    			if(arrbName == "name" || "Name")
    			{
    				string stringName = varName;
    			//	information.setName(stringName);
    				information.setName(stringName);
    			}
    			else if(arrbName == "age" || "Age")
    			{
    		//		string streetName = varName;
    				int i = atoi(varName);
    				information.setAge(i);
    				
    			}
    			else
    				cout<<"ERROR the property name does not match with out members"<<endl;
    		}
    		else if(strcmp (command2,command) == 0)
    		{
    			if(input == "name" || "Name")
    			{
    				cout<<information.getName()<<endl;
    			}
    			else if(input == "age" || "Age")
    			{
    				cout<<information.getAge()<<endl;
    			}
    			else if(input =="*")
    			{
    				cout<<"name: "<<information.getName()<<endl;
    				cout<<"age: "<<information.getAge()<<endl;
    			}
    			else
    				cout<<"ERROR the property name does not match with out members"<<endl;
    		}
    		else
    			cout<<"ERROR the command line is not reconized"<<endl;
    
    	}
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    if(arrbName == "name" || "Name")
    You probably think this does something. Alas, it does not. Now if you had done
    Code:
    if(arrbName == "name" or arrbName == "Name")
    then you'd be somewhere.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Is there a reason you so heavily mix C and C++ code?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Replies: 3
    Last Post: 06-23-2005, 06:39 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM