Thread: I'm REALLY confused. Why isn't cout or cin working here?

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    11

    I'm REALLY confused. Why isn't cout or cin working here?

    I'm creating a program that fills arrays with random numbers, then sorts them in ascending and descending order. I have no problem with that. My question is, why am i getting errors that cout and cin are undeclared in my code. I try using #include <iostream>, but that gives me an even longer list of errors. Here's my code: (Thanks for reading)

    Code:
    #include <cstdlib>
    #include <iomanip>
    using namespace std;
    
    const int SIZE = 200;
    
    void initialize_array(int random_ints[], int num_elements, int bound);
    
    int main(void)
       {
       int original_data[SIZE];
       int copy_data[SIZE];
       int num_values = 0;
       int bound = 0;
    
       cout << "Number of random data values in array (0-200)? ";
       cin >> num_values >> endl;
       cout << "Bound on maximum value? ";
       cin >> bound >> endl;           
    
    
       return 1;
       }
    
    void initialize_array(int random_ints[], int num_elements, int bound)
       {
       int seed_value = 0;
    
       cout << "Random seed value? ";
       cin >> seed_value >> endl;
    
       srand(seed_value);
       for(int i = 0; i < num_elements; i++)
          {
          random_ints[i] =  rand();
          srand(random_ints[i]);
          i++;        
          }
       }

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    compiler? What errors? iostream should be #include'd because that is where cout and cin, and endl are declared in the std namespace.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    11

    More info

    I'm using g++ in Unix, Solaris i believe.

    Here's the errors:

    assign2.cc: In function `int main()':
    assign2.cc:16: `cout' undeclared (first use this function)
    assign2.cc:16: (Each undeclared identifier is reported only once for each
    function it appears in.)
    assign2.cc:17: `cin' undeclared (first use this function)
    assign2.cc:11: warning: unused variable `int original_data[200]'
    assign2.cc:12: warning: unused variable `int copy_data[200]'
    *** Error code 1
    make: Fatal error: Command failed for target `assign2'

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    I don't know if g++ in Unix allows you to cin >> endl, but fixing those errors on winXP, there were no more errors.

    Actually, I think I just caught it, you may have to save it as a .cpp file, I don't know if Unix saves .cpp files as .cc, but if it doesn't, then I think that would be your error.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    #include <iostream> is required. What errors are you getting when you use that?

    Try using std::cout and std::cin and std::endl
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    >> using namespace std;

    he doesnt need to use std:: if he declares the namespace
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    11

    Not at all what I thought

    I found the problem with my code, and the reason I was getting about 10 pages of errors:

    cout << "Number of random data values in array (0-200)? ";
    cin >> num_values >> endl;
    cout << "Bound on maximum value? ";
    cin >> bound >> endl;

    I put my endl in the stream of the cin statement.

    I switched the code to look like this:

    cout << "Number of random data values in array (0-200)? ";
    cin >> num_values;
    cout << endl; << "Bound on maximum value? ";
    cin >> bound;
    cout << endl;

    and it compiled.

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    great to hear it compiled. I caught that, not sure if you saw me post about it though. Yeah, sometimes errors are like that.

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    11

    i didn't catch that

    Sorry about that, I guess I just read it too fast or something, thanks for the help though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with cout
    By kristentx in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2006, 12:37 PM
  2. cin not allowing input after first use of function
    By Peter5897 in forum C++ Programming
    Replies: 5
    Last Post: 01-31-2006, 06:29 PM
  3. ARG! If statements with cin NOT WORKING!
    By bladerunner627 in forum C++ Programming
    Replies: 6
    Last Post: 09-26-2005, 10:59 PM
  4. Why isn't my program working?
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 01-21-2002, 02:10 PM
  5. STL to: Cin, Cout, ifstream & ofstream
    By kuphryn in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2001, 09:32 AM