is it possible to have a string into an array?

This is a discussion on is it possible to have a string into an array? within the C++ Programming forums, part of the General Programming Boards category; i have a cin.getline string that i want to place into an array , is it possible? if it isnt. ...

  1. #1
    yorge
    Guest

    is it possible to have a string into an array?

    i have a cin.getline string that i want to place into an array , is it possible? if it isnt. is there another way i can do it?

    here is a bit of the code. i need to put the input (string) into the array.

    void newperson ()
    {
    int count, numppl;
    cout<<"How many people do you want to introduce?"<<endl;
    cin>>numppl;
    for (count=0 ; count <=numppl ; count=count +1)
    {
    cin.getline (input, 200, '\n');
    }

    }

  2. #2
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    Posts
    161
    a string is an array.
    you can get a specific value in the array using brackets (example: [])

    if you want some number to become some array...
    here is a very basic sample code segment I made:

    Code:
    int numberarray[32];
    int number;
    int numbercopy;
    int counter;
    
    cin>>number;
    
    numbercopy = number;
    
    // transfer value from int to array
    // note: the numbers are stored backwards
    for(counter=0;number>0 && counter<32;counter++)
    {
     numberarray[counter] = numbercopy % 10;
     numbercopy %= 10;
    }
    
    // search for the last value of the array
    int arraylength;
    for(counter=0;counter<32;counter++)
    {
     if (numberarray[counter]=='\0') // '\0' = NULL
     {
      arraylength=counter;
      break;
     }
    }
    
    // reverse order of array using "swap" method
    int temp;
    for(counter=0;counter<=arraylength/2;counter++)
    {
     temp=numberarray[arraylength - counter];
     numberarray[arraylength - counter] = numberarray[counter];
     numberarray[counter] = temp;
    }
    
    // print out the array
    for(counter=0;counter<arraylength;counter++)
    {
     cout<<numberarray[counter];
    }
    cout<<endl;
    
    // convert the number array back to type integer;
    for(counter=0;counter<arraylength;counter++)
    {
     numbercopy = numbercopy * 10 + numberarray[counter];
    }
    Last edited by toaster; 04-26-2002 at 10:42 PM.
    think only with code.
    write only with source.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    Yes you can here is a sample program If you don't have string.h then it gets a bit more complicated I think....

    Code:
    #include <iostream.h>
    #include <string.h>
    
    int main()
    {
      string names[5];
      names[0]="Arminas";
      names[1]="Lostminds";
      names[2]="says";
      names[3]="yes";
      for (int x=0;x<4;x++)
        cout << names[x] <<' ';
      cout << "\n\nWhat is your name?\n";
      cin >> names[4];
      cout << "\n\n\t" << names[4] << " that is a good name.";
      getchar();
    }//end main
    "The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    200
    I dont know very much about C++ but I think if you write like so
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        string mystring;
        cout<<"enter a string"<<endl;
        cin>>mystring;
        return 0;
    }
    then mystring is stored in an object of type string that does a lot of bookkeeping for you.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    and if you want an array of strings its:
    Code:
      string customer_names[MAX];
    to access the first letter of the first word it is:
    Code:
      cout << customer_names[0][0];
    to access the first word it is just : customer_names[0];
    "The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."

  6. #6
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    doing forget to #include <string> before declaring variables of type 'string'... if you don't you'll get errors.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 07:04 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

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