Converting string to char

This is a discussion on Converting string to char within the C++ Programming forums, part of the General Programming Boards category; I got this error in my code I do not know how to figure it out Cannot convert 'string' to ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    58

    Converting string to char

    I got this error in my code I do not know how to figure it out


    Cannot convert 'string' to 'char' in function healthClubMaintain::addClient(string)


    Code:
    #include <iostream>
    using namespace std;
    
    class healthClubMaintain{
    
    private:
    	 char Al[15];
    	string classCleintName;
    	int classClientIDNumber;
    public:
    	void addClient(string);
    
    };
    
    
    void healthClubMaintain::addClient(string clientName){
    	
    	classCleintName = clientName;
    	
    		Al[0]=classCleintName;
    	
    	   cout << Al[0] << endl;
    }
    
    
    int main(){
    
    int number,loop=0;
    int clientIDNumber,programNumber,approvalIDNumber,clientIDA,programNumberA;
    string clientName,programName,approvalName;
    healthClubMaintain Club;
    
    while ( loop==0){
    
    	cout<< "Select one of the following option:"<<endl;
    	cout<< "(1) Add a new client to the program"<<endl;
    	cout<< "(2) Display all clients in the program"<<endl;
    	cout<< "(3) Add a new exercise to the program"<<endl;
    	cout<< "(4) Display all exercise to the program"<<endl;
    	cout<< "(5) Add an approval for an exercise for a client"<<endl;
    	cout<< "(6) Display all approval records in the program"<<endl;
    	cout<< "(7) Add a performed record to the program"<<endl;
    	cout<< "(8) Display all preformed records in the program"<<endl;
    	cout<< "(9) Determine for client(s) performance "<<endl;
    	cout<< "(10) Exit the program"<<endl;
    	cin >> number;
    
    
    if ( number ==1 ){
    	cout << "Insert the client's name" << endl;
    	cin >> clientName;
    	cout << "Insert a new ID" << endl;
    	cin >>clientIDNumber;
    	Club.addClient(clientName);
    }
    }
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Al[0] is a character, not a string. I don't see a purpose for Al, so maybe you can just remove it; but if you need it somehow change it to a string.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    I suppose you want to have vector of strings and not an array of chars (do I have a dejavu?)

    even more accurate - you want to have a map<int,string>
    which will store pairs Id,Name
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    Is there any way to do it without using the map thing because it is so complicated for me.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    My big concern is that I want to make sure that I stored the values in the array everytime I run the menu until it becomes full.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you don't like vectors, you can at least use an array of strings (like string ClientNameList[15] or similar instead of char Al[15]).

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    yeah I am using string array this time, but I am trying to store the values into the array but it doesn't seem working

    Code:
    void healthClubMaintain::addClient(string clientName){
    	
    	for(int i=0;i<15;i++)
    	{
    		Al[i]=clientName;
    	}
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Define "working".

    Read your code -- you put clientName into each slot of your array. Do you want clientName in each slot of your array? If not, then why put it there? Presumably you need to figure out which number client this is (a counter, maybe?) and put it in that one slot.

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    don't you want to store IDs as well?
    Make class Client that will have 2 members:
    int ID;
    string Name;

    Make an array (I'd say vector, but you do not like to write a correct C++ code) of Clients
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    Well I asked my professor and he gave me an easier way to do it. I will need to initial a variable and increment it each time the user will add a new client.. but I still have some problems in the code.. here is my code>>
    Code:
    #include <iostream>
    using namespace std;
    
    class healthClubMaintain{
    
    private:
    	 string Al[15];
    	 
    	 int counter,i;
    	
    	string classCleintName;
    	int classClientIDNumber;
    public:
    	void addClient(string);
    
    };
    
    
    // Here is the method ... I need to store my inputs in an array and then print them
    void healthClubMaintain::addClient(string clientName){
    	counter=0;
    	i=0;
    	
    	if (counter<15){
    		Al[counter]= clientName;
    	}
    	counter++;
    	
    	while(i<15){ 
    		cout<<Al[i];
    		i++;
    	}
    }
    
    
    
    int main(){
    
    int number,loop=0;
    int clientIDNumber,programNumber,approvalIDNumber,clientIDA,programNumberA;
    string clientName,programName,approvalName;
    healthClubMaintain Club;
    
    while ( loop==0){
    
    	cout<< "Select one of the following option:"<<endl;
    	cout<< "(1) Add a new client to the program"<<endl;
    	cout<< "(2) Display all clients in the program"<<endl;
    	cout<< "(3) Add a new exercise to the program"<<endl;
    	cout<< "(4) Display all exercise to the program"<<endl;
    	cout<< "(5) Add an approval for an exercise for a client"<<endl;
    	cout<< "(6) Display all approval records in the program"<<endl;
    	cout<< "(7) Add a performed record to the program"<<endl;
    	cout<< "(8) Display all preformed records in the program"<<endl;
    	cout<< "(9) Determine for client(s) performance "<<endl;
    	cout<< "(10) Exit the program"<<endl;
    	cin >> number;
    
    
    if ( number ==1 ){
    	cout << "Insert the client's name" << endl;
    	cin >> clientName;
    	cout << "Insert a new ID" << endl;
    	cin >>clientIDNumber;
    	Club.addClient(clientName);
    	
    
    }
    }
    
    }
    Last edited by aama100; 02-20-2008 at 03:35 PM.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,772
    Well, you set your counter to 0 every time the addClient function is run, so you overwrite the value in the same slot. Again.
    You should simply initialize counter to 0 in the constructor and increment it after adding a new client.
    Btw, I also mentioned it in the other thread, but indenting your code a little better wouldn't hurt.
    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. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. string to char converting problem
    By bergziege in forum C++ Programming
    Replies: 2
    Last Post: 08-07-2007, 03:37 PM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. converting string to integer, for further use
    By shoobsie in forum C Programming
    Replies: 2
    Last Post: 07-01-2005, 03:12 AM
  5. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM

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