Strings & Arrays


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Strings & Arrays

  1. #1
    Join Date
    Nov 2008
    Posts
    26

    Strings & Arrays

    I am trying to write a program that Store 5 numbers these values into an array of int []. Using the length of the array in a for() loop and decrementing the loop counter, write the array values in reverse order to the screen. Thats the easy part This is were i am having issues add code to read 5 words from the Console (user input). Store these values into an array of string []. Make sure the string[] array is large enough to hold at least 6 values. Store the string constant “end_of_array” into the last element of the array. Using a do…while() loop, print out the 1st and 3rd letters of each word (two letters per line) using the substring function. Can someone please help out.

    #include <iostream>
    #include <string>

    using namespace std;


    const char* END_OF_ARRAY = "end_of_array";

    int main()
    {
    string theArray[6];
    theArray[5] = END_OF_ARRAY;
    cout << "Please enter 5 strings of your choice\n";
    for(int counter = 0; counter < 5; counter++)
    {
    cin >> theArray[counter];
    }

    cout << "\nThe strings are:\n";
    int counter = 0;
    do
    {
    cout << theArray[counter++] << " \n";
    } while (theArray[counter] != END_OF_ARRAY);

    cin.get();
    return 0;
    }

  2. #2
    Join Date
    Dec 2007
    Posts
    401
    Code:
    for(int counter = 0; counter < 5; counter++)
    {
        cin >> theArray[counter];
    }
    this will not work correctly if the string that the user enters contains white spaces. to accept a complete line of input (up to the newline), use std::getline

    Code:
    for( std::size_t counter = 0; counter < 5; counter++ )
    {
        std::getline( std::cin, theArray[counter] ) ;
    }
    > print out the 1st and 3rd letters of each word (two letters per line) using the substring function.

    using the substring function? this is really asinine.
    to access characters at specific positions in a string, use the array subscript [] operator.

    Code:
    std::size_t counter = 0 ;
    do
    {
       // assuming that the 'first' letter is at postion 0
      if( theArray[counter].size() >= 3 )
         std::cout << theArray[counter][0] << theArray[counter][2]  << '\n' ;
      else
        std::cout << "the string has less than three characters\n" ;
      
      ++counter ;
    
    } while( counter < 5 ) ;
    while( theArray[counter] != END_OF_ARRAY ) ;
    has a problem if the user is unkind enough to enter "END_OF_ARRAY" as one of the five strings.

  3. #3
    Join Date
    Nov 2008
    Posts
    26
    I get compiling Errors

    arrays.cpp(31) : error C2143: syntax error : missing ';' before '!='
    arrays.cpp(31) : error C2059: syntax error : ')'

  4. #4
    Join Date
    Dec 2007
    Posts
    401
    post the code for which you are getting the errors.

  5. #5
    Join Date
    Nov 2008
    Posts
    26
    It comiples thank you for all your help


    #include <iostream>
    #include <string>
    using namespace std;

    // Line 15 user inputs 5 words
    // Line 18 int set to 0 and will count 1 until it hits 5 Arrays
    // Line 28 the first [0] is the string 2nd [2] is how many letters it is going to count
    // Line 24-32 Running the do while loop that has to access the Array Boxes
    // Line 34 End_of_Array when the expression is true
    const char* END_OF_ARRAY = "end_of_array";

    int main()
    {
    string theArray[6];
    theArray[5] = END_OF_ARRAY;
    cout << "Please enter 5 strings of your choice\n";
    for (int counter=0;counter < 5;counter++)
    {
    cin >> theArray[counter];
    }

    cout << "\nThe strings are:\n";
    int counter = 0 ;
    do
    {

    if( theArray[counter].size() >= 3 )
    cout << theArray[counter][0] << theArray[counter][2] << "\n" ;
    else
    cout << "the string has less than three characters\n" ;

    ++counter ;

    } while( counter < 5 );

    cin.get();
    return 0;
    }

  6. #6
    Join Date
    Dec 2007
    Posts
    401
    > // Line 28 the first [0] is the string 2nd [2] is how many letters it is going to count

    oh, no!. i presume that this is the line you are referring to:
    Code:
    cout << theArray[counter][0] << theArray[counter][2] << "\n" ;
    here, theArray[counter] is the string (at position counter in the array)
    theArray[counter][0] is the first char in that string (at position 0).
    and theArray[counter][2] is the third char in that string (at position 2).

    and to send a single char to a stream, use << '\n' (instead of << "\n"). it is more efficient.

Similar Threads

  1. Replies: 3
    Last Post: 04-19-2008, 12:37 AM
  2. Marshal arrays of strings - how to?
    By marcellus in forum .NET
    Replies: 0
    Last Post: 11-23-2006, 08:08 AM
  3. Replies: 15
    Last Post: 05-09-2001, 05:40 AM
  4. VB Arrays and memory allocation
    By Brian Leung in forum VB Classic
    Replies: 12
    Last Post: 06-20-2000, 04:06 PM
  5. VB Arrays and memory allocation
    By Brian Leung in forum VB Classic
    Replies: 0
    Last Post: 06-20-2000, 10:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center