Thread: strings

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    strings

    look at my code,
    what is held in name[7]? a null? or random information?
    for example,,,how would i get it to print letter by letter till it reachs the end of my name , rodrigo. ? without directly telling it to stop at name[6]?
    Code:
    #include <string>
    #include <iostream>
    #include <conio.h>
    using namespace std;
    int main()
    {
         string name("rodrigo");
         cout << name[7]; 
         getch();
         }
    i tried this to find out what name[7] was holding but prints nothing.
    -
    i have tried this but i have no idea what i was doing,, lol
    Code:
    #include <string>
    #include <iostream>
    #include <conio.h>
    using namespace std;
    int main()
    {
         string name("rodrigo");
         for(int x=0; x!=NULL;x++){
         cout << name[x]; 
         endl;
         
         }
         getch();
         }
    Last edited by rodrigorules; 06-07-2006 at 06:36 PM.

  2. #2
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    The string class has a member function that returns the length of the string. use string::size() in a for loop:
    Code:
    string name = "rodrigo";
    for (string::size_type i = 0; i != name.size(); ++i)
    {
      cout << name[i] << endl;
    }
    There is a difference between tedious and difficult.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    In yourt first example you forgot that counting strts at zero in programming. So the last letter in "rodrigo" is
    name[6].

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    one more question since i have thread already open

    Code:
    #include <string>
    #include <iostream>
    #include <conio.h>
    using namespace std;
    int main()
    {
         string name("rodrigo");
         for(int x=0; x!=name.size();++x){
         cout << name[x]; 
         
         endl; /*putting this endl; makes my program not compile? why?*/
         
         }
         getch();
         }

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    Quote Originally Posted by citizen
    In yourt first example you forgot that counting strts at zero in programming. So the last letter in "rodrigo" is
    name[6].
    ya i know, i was trying to find out what is held in name[7] since rodrigo stops at name[6]

  6. #6
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    endl is used at the end of cout statements to make a new line. For example:
    Code:
    cout << "HELLO" << endl;
    You can also use:
    Code:
    cout << "HELLO\n";

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    20
    Quote Originally Posted by Sentral
    endl is used at the end of cout statements to make a new line. For example:
    Code:
    cout << "HELLO" << endl;
    You can also use:
    Code:
    cout << "HELLO\n";
    Or you could use plain
    Code:
    cout<<endl;
    OR cout<<"/n";
    The first is suggested since it also clears the stream

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM