Thread: Question about string & character array

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    50

    Question about string & character array

    I see this in the guidelines of my assignment. It is rather confusing.
    Code:
    char arr[10];
    cin.getline(arr,10);
    string str(arr);
    Beside the use of string function, what is the point of converting the content of a character array into string?
    Why use character array?Why not just define a string?

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I can't think of any reason, besides not knowing about the non-member getline function.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Perhaps the intent is to limit the input to 9 characters. The string version of getline is not capable of doing that.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    Thx!
    I need to convert part of a character array into integer and am wondering how.
    How to convert string into integer ?
    How to convert string into content of a character array ?

    I ask these questions because I think the atoi thing on C++.com may be able to do these. It need an entire character array but in my case, I only need part of a character array for conversion to integer.
    This is the example on c++.com:
    Code:
    /* atoi example */
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ()
    {
      int i;
      char szInput [256];
      printf ("Enter a number: ");
      gets ( szInput );
      i = atoi (szInput);
      printf ("Value entered is %d, and its double %d",i,i*2);
      return 0;
    }
    
    Output:
    Enter a number: 73
    Value entered is 73, and its double 146
    Last edited by Roy01; 11-19-2006 at 08:32 AM.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    More basic question about string. My problem is here:
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    using namespace std;
    void main()
    {
    	string str='000-000-0000';//The compiler said the string is too long. How to fix it?
    	if (str=='000-000-0000')//Using == operator is illegal here. How to fix it?
    	cout<<str;
    }

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Single quotes/apostrophes (' ') denote a single char, double quotes(" ") a char array; use double quotes.

    Also, use int main, not void main. Also, prefer cstdlib over stdlib.h and only if your code requires it.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    Thank you, Ken.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to put a string in a 2 dimensional character array?
    By atif7865 in forum C Programming
    Replies: 2
    Last Post: 12-05-2008, 10:26 PM
  2. String to a character array?
    By Coder87C in forum C++ Programming
    Replies: 18
    Last Post: 05-24-2005, 08:36 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM