String Help!!!

This is a discussion on String Help!!! within the C++ Programming forums, part of the General Programming Boards category; can someone check on this code [error occur] Code: cout<<endl<<str.size()<<endl; if(str.size() % 2 ==0) { cout<<str; } else { str.resize((str.size)+1); ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    71

    String Help!!!

    can someone check on this code [error occur]

    Code:
        cout<<endl<<str.size()<<endl;
        if(str.size() % 2 ==0)
        {
            cout<<str;
        }    
        else
        {
            str.resize((str.size)+1);
        }
    the error message is

    In function `int main()':
    invalid use of member (did you forget the `&' ?)

  2. #2
    Supermassive black hole ahluka's Avatar
    Join Date
    Jul 2005
    Location
    South Wales, UK
    Posts
    1,709
    Can you post the whole code or is it too long?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    I don't about the error you are showing...you need to give the complete code for that...but there is another error in this code...it should be
    Code:
    (str.size()+1);
    instead of
    Code:
    ((str.size)+1);

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    yeah it is bit long

  5. #5
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Quote Originally Posted by gtr_s15
    yeah it is bit long
    Let me save your time...follow these steps
    1.Ctrl+A
    2.Ctrl+C
    3.Ctrl+V

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    Quote Originally Posted by sunnypalsingh
    I don't about the error you are showing...you need to give the complete code for that...but there is another error in this code...it should be
    Code:
    (str.size()+1);
    instead of
    Code:
    ((str.size)+1);
    ... and this error would likely make the compiler issue a statement like "invalid use of member", because it's... an invalid use of a member
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    Supermassive black hole ahluka's Avatar
    Join Date
    Jul 2005
    Location
    South Wales, UK
    Posts
    1,709
    But what would & have to do with anything?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  8. #8
    Registered User
    Join Date
    Dec 2005
    Location
    the Netherlands
    Posts
    5
    Don't know what you exactly want, anyway this is the official syntax:
    #include <string>
    void resize( size_type num, const TYPE& val = TYPE() );

    So, you cannot just add 1 in the resize syntax, as you did here:
    str.resize((str.size)+1);
    Instead, try something like this:
    Code:
    int newsize = str.size() + ;1
    str.resize(newsize);
    Here's an example:
    Code:
    #include <string.h>
    #include <iostream.h>
    
    using namespace std;
    
    int main ()
    {
    	string some_string;
    	int new_string_size;
    	
    	cout << "Enter a string:\n";
    	cin >> some_string;
    	cout << "string size = " << some_string.size () << endl << endl;
    	
    	if (some_string.size () % 2 == 0)
    	{
    		cout << "Your string size is even.\n";
    	} else
    	{
    		new_string_size = some_string.size () + 1;
    		some_string.resize (new_string_size);
    		
    		cout << "Your string size was odd and has now been made even.\n\n";
    	}
    }
    Last edited by StOnEwAlL; 12-09-2005 at 12:13 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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