std::string input not working, VC++6.0

Pages: 12
I'm using MS VC++ 6.0 Enterprise, which sucks. But alas, moving on.

The line I have currently is:

cin >> input; //input is declared as std::string input;

Error:
g:\c++\jarvis\jarvis.cpp(26) : 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 have also tried using getline:

getline(cin, input);

Error:
g:\c++\jarvis\jarvis.cpp(26) : error C2065: 'getline' : undeclared identifier

My includes:
1
2
3
4
5
6
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <vector>
#include <string.h> 
should be #include <string>

(string.h is something completely different)
c:\program files\microsoft visual studio\vc98\include\ostream(9) : fatal error C1083: Cannot open include file: 'ios': No such file or directory

EDIT: I get this when I switch out the .h
Last edited on
Why are you using iostream.h? I suggest you use iostream without the .h.

It also looks like you're trying #include<ostream> instead of #include <iostream>(?)
microsoft visual studio\vc98\


This place isn't really about getting 14 year old compilers to work. Perhaps you should considering using something a bit more modern.
if you declared input as std::string input, then shouldn't the declaration for getline be std::getline(std::cin, input); (or)
std::cin>>input;
No, I don't think getline(istream,string) is in std. For instance, the following code worked fine.

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>

int main()
{
    std::cout << "Say something." << std::endl;
    std::string name;
    getline(std::cin,name);
    std::cout << "You entered: " << name << std::endl;
    return 0;
}
getline should be in the std namespace. If it isn't, you have a problem with your supplied libraries probably.
Oh, wow, never suspected that, so getline is from #include <string>
and it's initialization does not need std? ;O !
getline is in my std:: but I guess it does not matter if you include getline without using namespace std; or using std::getline it still debugs fine...
I know the compiler is ancient, but it's the one my school makes me use. If I were doing this myself I'd use my copy of Express. but anyway. I will try it with std::getline
Whovian wrote:
No, I don't think getline(istream,string) is in std. For instance, the following code worked fine.

getline(std::cin,name);

This code uses std::getline(), due to argument-dependent lookup (whenever an unqualified function call is made, the compiler first looks in the namespace of the arguments. That's why it's possible to write std::cout << name and not std::operator<<(std::cout, name) )
Last edited on
using std::getline didn't help... was told that

g:\c++\jarvis\jarvis.cpp(12) : error C2039: 'getline' : is not a member of 'std'
g:\c++\jarvis\jarvis.cpp(12) : error C2873: 'getline' : symbol cannot be used in a using-declaration
g:\c++\jarvis\jarvis.cpp(27) : error C2039: 'getline' : is not a member of 'std'
g:\c++\jarvis\jarvis.cpp(27) : error C2065: 'getline' : undeclared identifier
I suggest that you don't use std::getline(std::istream,std::string), then. My IDE seems to support either std::getline or getline, without a using namespace std;.
What other functions can I use to get a c++ std::string?
EDIT: with the delimiter of a space... here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
void ParseInput()
{


	string Temp;
	do
	{
	//Check token
	while (getline(cin, Temp, ' ')) 
	{
		//save token
		parsed.push_back(Temp);
		//check token
		for(int x=0; x<greetings.size(); x++)
		{
			if(Temp==greetings[x])
			{
				cout << "Greeting found!" << endl;
			}
		}

		for(int y=0; y<commands.size(); y++) 
		{
			if(strcmp(Temp, commands[y])==0)
			{
				cout << "Command found!" << endl;
			}
		}

		for(int z=0; z<objects.size(); z++)
		{
			if(strcmp(Temp, objects[z])==0)
			{
				cout << "Object found!" << endl;
			}
		}
	//end of while loop		
	}
	//end of do loop
	}(while Temp!="end");


I'll work on the strcmp() stuff after I have strings to compare. all of the things I compare temp too are vector<string> containers
Last edited on
You could use:
1
2
std::string entry;
std::cin >> entry;


or

1
2
std::string entry;
std::getline(std::cin, entry);


The difference is that the first method will give you everything up until the first whitespace. The second one will give you everythign up until the first new line.

So in the code I just posted above, replacing getline with cin should still do what I want?

Tried it, got this:
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)
Last edited on
I assume so. That is assuming that you aren't worried about \t or \n.
see above
In that case, you didn't include:
#include <string>

Note that this is very different from
#include <string.h>
or
#include <cstring>
Last edited on
Pages: 12