This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why isn’t cin >> string working with Visual C++ 2010?
The first program I wrote refuses to compile in Visual C++, and it looks like it's complaining that the '>>' operator isn't defined for istream.
After looking it over carefully, it seemed to be correct, so I checked with g++ and it compiles fine (and has no warnings with -Wall).
So why does it work with g++ but not Visual C++?
Here is the program:
#include <iostream>
#include <list>
int main() {
std::list<std::string> list;
std::string str = "";
std::cin >> str;
while (str.compare("q") != 0) {
list.push_back(str);
std::cin >> str;
}
std::cout << "You entered: \n";
for (std::list<std::string>::iterator i = list.begin(); i != list.end(); i++) {
std::cout << *i << std::endl;
}
return 0;
}
I had thought C++ code written for Visual C++ and C++ code written for g++ would be nearly identical in most circumstances.
How different are they, how often would you say these kinds of issues come up, and do you know of anywhere I can find some of these differences/gotchas?
Different compilers have different headers that internally include other headers. gcc is probably including <string> inside of <iostream>, while Visual Studio's <iostream> doesn't include <string>. Try putting:
#include <string>
At the top with your other includes. <string> is the header file that defines operator>>(std::istream, std::string) (in other words, <string> is the header that "officially" provides the function you need to do std::cin >> str;).
Related
Below is a snippet to show the basic structure of my code right now. On that last line, Visual Studio is saying "no instance of overloaded function that matches the argument list". According to the references I can find from VS help, however, this is exactly how the function should be used. I have string and iostream included, and I have precompiled headers turned off, so I'm not sure if the fault for VS not recognizing this function is on me or not.
#include <iostream>
#include <vector>
#include <string>
int main(){
Stack<double> nums;
std::string input;
std::string::size_type index;
std::cin >> input;
double num = std::stod(input, index);
}
It should be a pointer to size_type. Verified in VS.
double num = std::stod(input, &index);
When overloading is around, you must be precise :)
when I try to compile my program I get this error:
error: ‘_byteswap_ushort’ was not declared in this scope
long lNum = (long)_byteswap_ushort(iNum);
this is the program:
#include <iostream>
#include <sstream>
#include <stdlib.h>
using namespace std;
int main()
{
long inputNum;
cout << "Input number:\n";
cin >> inputNum;
long Num = (long)_byteswap_ulong(iNum);
stringstream oss;
oss << hex << Num;
string mystring = oss.str();
return 0;
}
I thought that including stdlib should solve the problem. Is there any other library I should include?
The program is compiled with:
g++ -m32 -o output32 prog.cpp
You seem to be trying to use a function specific to MS Visual C++ compiler, which is not available in GCC. Use an appropriate GCC builtin instead.
Seems like uint32_t __builtin_bswap32(uint32_t x) would be appropriate in this case.
Another option, if you wanted a portable solution, would be to use something like Boost Endian library.
I have been programming since 3 years in c++. I have used compilers like turbo c++, Dev c++, linux & codeblocks.
Recently, I started using Visual Studio 2014 C++ and I'm facing a problem with strings.
using namespace std;
int main()
{
string s;
cout << "enter string: ";
getline(cin, s);
cout << s;
return 0;
}
However, the compiler isn't identifying getline. Moreover, it isn't letting cin and cout use strings as well. The code seems to work with other compilers (e.g. Turbo C++, Dev C++, Linux, CodeBlocks), but it doesn't compile on Visual Studio.
I'm totally confused what could be the problem here.
You are missing #include <iostream>. Without this, you cannot use std::cout or std::cin. Also, you need #include <string> to use std::getline() and std::string.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
cout<<"enter string: ";
getline(cin,s);
cout<<s;
return 0;
}
You can also put system("pause"); before the return statement to have it pause when it prints the output, in case you wanted to see it.
I have a basic program that compares two strings :
#include <string>
#include <iostream>
using namespace std;
int main (int argc, char *argv[]) {
if(strcmp (argv[0],"./test") != 0) {
cout << "not equal" << endl;
} else {
cout << "equal" << endl;
}
return 0;
}
it compiles with gcc but not with clang :
> clang -o test test_clang.cpp
test_clang.cpp:7:6: error: use of undeclared identifier 'strcmp'
if(strcmp (argv[0],"./test") != 0) {
^
1 error generated.
Why doesn't it compile with clang ?
EDIT: People are getting harsh on stack overflow, up to the point that I am hesitating to post a question. The question above has a simple answer, fine, but is it normal to down-vote questions (twice in the first minute!) because they have a simple, yet non obvious, answer ?
Use
#include <string.h>
or
#include <cstring>
instead of
#include <string>
The string header is for the std::string from C++. string.h is for C zero terminated char* strings. cstring is like string.h but for C++.
The reason it worked with gcc is probably different warning/error level settings. It is possible to compile the code without #including the header and having the declaration of strcmp. The compiler will not be able to do type checking but the symbol still gets resolved by the linker.
You can also avoid using strcmp completely and write
#include <string>
#include <iostream>
int main (int argc, char *argv[]) {
std::string command = argv[0];
if( command != "./test" ) {
std::cout << "not equal" << endl;
} else {
std::cout << "equal" << endl;
}
return 0;
}
Using a std::string on one side of the comparison will cause the "./test" string to be converted into a std::string as well and the comparison will be done by the == operator of the std::string class.
You're not including the correct header file
#include <cstring>
You need to #include <cstring> (or possibly #include <string.h>.)
Many compilers include extra standard headers when you include another. The Standard allows this; it's your responsibility to use the headers that guarantee declarations for what you use, not just headers that happen to have the declarations for your compiler.
You have to include <cstring>. <string> is the header for C++ strings.
When using ifstream class to read words from an input file, I have used the following expression:
#include <iostream>
#include <fstream>
int main(int argc, char *argv[])
{
std::ifstream inputStream(myFile.txt);
std::string myString;
myFile.open()
while(myFile.good())
{
myFile >> myString;
printf("%s \n", myString);
}
return 0;
}
The contents of myFile.txt are:
" This is a simple program. "
The compiles and executes as expected using g++ compiler.
However, the same code when compiled using msvc 2008, returns error at the extraction operator (>>) requiring me to replace the std::string with either an initialized character array or any of the supported native types.
This threw me off as I was expecting the usage of the standard library to be same across implementations.
I understand the compile error and know the way to fix it via using c_str().
But, it would help me a great deal, if someone could clarify why the usage for the standard library is different across platforms.
To me it is not starndard anymore !!
EDIT: Code updated to be complete. Content of myFile.txt updated.
Chances are that you forgot to #include <string>. Without it, Microsoft's version of <iostream> (and such) include enough of a declaration of std::string for some things to work, but other parts are missing, so you get strange, seemingly inexplicable failures.
One of the things that's missing is most of the operator overloads for std::string, which is exactly what you seem to be missing.
As an aside, while (myfile.good()) ... is pretty much a guaranteed bug -- you probably want:
while (myfile>>myString)
std::cout << myString << " \n";
Alternatively, you could do the job with a standard algorithm:
#include <string>
#include <algorithm>
#include <iterator>
#include <fstream>
#include <iostream>
int main() {
std::ifstream myfile("input.txt");
std::copy(std::istream_iterator<std::string>(myfile),
std::istream_iterator<std::string>(),
std::ostream_iterator<std::string>(std::cout, " \n"));
return 0;
}
The following compiles fine for me on MSVC 2010:
std::ifstream inputStream;
std::string myString;
inputStream.open("myFile.txt", std::ifstream::in);
while(inputStream.good())
{
inputStream >> myString;
}
Note: without using std::ifstream::in as my open mode, I got the same error as you. I suggest you check what value you have for this parameter.