Converting an Array of Character to a String

This is a discussion on Converting an Array of Character to a String within the C++ Programming forums, part of the General Programming Boards category; I am a newbie and completed a phone book project in which a user could enter a single name, number ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    9

    Converting an Array of Character to a String

    I am a newbie and completed a phone book project in which a user could enter a single name, number and single-word note for each entry. In a follow-up, the instructor has now required that the program accept names and notes that might be multiple words. I set the program up with a first class for entries and second class for the phone book; “currentname” and “currentnumber” are variables declared in the phone book class. The “add entry to the phone book” function below is intended to receive a character array (all words and spaces between) for names and numbers. The function is within the phone book class. I am getting error messages at the lines where I hope to make conversions of character arrays to strings.
    Code:
        void enterNewContact(){        char tempName[40], tempNote[50];
            if (num_entries > 0)
                i = num_entries;
            else
                i = 0;
            cout << "Please enter Name:";
            cin.getline(tempName, 40);
            currentname(tempName);           //Intending that the character array be "converted" to string here.  Getting error message.
            entryItem[i].setName(currentname);
            cout << "Please enter Number:";
            cin >> currentnumber;
            while (currentnumber.at(3) != '-'){
                cout << "Please enter the phone number in the following format: XXX-XXXX" << endl;
                cout << "Please enter Number:";
                cin >> currentnumber;
            }
            entryItem[i].setNumber(currentnumber);
            cout << "Please enter Note:" << endl;
            cin.getline(tempNote, 50);
            currentnote(tempNote);             //Same issue here.
            entryItem[i].setNote(currentnote);
            num_entries++;
            cout << "The new entry has been added to the Phone book."  << endl;
            }

  2. #2
    DRK
    DRK is offline
    Registered User
    Join Date
    Jun 2009
    Posts
    111
    Use assign operator instead of ()
    Code:
    currentname = tempName;
    Actually you can skip it and call just
    Code:
    entryItem[i].setName(tempName);
    causing the conversion constructor to be called implicitly.
    Last edited by DRK; 04-19-2012 at 06:38 AM.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    9
    Thanks DRK. I tried both ways and get Runtime errors.

  4. #4
    DRK
    DRK is offline
    Registered User
    Join Date
    Jun 2009
    Posts
    111
    The rest of the function code seems to be OK. The runtime error is triggered probably somewhere else in the program. Provide more information about declarations of:
    entryItem
    currentname , setName()
    currentnumber , setNumber()
    currentnote , setNote()

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    Please show how and where currentname is defined.

    Jim

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    9
    Here is my code

    Code:
    #include <iostream>#include <fstream>
    #include <string>
    #include <cstring>
    
    
    using namespace std;
    
    
    int phoneBookCheck = 0, delete_choice;
    int i, k = 0, num_entries;
    string currentname, currentnumber, currentnote;  //Here is where I declare currentname, currentnumber, and currentnote.
    char functionchoice;
    
    
    class entry {
    private:
    	string name1, number1, note1;
    public:
    	entry(){
    		name1 = " ";
    		number1= " ";
    		note1 = " ";
    	}
    	entry(string name, string number, string note){
    		name1 = name;
    		number1 = number;
    		note1 = note;
    	}
    	void setName(string x){name1 = x;}
    	void setNumber(string y){number1 = y;}
    	void setNote(string z){note1 = z;}
    	string getName()   {return name1;}
    	string getNumber() {return number1;}
    	string getNote()   {return note1;}
    };
    class phoneBook {
    public:
    	entry entryItem[100];
    	int num_entries;
    
    
    	phoneBook(){
    		num_entries = 0;
    	}
    //There are other phone book functions I have left out.
    	void enterNewContact(){
    		char tempName[40], tempNote[50];
    		if (num_entries > 0)
    			i = num_entries;
    		else
    				i = 0;
    		cout << "Please enter Name:";
    		cin.getline(tempName, 40);
    		entryItem[i].setName(tempName);
    		cout << "Please enter Number:";
    		cin >> currentnumber;
    		while (currentnumber.at(3) != '-'){
    			cout << "Please enter the phone number in the following format: XXX-XXXX" << endl;
    			cout << "Please enter Number:";
    			cin >> currentnumber;
    		}
    		entryItem[i].setNumber(currentnumber);
    		cout << "Please enter Note:" << endl;
    		cin.getline(tempNote, 50);
    		entryItem[i].setNote(tempNote);
    		num_entries++;
    		phoneBookCheck = 0;
    		cout << "The new entry has been added to the Phone book." << endl;
    	}
    };  //End of the phone book class.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    2,590
    you can skip char arrays altogether if you do the following:

    Code:
    std::string temp;
    std::getline(cin, temp);
    entryitem[i].setName(temp)
    char arrays can be very error-prone, and have virtually no place in modern C++. std::string is really the way to go for any text handling needs.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by IPthereforIam View Post
    I tried both ways and get Runtime errors.
    Well, are you going to post any details about those Runtime errors?

    You may as well have just said "I have a secret for you".
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    9
    Sorry iMalc - I appreciate you considering my problem and should have explained better. I am inserting a screen shot. The red text is the description of the error and my comment is in green.

    Name:  Screen Shot of Error.jpg
Views: 489
Size:  73.5 KB

  10. #10
    Registered User
    Join Date
    Mar 2012
    Posts
    9
    Thank you Elkvis for your help. I will try this today.

  11. #11
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    As the error says.. you're accessing out of range index values from a std::string.
    This wasn't caught by a char array.. but as the string throws an exception, you now know what to look for.

    Run the debugger and find out the exact line in which the exception is thrown.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    my comment is in green.
    It looks like part of your problem is possibly being caused by mixing the C++ extraction operator>> and getline(). You probably have a newline character left in the input buffer that your getline() takes as the end of its input. I can't really see the actual value of currentnumber in your picture, but it looks like there are only 3 characters, the std::string.at() starts at 0 so if there are only 3 characters in this string it will throw an exception, at(3) would be out of bounds. You may want to check the length of the string before you use your magic number in a test.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 11-12-2008, 11:25 AM
  2. Converting space character in string to integer
    By boostage in forum C++ Programming
    Replies: 6
    Last Post: 10-19-2006, 09:40 PM
  3. Converting a text box value into a character array?
    By n00bguy in forum Windows Programming
    Replies: 3
    Last Post: 07-29-2006, 09:08 PM
  4. Converting a string to a character array
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 10-29-2003, 10:25 PM
  5. Converting string/character to binary?
    By SyntaxBubble in forum C++ Programming
    Replies: 5
    Last Post: 10-28-2003, 12:02 PM

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