Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1647
  • Last Modified:

cin not accepting String variables in Turbo C++

[C++ Error] Unit2.cpp(12): E2094 'operator<<' not implemented in type
'ostream' for arguments of type 'string'

I'm getting this error each time i try to use 'cin' function to input String variables from the console. I'm using Turbo C++ Explorer I downloaded recently from borland.com.

How to do i get rid of this error?
0
VbMonk
Asked:
VbMonk
  • 2
1 Solution
 
Jose ParrotGraphics ExpertCommented:
That's right, VbMonk, there is no << for cin.

Exemple:
int number;
cin>>number;

The opposite direction, say <<, is used for cout.
Example:
int number=10;
cout<<number<<endl;

Jose
0
 
Jose ParrotGraphics ExpertCommented:
Still need a help?
Let me change the approach, as the message error is related to cout (not cin).
E2094 error reason is
"The operator you are calling is not defined in this class. When you have an expression: x + x, where x is of type class X, the operator + has to be defined in class X and be accessible."

You may want to check the following example.
Example

//
// string.cpp
//
 #include<string>
 #include <iostream>
 using namespace std;
 int main()
 {
   string test;
   //Type in a string over five characters long
   while(test.empty() || test.size() <= 5)
   {
     cout << "Type a string between 5 and 100 characters long. " << endl;
     cin >> test;
   }
   //Test operator[] access
   cout << "Changing the third character from " 
        << test[2] << " to * " << endl;
   test[2] = '*';
   cout << "now its: " << test << endl << endl;

   //Try the insertion member function
   cout << "Identifying the middle: ";
   test.insert(test.size() / 2, "(the middle is here!)");
   cout << test << endl << endl;

   //Try replacement
   cout << "I didn't like the word 'middle',so "
           "instead, I'll say:" << endl;
   test.replace(test.find("middle",0), 6, "center");
   cout << test << endl;
   return 0;
 }

Program Output

Type a string between 5 and 100 characters long.
roguewave <----- you typed this
Changing the third character from g to *
now its: ro*uewave
Identifying the middle: ro*u(the middle is here!)ewave
I didn't like the word 'middle', so instead, I'll say:
ro*u(the center is here!)ewave

Are the includes and construction in your code as in the abova example? Does this sample runs properly in your environment?

Jose
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now