well I'm almost there

This is a discussion on well I'm almost there within the C++ Programming forums, part of the General Programming Boards category; thank you all for the help with the program check out this code Code: #include<iostream> #include<cstring> #include<cmath> using namespace std; ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    78

    well I'm almost there

    thank you all for the help with the program check out this code
    Code:
    #include<iostream>
    #include<cstring>
    #include<cmath>
    using namespace std;
    template <class T , int r>
    
    class myclass
    {
     private: T n[r];
     public:
    	 void read()
    	 {
    		 for(int i=0; i<r; ++i)
    		 {
    			 cout<<"Enter data: "; 
    			 cin>>n[i];
    		 }
    	 }
    
    	void show ()
    	{
    	for(int i=0; i<r; ++i)
    		 {
    			 cout<<"\t"<<n[i];
    			
    		 }
    	}
    };
    
    void main()
    {
    	cout<<"Enter integer data\n";
    	myclass<int, 5> a; 
    	a.read();
    	a.show();
    	
    	cout<<"\nEnter character data\n";
    	
    	myclass<char,5> b; 
    	b.read();
    	b.show();
    	cout<<endl;
    	
    	//myclass<string, 5>c;
    	//cout<<"Enter string data\n";
    	//c.read();
    	//c.show();
    		
    	
    }
    As all of you may have noticed I have commented out the last object "myclass<string, 5>c", and the reason behind that is because When I include it I get some wild error message. Does anyone know the remedy to this situation

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    I just run this code , it's very good, not error!
    Code:
    #include<iostream>
    #include<cstring>
    #include<cmath>
    using namespace std;
    
    template <class T , int r>
    
    class myclass
    {
     private: T n&#091;r&#093;;
     public:
       void read()
       {
         for(int i=0; i<r; ++i)
         {
           cout<<"Enter data: "; 
           cin>>n&#091;i&#093;;
         }
       }
    
      void show ()
      {
      for(int i=0; i<r; ++i)
         {
           cout<<"\t"<<n&#091;i&#093;;
          
         }
      }
    };
    
    int main()  //change void to int
    {
      cout<<"Enter integer data\n";
      myclass<int, 5> a; 
      a.read();
      a.show();
      
      cout<<"\nEnter character data\n";
      
      myclass<char,5> b; 
      b.read();
      b.show();
      cout<<endl;
      
      myclass<string, 5>c;
      cout<<"Enter string data\n";
      c.read();
      c.show();
      
      system("pause");
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    1
    You forgot to

    Code:
    #include <string>
    you need to include that header if you're using the std::string class.

    --
    rval
    Last edited by rval; 09-15-2004 at 04:29 AM.

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Thats so strange I copied and pasted the code and it compiled on dev-c++ with just changing void main to int and returning 0 strange huh? and ran right
    Woop?

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    @ prog-bman :
    just changing void main to int and returning 0 strange huh? and ran right
    I am confused too.

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    I see why i searched the include files after a good while of searching an included file in iostream that includes a file that includes <string> which probably is a bad thing i think.
    [edit]heres the path if anyone cares
    Code:
    iostream-->ostream||istream-->ios-->bits/basic_ios.h-->
    bits/locale_classes.h-->string
    [/edit]
    Last edited by prog-bman; 09-15-2004 at 05:17 AM.
    Woop?

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    That doesn't work on every compiler (of course) and on at least one compiler, if you forget to include <string> you can use only certain parts of the string class. For example, on VC++ 6.0 if you include <iostream> and not <string> you can use strings but you can't output them to cout using operator<<. People end up trying to use work arounds like .c_str() when all they need to do is include <string>.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21