Results 1 to 7 of 7

Thread: 'Strings' in C++

  1. #1
    Senior Member Stubzz's Avatar
    Join Date
    Sep 2003
    Posts
    573
    Thanks
    0
    Thanked
    0 times in 0 posts

    'Strings' in C++

    OK now how the hell do these work. I'm more used to java so but I thought this would be dead easy. How do you use 'strings' properly here? What it needs to do is display the current date, read in a new date and replace the old with the new date.

    Compileing the code below just says lvalue required so somethings not right
    Code:
    void Apointments::setDate(){
    	char date[20];
    	char todaysDate[20];
    	date = "18/03/2004";     <-----HERE
    	std::cout << "Todays date is currently: " << todaysDate;
    
    	std::cout << "\nPlease newdate: ";
    	std::cin >> todaysDate;
    
    	date = todaysDate;      <------HERE
    } //end setDate
    Whered I go wrong

  2. #2
    Member
    Join Date
    Aug 2003
    Location
    Wimbledon
    Posts
    141
    Thanks
    0
    Thanked
    0 times in 0 posts
    The problem is 'date' is not a string, it is an array of characters. If you want a java-like string object you need to use the string classes from the std library (#include<string>). To make your code work you need to use the string manipulation functions...

    Code:
    void Apointments::setDate()
    {
    	char date[20];
            char todaysDate[20];
            strycpy(date, "18/03/2004");     <-----HERE
    	std::cout << "Todays date is currently: " << todaysDate;
    
    	std::cout << "\nPlease newdate: ";
    	std::cin >> todaysDate;
    
    	strcpy(date, todaysDate);      <------HERE
    } //end setDate

  3. #3
    Senior Member Stubzz's Avatar
    Join Date
    Sep 2003
    Posts
    573
    Thanks
    0
    Thanked
    0 times in 0 posts
    Right, thanks for that. It compiles now but it only displays:

    Code:
    Todays date is: X(à
    Please enter todays date:
    It never shows the original value of date, but it works fine once the date has ben changed
    Last edited by Stubzz; 29-03-2004 at 11:21 AM.

  4. #4
    Goat Boy
    Join Date
    Jul 2003
    Location
    Alexandra Park, London
    Posts
    2,428
    Thanks
    0
    Thanked
    0 times in 0 posts
    You are not initializing todaysDate before printing it out, so you are just seeing some random bit of memory on your machine...
    "All our beliefs are being challenged now, and rightfully so, they're stupid." - Bill Hicks

  5. #5
    Senior Member Stubzz's Avatar
    Join Date
    Sep 2003
    Posts
    573
    Thanks
    0
    Thanked
    0 times in 0 posts
    Wheres the bang head on wall smilie when you need one?

    Was meant to print out DATE not todaysDate

    *puts on his dunce hat*

  6. #6
    Goat Boy
    Join Date
    Jul 2003
    Location
    Alexandra Park, London
    Posts
    2,428
    Thanks
    0
    Thanked
    0 times in 0 posts
    That work now?
    "All our beliefs are being challenged now, and rightfully so, they're stupid." - Bill Hicks

  7. #7
    Senior Member Stubzz's Avatar
    Join Date
    Sep 2003
    Posts
    573
    Thanks
    0
    Thanked
    0 times in 0 posts
    Yeah it's all working fine now thanks.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. guitar string help
    By Jonlad in forum General Discussion
    Replies: 17
    Last Post: 15-09-2004, 09:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •