Thread: Array/cin.getline question

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    Question Array/cin.getline question

    Hi,

    Had a question about a couple little array feeding programs I've been trying to get working. These were posted on this board (by someone named Mookie??) in answer to another question I asked about arrays.

    Code:
    // ArrayHelp3.cpp
    //having the same problem as ArrayHelp4.cpp
    //Output:
    // Enter some text: here's my text
    //
    // Enter some text: Continue? (y/n):
    // Enter some text: Continue? (y/n):
    // Enter some text: Continue? (y/n):
    // etc. 
    // Something not initialized???
    
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    { const int MAXSETS = 5;
      char txtArray[MAXSETS][81] = {'\0'};
      int i = 0;
      char choice = 'y';
        
      do
      { cout << "Enter some text: ";
        cin.getline(txtArray[++i], MAXSETS, '\n');
        cout << "Continue? (y/n): ";  cin >> choice; 
        //choice = (tolower(choice));
        cout << endl;
      } while(choice == 'y');
      
      if(choice != 'y')
      { for(i=0; i<MAXSETS; i++)
          cout << txtArray[i] << endl;
      }
        
      system("pause");
      return 0;
    }

    Here's another one with a similar problem if not the same one:


    Code:
    // ArrayHelp4.cpp
    //when it gets to the while loop, it somehow skips the cin.getline
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    { const int MAXSETS = 5;
      char Array[MAXSETS][15] = {'\0'};
      int count = -1;
      char choice = 'y';
        
      for(int j=0; j < MAXSETS && choice == 'y'; j++)
                          //while(choice == 'y' && count < MAXSETS)              
      { cout << "\nEnter some text: "; 
        cin.getline(Array[++count], 15, '\n');
                                        //cin.ignore();
        cout << "\nContinue? (y/n): ";
        cin >> choice;
                                        //if(choice != 'y')
      }
      
      cout << "\n";  
        
      for(int i=0; i<MAXSETS; i++)
        cout << "Array[" << i << "] " << Array[i] << "\n";
        
      system("pause");
      return 0;
    }

    Thanks for any help/suggestions you can give.

    Swaine777

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    cin.getline(txtArray[++i], MAXSETS, '\n');

    That line is a problem. First, ++i sets i equal to 1 the first time through the loop, and it should be equal to 0. Change ++i to i++.

    Second, MAXSETS is equal to 5. That means cin.getline() will read a maximum of 4 char's and it reserves the last index position for a '\0' that it tacks onto the end of every string. Replace MAXSETS with the size of the array of the first argument, which is 81.

    However, that program still has other problems. First of all, there are only 5 char arrays to read data into, and there is nothing preventing the user from hitting yes more than 4 times.

    Also, whenever you use cin>> and then subsequently use getline(), you are going to have problems you need to address. To read about the problem and a solution, Click on 4 here:

    http://www.augustcouncil.com/~tgibso...al/iotips.html
    Last edited by 7stud; 06-08-2003 at 04:00 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM