Thread: Pointer arrays, oo, fun! >.< URG.

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    155

    Pointer arrays, oo, fun! >.< URG.

    Code:
    #include <iostream.h>
    #include <conio>
    using namespace std;
    
    int main(){
    	char array[10];
    
    	for (int i = 0; i <= 9; i++){
    		cout << "Line " << i + 1 << ": ";
    		cin.getline(*(array + i));
    		//while(cin.get() != '\n'){}
    		//cin.ignore();
    		//getch();
    		cout << endl;
    	} 
    	
    	cout << endl;
    	
    	for (int i = 0; i <= 9; i++)
    		cout << "Line " << i + 1 << "contains string: " << *(array + i) << endl;
    	
    
        return 0;
    } //end int main
    PROBLEM:
    Write a function that accepts ten lines of user-input text and stores the entered lines as ten individual strings. Use a pointer array in your function.

    REAL PROBLEM: I can't clear the input buffer correctly. I've tried everything. Cin.getline won't work either

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    I am also not sure if I am using a pointer array

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    No offence friend, but I think you're getting a little ahead of yourself! You need to master the basics of this language first.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    try this.

    Code:
    #include <iostream.h>
    
    #define PTR_ARR_LEN 10
    #define STR_LEN 20
    
    int main()
    {
      char *Array[PTR_ARR_LEN];
    
      for (int i = 0; i <= PTR_ARR_LEN - 1; i++)
      {
        Array[i] = new char[STR_LEN];
        cout << "Line " << i + 1 << ": ";
        cin.getline(Array[i], STR_LEN);
        cout << endl;
      }
    
      for (int i = 0; i < PTR_ARR_LEN; i++)
        cout << "Line " << i + 1 << " contains string: " << Array[i] << endl;
    
      getchar();
    
      return 0;
    }
    Be a leader and not a follower.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    Now see, this just makes no sense. My code had similar syntax in that foor loop earlier but the input buffer would not budge...

    Does this have to do with the = new char[] thing? What's that do?

  6. #6
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    Before you had just declared an array of char pointers, but these pointers were not allocated any memory. They were just a load of variables holding random data, so before when you were trying to use cin.getline e.g. entering "hello", and you were putting this into array[0], there was no memory allocated to fit "hello".
    Therefore, when you say:
    Array[i] = new char[STR_LEN]; I would char pointer number one to hold a size string of 20 chars. Hope this helps a bit.
    Be a leader and not a follower.

  7. #7
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    I think Sebastiani was right.
    Be a leader and not a follower.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer arrays!!!
    By condorx in forum C Programming
    Replies: 1
    Last Post: 05-14-2002, 08:55 AM
  2. pointer arrays
    By condorx in forum C Programming
    Replies: 3
    Last Post: 05-03-2002, 09:04 PM
  3. Replies: 4
    Last Post: 11-05-2001, 02:35 PM
  4. Pointer of arrays - how do I access their elements?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-11-2001, 11:28 PM
  5. pointer to pointers to arrays of strings??
    By Binkstone in forum C Programming
    Replies: 8
    Last Post: 09-14-2001, 02:56 AM