Thread: problem with cin

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    25

    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

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Should work as you discribed, but try

    Code:
    #include <iostream>
    using namespace std;
    
    int main(void){
    cout << "Hello World" << endl;
    return 0;
    }

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    25
    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

  5. #5
    Unregistered
    Guest
    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.

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    367
    It's the Standard C++ bible by Al Williams!

  7. #7
    Unregistered
    Guest
    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.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Have you saved your program source file with a cpp or c extension? It does make a difference.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    25
    well done that man.

    Now works fine....no mention of .cpp extensions in book though!!!

    Thanks again.

    Bill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with cin. Please help me.
    By Antigloss in forum C++ Programming
    Replies: 17
    Last Post: 06-06-2005, 09:50 AM
  2. Input File HELP, weird problem
    By gravity-1 in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2005, 08:43 PM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Problem with cin
    By ErionD in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 11:27 AM