Solved

cin not accepting String variables in Turbo C++

Posted on 2006-11-27
4
1,630 Views
Last Modified: 2013-11-17
[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
Comment
Question by:VbMonk
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
JoseParrot earned 100 total points
ID: 18042466
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
 
LVL 18

Expert Comment

by:JoseParrot
ID: 18061065
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now