Thread: Problem with cin to char*

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    17

    Question Problem with cin to char*

    Hi, I wanted to pass an input to a char* then pass it to a class member.
    but before i even get to the class member, i got stuck in the char* itself.

    Code:
    #include <iostream>
    using namespace::std;
    
    class CTest
    {
    public:
    	char *name;
    };
    
    CTest Test;
    char *gName = "";
    
    int main()
    {
    	cout<<"Enter Name: ";
    	cin>>gName;   //<--here's the error
    	Test.name = gName;
    	return 0;
    }
    after i compile, it's ok.
    but once i run it.
    then it breaks and popup a message saying:
    Unhandled exception at 0x104f01be (msvcp80d.dll) in test.exe: 0xC0000005: Access violation writing location 0x0041664b.
    then opens a page on the "istream" file.


    any leads to solve this?
    I thought char* was able to store as string?

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> Test.name = gName;

    Should that be strcat or strcpy or something?



    EDIT -

    change

    Code:
    	cout<<"Enter Name: ";
    	cin>>gName;   //<--here's the error
    to

    Code:
    	char *gName;
    	cout<<"Enter Name: ";
    	cin>>gName;   //<--here's the error
    and I would recommend the strings rather than char*'s
    Last edited by twomers; 08-07-2006 at 03:58 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> char *gName = "";
    That points gName to a constant string that happens to be empty. You cannot modify the constant string, which is why it fails when you try to.

    You should use the C++ string class instead of character arrays. If you are forced to use C style strings, you need to allocate memory for the character array before using cin to read into it.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    17
    Quote Originally Posted by twomers
    >> Test.name = gName;

    Should that be strcat or strcpy or something?



    EDIT -

    change

    Code:
    	cout<<"Enter Name: ";
    	cin>>gName;   //<--here's the error
    to

    Code:
    	char *gName;
    	cout<<"Enter Name: ";
    	cin>>gName;   //<--here's the error
    and I would recommend the strings rather than char*'s

    thanks mate, but it doesn't help.
    Code:
    #include <iostream>
    using namespace::std;
    
    class CTest
    {
    public:
    	char *name;
    };
    
    CTest Test;
    //char *gName = "";
    
    int main()
    {
    	char *gName = "";
    	cout<<"Enter Name: ";
    	cin>>gName;
    	//Test.name = gName;
    	return 0;
    }

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    17
    Quote Originally Posted by Daved
    >> char *gName = "";
    That points gName to a constant string that happens to be empty. You cannot modify the constant string, which is why it fails when you try to.

    You should use the C++ string class instead of character arrays. If you are forced to use C style strings, you need to allocate memory for the character array before using cin to read into it.

    gosh~!!!
    <<snip profanity>>
    man, i was trying to do everything in char*.
    guess i have no choice.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It means you have to either allocate some space, or use an array of chars.

    You can't just declare a char* and assume it points at infinite amounts of memory you can do anything you like with.

    Oh, and watch the language in future.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    17
    Quote Originally Posted by Salem
    It means you have to either allocate some space, or use an array of chars.

    You can't just declare a char* and assume it points at infinite amounts of memory you can do anything you like with.

    Oh, and watch the language in future.
    ah, Salem. sorry to brake the rules.
    anyway, so i guess if i do every char* into char[99]; would be ok?
    how much bytes or size does a string takes anyway?
    coz if everything in char[99] works less than a string then i'll use char[99].

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It is unlikely that you need to worry about how much memory your string takes up. If you do, then the amount used by the C++ string class depends on your platform and its implementation of the string class. Most likely it would use less memory than char[99] for a name.

    Of course, you should use the string class because it is easier to use and safer than character arrays, not because it might use less memory in this particular instance.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Daved
    safer than character arrays
    What's unsafe about character arrays? o.O
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    17
    Quote Originally Posted by Daved
    It is unlikely that you need to worry about how much memory your string takes up. If you do, then the amount used by the C++ string class depends on your platform and its implementation of the string class. Most likely it would use less memory than char[99] for a name.

    Of course, you should use the string class because it is easier to use and safer than character arrays, not because it might use less memory in this particular instance.
    ah~~~ ok.
    Now I've got more reasons to change all my char* into string.
    Thanks Daved.

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by itsme86
    What's unsafe about character arrays? o.O
    http://www.parashift.com/c++-faq-lite/containers.html
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> What's unsafe about character arrays? o.O

    With character arrays, there are many more opportunities for careless code to cause data corruption or potential security risks, since the programmer is required to maintain the length of the array and monitor what is copied or concatenated to it. For example, in the original code and even more so in twomers attempted solution, memory that does not belong to the string is being written. This could have all kinds of affects on the program, and in the case of the OP, it led to a crash. Once proper memory is allocated for gname, it must be strcpy'd to Test.name. If the size of Test.name is not properly set and/or the code isn't perfect, then it is possible that data input by the user through cin could overrun the Test.name array buffer.
    Last edited by Daved; 08-07-2006 at 04:43 PM.

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    So it sounds like, from reading what Daved wrote and what Mario linked to, that the only unsafe factor regarding arrays is the human factor. What you wrote, Daved, about the string class being safer than using arrays made it sound, to me at least, like arrays were inheritly unsafe for some reason. But it really just sounds like it boils down to the programmer's experience with them (arrays).

    Anyway, I was just looking for some clarification. I'm much more familiar with C than C++ and wanted to make sure I didn't learn something phenomenal.
    If you understand what you're doing, you're not learning anything.

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I guess the term makes no sense from a C perspective, of course.
    I personally prefer the term "evil" as the C++ FAQ suggests. Unsafe is not really correct, because there is nothing inherently unsafe about arrays.

    But from within C++ it is more than human factor that allows arrays to be labelled unsafe. Sure it all boils down to the human factor. But you have to dig deep to reach that conclusion. And this because C++ is not exclusively procedural. OOP and Generic Programming add complexity layers to your code that simply can rend arrays (and pointers, btw) a nightmare to manage even by the most expert of the programmers.

    Without any prejudice to C, under C++, and past a certain level of complexity, the use of arrays can become inherently unsafe. Not because they are so, but because they resemble unsing a wrench to unplug a keyboard. It's easy to break something.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    But a skillful and experienced weirdo can, with practice, unwrench a keyboard quickly and elegantly!

    If char arrays are bad, then so are gotos and continues, I take?
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with cin. Please help me.
    By Antigloss in forum C++ Programming
    Replies: 17
    Last Post: 06-06-2005, 09:50 AM
  2. Input File HELP, weird problem
    By gravity-1 in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2005, 08:43 PM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Problem with cin
    By ErionD in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 11:27 AM