Thread: How to use "String.h" library in MFC visual C++

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    52

    Post How to use "String.h" library in MFC visual C++

    I use WinConsle Application in visual Studio C++ to write code,but I can't use "string.h" ,compiler inform that "string undeclared identifier" although in folder "Include" of VC++ ,I had this file(string.h)

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <iomanip.h>
    #include <conio.h>
    #include <string.h>
    //const char file.out[]="C://thu.txt";
    //void outputline (int acc,const char*name,double bal){
    	/*cout<<setiosflags(ios::left)<<setw(10)<<acc
    		<<setw(13)<<name<<setw(7)<<setprecision(3)
    		<<resetiosflags(ios::left)
    		<<setiosflags(ios::fixed| ios::showpoint)
    		<<bal<<'\n'<<flush;*/
    void outputline(int acc){
    	cout<<acc<<endl;
    }
    void main()
    {
    	string a = "abcd";//Error at this line,don't understand  string
    	int i = a.length;
    	cout<<i;
        ofstream outputfile(file.out,ios::out);
        if(!outputfile){
            cerr<<"File not found";
            exit(1);
        }
        int account,acc;
        char name[30];
        float balance;
        //while (cin>>account>>name>>balance){
            /*outputfile <<account<<' '<<name
                        <<' ' <<balance<<'\n'<<flush;*/
    	while (cin>>account)
    		//Viet du lieu ra file
    		outputfile <<account<<flush;
            //cout<<"?";
    
    	ifstream inFile("C://thu.txt");
    	if (!inFile){
    		cerr<<"File not found\n";
    		exit(1);
    }
    
    /*	int acc;
    	char name[30];
    	double bal;
    
    	cout<<setiosflags(ios::left)<<setw(10)<<"Account"
    		<<setw(13)<<"Name"<<"Balance\n"<<flush;*/
    	/*while(inFile>>acc>>name>>bal)
    		outputline(acc,name,bal);*/
    	
    	while(inFile>>acc && (!inFile.eof()))
    		outputline(acc);
    	getch();
    }
    Whoever show me how to use "string.h" file in Win Console??Thanks very much
    Last edited by Ken Fitlike; 09-28-2006 at 07:38 AM. Reason: added code tags

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Looks like you want to use std::string.
    The right header would be
    Code:
    #include <string>
    using string.h just declares the c-string ( char arrays ) functions.
    Kurt

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    You are trying to include the C style string header that declares functions such as strcmp and strcpy.

    The header you want is <string>

    Also use code tags too please. It's not fun to have to read your code as is.
    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.

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Code:
    void main
    is outdated, I am suprised MSVC++ lets you use this.
    Change it to
    Code:
    int main ( void )
    as it more to the standard. Also, you shuold of at least had a compiler warning about this, unless you have set it on a low level.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    52
    I think void main() or int main() is Ok,compiler in MFC Win Consolse C++ doesn't catch them .
    Quote Originally Posted by ZuK
    Looks like you want to use std::string.
    The right header would be
    Code:
    #include <string>
    using string.h just declares the c-string ( char arrays ) functions.
    Kurt
    I have this error and when I edit like you typed,It is true..thanks for Zuk...
    But I don't understand why I can type #include <iostream.h> but to use string.h I have to type
    " #include <string>
    using std::string;"
    Thanks for helping of everybody......
    Last edited by zaracattle; 09-28-2006 at 08:29 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    What is the current code that you are working with?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    52
    Quote Originally Posted by laserlight
    What is the current code that you are working with?
    This code is only code for me to try "string.h" library ,thanks for your interest....

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    But I don't understand why I can type #include <iostream.h> but to use string.h I have to type
    " #include <string>
    using std::string;"
    Might be a compiler quirk. You should #include <iostream>, not <iostream.h>
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    string.h is for C-Style string functions. Not for std::string functions. Your code indicates you are using std::string in this line:

    Code:
    string a = "abcd";
    That is a C++ style string. And the way to use it is to include <string>, not <string.h>. You also have to code:

    Code:
    std::string a = "abcd";
    Instead of what you had before. This is so because the type string is included inside the namespace std. If you don't like to have to add std:: to every instance of string, you can put using namespace std; just before main().

    If on the other side you are indeed wanting to use C-Style strings, not the C++ ones. Then you cannot create a string with string a. You leave your include as is right now and your strings are defined like so:

    Code:
    char a[] = "abcd";
    
    or 
    
    char *a = "abcd";
    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.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> But I don't understand why I can type #include <iostream.h> but to use string.h I have to type

    <iostream> is the new C++ version of <iostream.h>, so for old compilers either will work relatively well.

    <cstring> is the new C++ version of <string.h>, both work on C style string functions.

    <string> has no older, pre-standard equivalent, so if you aren't including <string>, you cannot use the string class. Note that this is different than <string.h>/<cstring>.

    You should be using <iostream>, not <iostream.h> because when you move on to a modern compiler your <iostream.h> won't work any more.

  11. #11
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Your code hasn't been valid C++ since 1997. You might consider a somewhat newer reference to base your work off of, something that actually takes into account the 1998 standard?


    For example:
    <iostream.h> => <iostream>
    <fstream.h> => <fstream>
    <stdlib.h> => <cstdlib>
    <iomanip.h> => <iomanip>
    <string.h> => <string>

    You're missing a large number of std:: as well; your code is extremely legacy from before namespaces existed in C++.
    Last edited by Cat; 09-28-2006 at 09:26 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> <string.h> => <string>
    That's not correct, and it is the source of the OPs confusion as well.

    <string.h> => <cstring>
    nothing => <string>

    >> <conio.h> => <cconio>
    Also, conio.h is not standard, so if your compiler supports it, it is still <conio.h>.

  13. #13
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Also there's no evolution like the one implied in <iomanip.h> => <iomanip>

    iomanip is strictly a C++ library. Much like "iostream.h", iomanip.h is supplied by some compilers in order to provide backward compatibility.

    EDIT: Oops. Nevermind the above. Cat was addressing exactly C98 changes.
    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.

  14. #14
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by Daved
    >> <string.h> => <string>
    That's not correct, and it is the source of the OPs confusion as well.

    <string.h> => <cstring>
    nothing => <string>
    Yeah, I know, I was detailing the changes he should make, not necessarily the changes made by the 1998 standard.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using external library with Visual C++
    By aeldes1 in forum C Programming
    Replies: 12
    Last Post: 04-18-2008, 09:04 AM
  2. question about .net to 6.0 change causing errors
    By jverkoey in forum C++ Programming
    Replies: 17
    Last Post: 03-23-2004, 10:45 AM
  3. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  4. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM