string into 2 dimensional array - basic stuff

This is a discussion on string into 2 dimensional array - basic stuff within the C++ Programming forums, part of the General Programming Boards category; I'm trying to input multiple strings into an array. I tried using 2 dimensional array Code: #include<iostream.h> int main() { ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    37

    string into 2 dimensional array - basic stuff

    I'm trying to input multiple strings into an array.

    I tried using 2 dimensional array

    Code:
    #include<iostream.h>
    
    int main()
    {
    
     const int ROWS=3;
     const int COLS=20;
    
     char array[ROWS][COLS];
    
     cout<<"enter some text: "<<endl;
     cin.getline (array, ROWS, '\n');
    ......
    I know the mistake is right there at cin.getline , but I can't stuff anymore parameters into that. I know you can use a for loop for numbers, but string is different.

    I tried using normal arrays

    Code:
    ....
    const int MAX=30;
    char array[MAX];
    char indicator ='y';
    
    while (indicator=='y' || indicator =='Y')
    {
      cout<<"enter some text: "<<endl;
      cin.getline(array, MAX, '\n');
      cout<<"continue? (y/n)?: ";
      cin>> indicator;
    }
    
    cout<<array;
    ...
    The "while loop" doesnt work for some reason. I just get this:

    Code:
    output: enter some text:
    intput: abc
    output: continue?(y/n)?:
    input: y
    output: enter some text:
                 continue?(y/n)?
    why does it loop only once??
    Last edited by mellisa; 01-17-2003 at 08:56 PM.

  2. #2
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    Post some of the errors you're getting.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    37
    for the 2 dimensional array, the error was:

    Code:
    error C2664: 'class istream &__thiscall istream::getline(char *,int,char)' 
    : cannot convert parameter 1 from 'char [3][20]' to 'char *'
     
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    Error executing cl.exe
    there wasnt any errors or warnings for the 2nd one.

  4. #4
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    you have intialised char indicator ='y' to y. Then the code hits the while condition which is while (indicator=='y' || indicator =='Y'). This is true, and it will therefore not be processed. So it just skips the while, and outputs the array contents. whatever it may be.
    Be a leader and not a follower.

  5. #5
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    sorry just ignore that last post, i've had a few to many...............................
    Be a leader and not a follower.

  6. #6
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    getline() reads up to a '\n', but doesn't remove it from the input buffer.

    call cin.ignore() before the end of the loop.
    eg:

    Code:
    while (tolower(indicator) =='y')
    {
      cout<<"enter some text: "<<endl;
      cin.getline(array, MAX, '\n');
      cin.ignore();
      cout<<"continue? (y/n)?: ";
      cin>> indicator;
    }
    As for your problem with the multi-dimensional array...

    Code:
    int count = 0;
    while (tolower(indictator) == 'y' && count < ROWS)
    {
       cout<<"enter some text: "<<endl;
       cin.getline(array[count], COLS-1);
       cin.ignore();
       cout<<"continue? (y/n)?: ";
       cin>> indicator;
    }
    Or, you could really flex C++ and use an std::vector<std::string> type.

    Code:
    std::vector<std::string> list;
    std::string temp;
    do
    {
       cout << "enter some text: ";
       std::getline(std::cin, temp);
       list.push_back(temp);
    } while (temp[0]);
    Last edited by Eibro; 01-17-2003 at 09:21 PM.

  7. #7
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    Use a string instead...

    #include <string>

    string charStr;

    This is solved in the C-board. Cross-post.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    37
    thanks everyone,

    i got abit of help in C forum too... but this is what I want now.

    Code:
    #include <iostream.h>
    
    int main(void)
    {
         const int MAX = 100;
         char array[MAX][81];
         int i,c=0;
    
    
         for (i=0, c='a'; i<MAX && c!='#'; i++)
    		{
    		  cout << "enter a string or hit # to stop: ";
         	                 cin.getline(array[i], 81, '\n');		  
    				  
    		 }
    
    	 
    
                   for(i=0; i < MAX; i++)
         	cout << array[i] << endl;
         return 0;
    }
    it needs to loop 100 times until user hit the "#" key. But that didnt work.

  9. #9
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    This should do it
    Code:
    int main()
    {
       const int MAX = 100;
       char array[MAX][81];
       int i;
    
     
       for (int i = 0; i<MAX; i++)
       {
          cout << "enter a string or hit # to stop: ";
          cin.getline(array[i], 80, '\n');
          if (array[i][0] == '#') break;		  
    				  
       }
    	 
    
       for(i = 0; i < MAX; i++)
          cout << array[i] << endl;
       return 0;
    }

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    37
    this worked

    Code:
       for (i = 0; i<MAX; i++)
       {
          cout << "enter a string or hit # to stop: ";
          cin.getline(array[i], 80, '\n');
          if (array[i][0] == '#') 
    		  return 0;		  
    				  
       }
    but that's very bad programming they said... having 2 exit points ... ah well , until someone have something better

  11. #11
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    did u see Eibro's post??
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    37
    yes, he had a "break" and that didnt work.
    anyway, this seem to work too

    Code:
    #include <iostream.h>
    int main()
    {
    	 const int MAX = 100;
    	 char array[MAX][81] = {'\0'};
    	 int i;
    	 int endLoop = 0;
    	 for (i = 0; i<MAX && endLoop == 0; i++)
    	 {
    		cout << "enter a string or hit # to stop: ";
    		cin.getline(array[i], 80, '\n');
    		if (array[i][0] == '#')
    		{
    		  endLoop = 1;
    		  array[i][0] = '\0';
    		}
    	 }
    	 for(i = 0; i < MAX; i++)
    	 cout << array[i] << endl;
    	 return 0;
    }
    oh about the CROSS-POST ! sorry , wasnt done one purpose. After posting in C, i realised "oh wat? this is not C++ forum" hehe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 02:02 AM
  2. Copying a string, into a string array.
    By m.mixon in forum C Programming
    Replies: 5
    Last Post: 07-31-2006, 06:19 PM
  3. can't assign proper values to an array of string
    By Duo in forum C Programming
    Replies: 1
    Last Post: 04-04-2005, 07:30 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21