#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Binghamton, NY
    Posts
    3
    Rep Power
    0

    Problem with sting and cout


    I am using visual C++ 6.0 to write a DLX architecture simulation for my class. I have a sample input file that contains the instructions I will be simulating. For some reason I am unable to use cout to print the contents of a string. Its been awhile since I have programmed so maybe I am overlooking something simple. Here is my code:

    #include <stdio.h>
    #include <iostream.h>
    #include <fstream.h>
    #include <string>
    #include <cstdlib>


    main() {

    char line[20];
    char *ptr;
    std::string test;

    ifstream assemblyfile;
    assemblyfile.open("Z:\\School\\EE552\\input.txt", ios::in);

    assemblyfile.getline(line,20,'\n');

    ptr = strtok(line,",");

    while(ptr != NULL) {
    test = ptr;
    cout << test << endl; //This is the line that causes the error
    ptr = strtok(NULL,",");

    }


    assemblyfile.close();
    return(0);

    }

    The error message I get is:
    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 conversion)

    I can also not use the "using namespace std" without getting an error.

    Thanks in advance for the help.
  2. #2
  3. ASP.Net MVP
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Aug 2003
    Location
    WI
    Posts
    4,378
    Rep Power
    1510
    stdio.h and iostream.h are included in fstream.h. Also, you can drop the .h's now just like you did from string and cstdlib. Maybe that will help.
    Primary Forum: .Net Development
    Holy cow, I'm now an ASP.Net MVP!

    [Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]

    http://twitter.com/jcoehoorn
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Binghamton, NY
    Posts
    3
    Rep Power
    0
    Turns out it was something with my Visual Studio program. I swited to using g++ to compile and it is working fine now...no more visual c++ for this project
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    12
    Rep Power
    0
    I can compile the folowing code with no problem in VS.net

    PHP Code:
    #include <stdio.h> 
    #include <iostream> 
    #include <fstream> 
    #include <string> 
    #include <cstdlib> 

    using namespace std;

    int main() 

        
    char line[20]; 
        
    char *ptr
        
    std::string test

        
    ifstream assemblyfile
        
    assemblyfile.open("input.txt"ios::in); 

        
    assemblyfile.getline(line,20,'\n'); 

        
    ptr strtok(line,","); 

        while(
    ptr != NULL
        { 
            
    test ptr
            
    cout << test << endl//This is the line that causes the error 
            
    ptr strtok(NULL,","); 
        } 


    assemblyfile.close(); 
    return(
    0); 


  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Binghamton, NY
    Posts
    3
    Rep Power
    0
    I am unsure why it did not compile on my version of Visual Studio. It works absolutly fine when I use gpp. My version must be missing something. Oh well, thanks for all your help.
  10. #6
  11. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    11
    Originally posted by bbenvenuti88
    I am unsure why it did not compile on my version of Visual Studio. It works absolutly fine when I use gpp. My version must be missing something. Oh well, thanks for all your help.
    No, it was because you were mixing standard and non-standard headers. The following compiles just fine on my version of VC++ 6.0 (Enterprise edition, SP5):

    Code:
    #include <cstdio> 
    #include <iostream> 
    #include <fstream> 
    #include <string> 
    #include <cstdlib> 
    
    int main()
    { 
    	char line[20]; 
    	char *ptr; 
    	std::string test; 
    
    	std::ifstream assemblyfile; 
    	assemblyfile.open("Z:\\School\\EE552\\input.txt", std::ios::in); 
    	assemblyfile.getline(line,20,'\n'); 
    	ptr = strtok(line,","); 
    
    	while(ptr != NULL) { 
    		test = ptr; 
    		std::cout << test << std::endl;
    		ptr = strtok(NULL,","); 
    	} 
    
    	assemblyfile.close(); 
    	return(0); 
    }

IMN logo majestic logo threadwatch logo seochat tools logo