-
problem with cin
Hi,
This is my first day at trying to teach myself C++, although I do have experince of C.
The problem that I have is that using Microsoft Developer Studio, when I include <iostream.h>, the compiler won't recognise "cin" or "cout", even though it seemingly can find the header file.
However, it does find all C++ keywords, such as "try" and "private", so I am thinking that the compiler is set up correctly.
Any tips much appreciated.
Bill
-
Should work as you discribed, but try
Code:
#include <iostream>
using namespace std;
int main(void){
cout << "Hello World" << endl;
return 0;
}
-
Can you post your code and any errors/warnings the compiler is giving you? One thing to try would be using the current standard C++ which omits the .h extension from header files and takes advantages of namespaces:
Code:
#include <iostream>
int main()
{
std::cout<<"Hello, World!\n";
return 0;
}
-Prelude
-
the code is.....
#include <iostream.h>
// -------------
// PROTOTYPES
// -------------
int calculateArea(int height, int width, int depth);
// -------------
// FUNCTIONS
//--------------
int main() {
int height;
int width;
int depth;
int totalArea;
cout << "Please insert height of box:";
cin >> height;
cout << "\nPlease insert width of box:";
cin >> width;
cout << "\nPlease insert depth of box:";
cin >> depth;
totalArea = calculateArea(height, width, depth);
cout << "Total area is " << totalArea;
return 0;
}
int calculateArea(int height, int width, int depth) {
return (height*width*depth);
}
The errors are:-
error C2065: 'cout' : undeclared identifier
error C2297: '<<' : bad right operand
error C2065: 'cin' : undeclared identifier
error C2297: '<<' : bad right operand
error C2297: '<<' : bad right operand
error C2297: '<<' : bad right operand
i haven't yet come across 'namespaces' so i don't really want to use them. According to the book, the above code should work.
Thanks
Bill
-
but the book and the compiler may not be of the same vintage. That is, the book may not use namespaces routinely, but the compiler may rquire that you do so. It's not difficult to use namespaces in a "lazy" way (that is with the using namespace std line), and then learn the ideal syntax later (that is with the std:: cout do dah) when you come to that section in the book you are using.
-
It's the Standard C++ bible by Al Williams!
-
so what? I haven't heard of it. Even if I had, if it was published before about 1998 it probably doesn't use namespaces routinely. If you just need to add the using namespaces std line and the code compiles, count your blessings. There are so many versions of compilers and different editions of books available that having a disconnect between them is not at all unusual. Wait til you try learning about Windows using a book that is written for VC++ and a compiler that is made by BCB. You soon learn how to make adjustments.
-
Have you saved your program source file with a cpp or c extension? It does make a difference.
-
well done that man.
Now works fine....no mention of .cpp extensions in book though!!!
Thanks again.
Bill