I'm trying to learn c++ but a simple method like "cout" and "cin" does not exist
this is my code:
#include "stdafx.h"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
cout>>"hello";
return 0;
}
there is an error that says "error C2065: 'cout' : undeclared identifier"
and "IntelliSense: identifier "cout" is undefined"
cout is declared in the std namespace:
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "hello";
return 0;
}
You're also doing it wrong. Note how I have the angle brackets aboove.
Add #include <iostream> to stdafx.h. I was having trouble with that and it worked for me.
Adding using namespace std; may help, and also do cout << "hello" not >>.
cout is in std so you to have use using namespace std. And for cout operator is like <<. >> this is for input i.e cin.
#include "stdafx.h";
#include "iostream";
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout<<"hello";
system("pause");
return 0;
}
Related
#include <string.h>
#include <fstream>
using namespace std;
ofstream fout(argv[1]);
main function
int main(int argc, const char * argv[]) {
//I try to add "new word" fstream file
fout<<"new words"<<endl;
}
How can I do this? The bottom one didn't work either. It is necessery. It must be before main function
#include <string.h>
#include <fstream>
using namespace std;
string argument;
ofstream fout(argument]);
main function
int main(int argc, const char * argv[]) {
argument=argv[1]
fout<<"new words"<<endl;
}
You cannot initialize your fout before you know what argv is. So, in some sense, impossible. However, you can initialize it with nothing, and open it later when you get your argv:
ofstream fout;
int main(int argc, const char * argv[])
{
fout.open(argv[1]);
fout<<"new words"<<endl;
}
This way, the global fout is accessible to all functions defined after it. However, there is a short period, before opening it in main, when fout is invalid. This is the best you can do, from a logical point of view.
I am trying to create a basic test file using gtest but I am getting the error : testvs\debug\testvs.exe is not recognized as an internal or external command, operable program or batch file.
test.cpp
#include <gtest\gtest.h>
#include <iostream>
using namespace std;
TEST(test_case,simple_case)
{
int i=0;
int b=1;
int result= i*b;
int expected=0;
EXPECT_EQ(result,expected);
}
main.cpp
#include <gtest\gtest.h>
#include <iostream>
using namespace std;
void main(int argc, char* argv[])
{
::testing::InitGoogleTest(&argc,argv);
RUN_ALL_TESTS();
}
when I comment out the lines
TEST(test_case,simple_case)
{
int i=0;
int b=1;
int result= i*b;
int expected=0;
EXPECT_EQ(result,expected);
}
and
::testing::InitGoogleTest(&argc,argv);
RUN_ALL_TESTS();
it does not give any error.
Please help
This program compiles just fine:
#include <iostream>
#include <stdio.h>
#include <boost/program_options.hpp>
int main(int argc, char* argv[]) {
std::cout << "Hello world" << std::endl;
}
This program gives a long compilation error:
#include <iostream>
namespace cio {
#include <stdio.h>
}
#include <boost/program_options.hpp>
int main(int argc, char* argv[]) {
std::cout << "Hello world" << std::endl;
}
The full dump of the compilation error: http://codepad.org/aIcQqkgH
The linux command I'm using to compile the program is: c++ -o main.cpp.o -c main.cpp
Simply use
#include <cstdio>
http://en.cppreference.com/w/cpp/header/cstdio
This header is officially required to declare all the legacy C library declarations inside namespace std.
However, if you have a "problem" that requires these "solutions", indeed just namespace your own stuff. If it's actually Boost that pollutes the global namespace, then file a bug.
In the Dev C++ compiler I can write and successfully compile this:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string method;
cin >> method;
return 0;
}
but when I wrote the above code in Visual Studio 2013 (console app mode) I got this error:
Error: no operator ">>" matches these operands
operand types are: std::istream >> std::string
EDIT
in Visual Studio:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string method;
cin >> method;
return 0;
}
I understand what the error tells me. But why in only Visual Studio 2013?
Try to place header "stdafx.h" before other headers.
#include "stdafx.h"
simple exlample for read string in visual studio :
#include "stdafx.h"
#include<iostream>
#include<string>
#include<conio.h>
using namespace std;
int main()
{
std::string str ;
getline(cin, str);
cout << str;
_ getch();
return 0;
}
Here is my code:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
#include <math.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int userInput = -9999;
userInput = ReadNumber();
WriteAnswer(userInput);
system("pause");
return 0;
};
int ReadNumber ()
{
int liInput = -9999;
cin >> liInput;
return liInput;
};
void WriteAnswer(int data)
{
cout << data << endl;
};
When I try to compile, it saids :
1>error C3861: 'ReadNumber': identifier not found
1>error C3861: 'WriteAnswer': identifier not found
Why did the above error occurs? and how to solve this problem?
Thanks
C++ sources are compiled from beginning to end.
When the compiler has gotten this far:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
#include <math.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int userInput = -9999;
userInput = ReadNumber(); // <-- What is this?
It's true -- there is no evidence of ReadNumber existing.
Declare the existence of your functions before they are used.
int ReadNumber ();
void WriteAnswer(int data);
You forgot to type function prototypes.
int ReadNumber ( void );
void WriteAnswer(int );
Put them in your code before calling the functions.
In your code you try to call ReadNumber function, that hasn't been declared yet. The compiler doesn't know anything about this function:
int _tmain(int argc, _TCHAR* argv[])
{
...
ReadNumber(); // call to function ReadNumber, but what is ReadNumber ???
}
// definition of ReadNumber:
int ReadNumber ()
{
...
}
You should declare it first:
// declaration:
int ReadNumber();
int _tmain(int argc, _TCHAR* argv[])
{
...
ReadNumber(); // call ReadNumber that takes no arguments and returns int
}
// definition of ReadNumber:
int ReadNumber ()
{
...
}
You have to write a function prototype or function itself before first call of the function.
In your code compiler see call of the ReadNumber() but it doesn't know what is that function.