String in CLASS

will string be different in class?

Page 1 of 1

14 Replies - 1531 Views - Last Post: 14 January 2010 - 10:40 AM Rate Topic: -----

#1 raiga23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 06-August 09

String in CLASS

Posted 14 January 2010 - 01:24 AM

May I asked why is my programming facing error stating

C:\Users\Nick\Desktop\test\test.cpp(16) : 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 co

// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream.h>
#include<string>

using namespace std;

int main(int argc, char* argv[])
{
	string s;

	cout<<"Name : ";
	cin>>s;


	printf("Hello World!\n");
	return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: String in CLASS

#2 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 287
  • View blog
  • Posts: 1,471
  • Joined: 16-May 09

Re: String in CLASS

Posted 14 January 2010 - 01:40 AM

First, Instead of <iostream.h> use <iostream> as the standard says.

the problem is with compiler I guess... what is the brand and version?

My compiler can compile this
#include <iostream>
#include <string>
using namespace std;
int main () {
	string buf;
	cin>>buf;
	cout<<buf<<endl;

	return 0;
}

So the problem is with header files your compiler uses.

This post has been edited by Anarion: 14 January 2010 - 01:41 AM

Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: String in CLASS

Posted 14 January 2010 - 01:45 AM

The cin >> operator does not write strings, but rather character arrays. See istream::operator>>

To use a string you want to use the strings library getline() function.
Was This Post Helpful? 0
  • +
  • -

#4 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 287
  • View blog
  • Posts: 1,471
  • Joined: 16-May 09

Re: String in CLASS

Posted 14 January 2010 - 01:59 AM

Quote

The cin >> operator does not write strings

Compiled with GCC (meant ++ instead of cc but it gets filtered) 4.3.3 and no problem

Edit: I guess in older compilers, it can't be compiled because of the old definitions

Look at the next post

This post has been edited by Anarion: 14 January 2010 - 02:19 AM

Was This Post Helpful? 0
  • +
  • -

#5 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1256
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: String in CLASS

Posted 14 January 2010 - 02:10 AM

You're actually both wrong. the std::string class overloads the operator>> function which allows it to interact with std::istream objects. sweet.
Was This Post Helpful? 1
  • +
  • -

#6 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: String in CLASS

Posted 14 January 2010 - 02:11 AM

View PostAnarion, on 14 Jan, 2010 - 12:59 AM, said:

Quote

The cin >> operator does not write strings

But look at this line: cin>>strobj;
the output of cin>> is an array of characters, when you have a string on the right side of this operator, it stores this char array to the internal storage of the string. So it can be used this way.

Compiled with GCC (meant ++ instead of cc but it gets filtered) 4.3.3 and no problem

Edit: I guess in older compilers, it can't be compiled because of the old definitions

Look at the OP's error message.
Was This Post Helpful? 0
  • +
  • -

#7 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 287
  • View blog
  • Posts: 1,471
  • Joined: 16-May 09

Re: String in CLASS

Posted 14 January 2010 - 02:15 AM

View PostDark_Nexus, on 14 Jan, 2010 - 11:40 AM, said:

You're actually both wrong. the std::string class overloads the operator>> function which allows it to interact with std::istream objects. sweet.


Quote

I guess in older compilers, it can't be compiled because of the old definitions

I got it and changed already :D thanks for telling

View Postn8wxs, on 14 Jan, 2010 - 11:41 AM, said:

View PostAnarion, on 14 Jan, 2010 - 12:59 AM, said:

Quote

The cin >> operator does not write strings

But look at this line: cin>>strobj;
the output of cin>> is an array of characters, when you have a string on the right side of this operator, it stores this char array to the internal storage of the string. So it can be used this way.

Compiled with GCC (meant ++ instead of cc but it gets filtered) 4.3.3 and no problem

Edit: I guess in older compilers, it can't be compiled because of the old definitions

Look at the OP's error message.

Sir I already looked at it. Nexux explained well, also look at Here
Edit: Probably his compiler has an older version of string class, which doesn't overload >>

This post has been edited by Anarion: 14 January 2010 - 02:28 AM

Was This Post Helpful? 0
  • +
  • -

#8 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: String in CLASS

Posted 14 January 2010 - 02:15 AM

View PostDark_Nexus, on 14 Jan, 2010 - 01:10 AM, said:

You're actually both wrong. the std::string class overloads the operator>> function which allows it to interact with std::istream objects. sweet.

Only partially:

Quote

Extract string from istream
Extracts a string from the input stream is storing its content in str. Any previous content of str is cleared.

This function overloads the global operator>> to behave as described in istream::operator>> but applied to string objects.

Notice that the istream extraction operations use whitespaces as separators, therefore this operation will only extract what can be considered a word from the stream. To extract entire lines of text, refer to the string overload of global function getline.

Was This Post Helpful? 0
  • +
  • -

#9 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1256
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: String in CLASS

Posted 14 January 2010 - 02:19 AM

how is that any different than what i said?
Was This Post Helpful? 0
  • +
  • -

#10 raiga23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 06-August 09

Re: String in CLASS

Posted 14 January 2010 - 08:21 AM

I tried ad, but the string still cant work.
What i am actually trying to do is :

string str;

cout<<"Name : "
cin>>str;

if( str == "Alien")
cout<<"You are not human."<<endl;

but the error still exist.
Was This Post Helpful? 0
  • +
  • -

#11 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: String in CLASS

Posted 14 January 2010 - 08:33 AM

Well the problem was probably the mixing of a standard header and a non-standard header. The non-standard header may not include the std:: namespace, which means that the operator<< overload did not apply to istream but to std::istream -- thus the confusion.

The following code works on Borland C++ 5.5, Visual Studio 2008,2010, MinGW, and DigitalMars
#include <iostream>
#include <string>

using namespace std;

int main() {
	string name;
	cout << "Please enter your name: ";
	cin >> name;
	if (name == "Alian") {
		cout << "you are not human!" << endl;
	} else {
		cout << "Hello, " << name << endl;
	}

	return 0;
}


if it does not work on your compiler... get a new compiler.
Was This Post Helpful? 0
  • +
  • -

#12 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: String in CLASS

Posted 14 January 2010 - 08:41 AM

confirmed. Using these headers:
#include <iostream.h>
#include <string>
the above program fails on VS2008 (2010 does not even recognize the .h header and does a file not found).

The error in VS is: error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

Note that the other compilers:

Borland 5.5 -- its iostream.h is basically:
using namespace std;
#include <iostream>
and so it compiles the code just fine, and so does DigitalMars (they are both using the Dinkum STL libraries).

MinGW will compile it, but it complains bitterly about it.
Was This Post Helpful? 0
  • +
  • -

#13 raiga23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 06-August 09

Re: String in CLASS

Posted 14 January 2010 - 09:10 AM

View PostNickDMax, on 14 Jan, 2010 - 07:33 AM, said:

Well the problem was probably the mixing of a standard header and a non-standard header. The non-standard header may not include the std:: namespace, which means that the operator<< overload did not apply to istream but to std::istream -- thus the confusion.

The following code works on Borland C++ 5.5, Visual Studio 2008,2010, MinGW, and DigitalMars
#include <iostream>
#include <string>

using namespace std;

int main() {
	string name;
	cout << "Please enter your name: ";
	cin >> name;
	if (name == "Alian") {
		cout << "you are not human!" << endl;
	} else {
		cout << "Hello, " << name << endl;
	}

	return 0;
}


if it does not work on your compiler... get a new compiler.



But for my case i need to use iostream.h since i m making something up in class.
So if for my case how do i edit it??
Because in normal cpp i could compile it but if in class cpp i cant.
Was This Post Helpful? 0
  • +
  • -

#14 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: String in CLASS

Posted 14 January 2010 - 09:24 AM

What compiler are you using for class?
Was This Post Helpful? 0
  • +
  • -

#15 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: String in CLASS

Posted 14 January 2010 - 10:40 AM

DO NOT use iostream.h -- it is NOT STANDARD (we have been covering this a good deal lately). It is VERY OLD and not even available in some modern compilers (VS 2010 does not ship with it).

Why are school SO FAR behind the times, why are they teaching kids programming from the 1980/90's? It is 2010! We are supposed to have flying cars and spacecraft -- if you want to know why we don't -- well take a look at the stupid professors who can't take 2 hours out of their precious day to learn a few updates to a language.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1