Thread: Need help please, something's wrong with the string

  1. #1
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200

    Unhappy Need help please, something's wrong with the string

    It kinda getting really annoying when i had done all the research and in theory, this should be working, (but it did not)
    Code:
    #include <iostream.h>
    #include <string>
    
    int main()
    {
    
    	using namespace std;
    
    	string s1, s2, s3;
    	char input1[20], input2[20];
    
    	cout<<"Input 1"<<endl;
    	cin>>input1;
    
    	cout<<"Input 2"<<endl;
    	cin>>input2;
    
    	s1 = input1;
    	s2 = input2;
    
    	s3 = s1 + "." + s2;
    
    	cout<<s3;
    	return 0;
    }
    For some reason, when i run it, the program gives me this message:
    Code:
    error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversi
    on)
    Can anyone please point out what i did wrong. Thanks a lot
    Hello, testing testing. Everthing is running perfectly...for now

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    #include <iostream.h>

    should be:

    #include<iostream>

    and your using declaration should be above main().

  3. #3
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    I have modified your code a bit
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    
    	string s1, s2, s3;
    	char input1[20], input2[20];
    
    	cout<<"Input 1"<<endl;
    	cin>>input1;
    
    	cout<<"Input 2"<<endl;
    	cin>>input2;
    
    	s1 = input1;
    	s2 = input2;
    
    	s3 = s1 + "." + s2;
    
    	cout<<s3;
    	return 0;
    }
    Last edited by sunnypalsingh; 10-25-2005 at 09:24 PM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Why do you have input1 and input2? Once you make the change to <iostream> you should be able to read directly into s1 and s2, and remove input1 and input2 from your program. There is no need for character arrays here.

  5. #5
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    to Daved: No, i was trying to add two string together. When i add the array of charracters, i get an error said "can't add the two pointer". That why i convert it into string. I know we can use string to get the character from the users, but one of my program requires input of only array char

    to sunny: It actually works. What is the different between <iostream> and <iostream.h>? I thought it is just two different way to call the header file, one for old version and one for new version.

    Oh, one more question: how do you convert from string to array of characters?
    Last edited by hdragon; 10-25-2005 at 10:07 PM.
    Hello, testing testing. Everthing is running perfectly...for now

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The string class is "newer", so <iostream.h> may not have the required code to use with it, which is why <iostream.h> is not standard and you should always use <iostream>. Why would you want to use the old version if a newer one was available anyway?

    To get a constant character array from a string that you don't want to modify, you use the c_str() method. It sounds like you might want to modify the character array, so you'd have to use strcpy to copy the result of the call to c_str() into the character array. Make sure the character array is big enough.

    Using the string class is almost always preferred over character arrays in beginning (and advanced) C++ programs. If your instructor/book/tutorial requires character arrays, then that is old-school thinking. Keep learning and using the string class for your own work.

    If you did have to do the whole program with character arrays, you can add them together with strcat.

  7. #7
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    iostream is somewhat more restrictive than the older iostream.h. One would possibly avoid problems by careful use of namespaces, but it would be a lot smarter to stick with one or the other.

    On a futher note, iostream.h is deprecated (it was replaced when the C++ standard came out about 7 years ago)

    Technically speaking, iostream.h is not considered depreciated, because it was never part of the official standard in the first place.

    You should avoid using the *.h version as much as possible, because some implementations have bugs in their *.h version. Moreover, some of them support non-standard code that is not portable, and fail to support some of the standard code of the STL.

    Furthermore, the *.h version puts everything in the global namespace. The extension-less version is more stable and it's more portable. It also places everything in the std namespace.

    iostream.h is an old style method of including std C++ headers, and in opposite to iostream you don't have to declare that you're using the std namespace.

  8. #8
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    Well, it had been while since i was working with string and its functions. I just figured out that my function in my program can take "const char *variable". So i was successful in using c_str();
    Speaking of include file without ".h", do all of them can use with this method? like <stdio>, <stdlib>, <windows> ....
    Hello, testing testing. Everthing is running perfectly...for now

  9. #9
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Quote Originally Posted by hdragon
    Speaking of include file without ".h", do all of them can use with this method? like <stdio>, <stdlib>, <windows> ....
    Take a look at this
    http://www.cplusplus.com/doc/ansi/hfiles.html

  10. #10
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    I see, thanks a lot guys
    Hello, testing testing. Everthing is running perfectly...for now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM