Thread: i position of a string

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    104

    i position of a string

    I'm trying to work with some code like this:
    Code:
    #include <string>
    #include <vector>
    #include <iostream>
    
    using namespace std;
      int main(void){
      string letters="AAAAAA";
      int len=letters.size();
      for(int i=0; i<len; i++)cout<<" letters[i]="<<letters[i];
      return(0);
    }
    I'm expecting it to return results like
    letters[i]=A letters[i]=A letters[i]=A..... etc.
    but it returns
    letters[i]=AA letters[i]=AA letters[i]=AA

    Point is, I'm expecting it to return one(1) character,
    but it returns two(2). How do I get it to return only the
    character in the string at position i?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Could maybe try...
    Code:
    for(int i=0; i<len; i++)cout<<" letters[i]="<<letters.at(i);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    148
    This code is correct,and works as expected ( letters[i]=A letters[i]=A letters[i]=A..... etc.).


    @hk_mp5kpdw
    at is [],but with index checking.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    104
    This code is correct,and works as expected ( letters[i]=A letters[i]=A letters[i]=A..... etc.).
    I am using Borland C++ and it returns letters[i]=AA !?
    What gives?

    code:--------------------------------------------------------------------------------for(int i=0; i<len; i++)cout<<" letters[i]="<<letters.at(i);
    what is the .at() ? ie-which library is needed and what does it do? Basically, I just need more info on it?

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    That code works fine on my compiler(gcc 3.2). at( ) is a member function of the string object...you don't need a special library for it. What version of Borland are you using?

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    104
    Oh, sorry, forgot to mention that I'm using version 6 of Borland C++ Builder.
    Also, I am getting a E2285 Could not find a match for 'argument(s)* Compiler error (No C++ function could be found with parameter matching the supplied arguments. Check parameters passed to function or overload function for parameterd that are being passed.) when I try to use
    Code:
    cout<<" letters[i]="<<letters[i].at();
    Doesn't this indicate I need another library?
    Last edited by kes103; 05-29-2003 at 12:09 PM.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    It is not letters[i].at(); but rather letters.at(i);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    104
    It is not letters[i].at(); but rather letters.at(i);
    Well, that compiles and runs, but it still retuns two(2) characters instead of one!?

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    Maybe it really is just 1 character...but for some reason it's not printing correctly or is printing out of order? That output you have there isn't formatted very well anyway. This probably isn't the case...just a suggestion

  10. #10
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    and an endl at the end, just for format's sake

  11. #11
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Thats a rather strange problem... I got that code to work just fine in BCB 5, and Dev.

    Is that the code you are working with, or is there more to this program?
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  12. #12
    Registered User
    Join Date
    Dec 2001
    Posts
    104
    I don't think it would make much of a difference but here is the entire code:
    Code:
    #include <string>
    #include <cstring>
    #include <vector>
    #include <iostream>
    
    using namespace std;
    class CeyKaps {
    	public:
    	string decipher(string typed, vector <string> switches) {
        string origMess=" ";
        int nochar=typed.size();
        int noswitches=switches.size();
        for(int i=0; i<noswitches; i++){
            for(int j=0; j<nochar; j++){
               cout<<typed[j];
               if(typed[j]==switches[i][1]){
                    cout<<"yes";
                    typed(j)=switches[i][0];
               }
              else if(typed[j]==switches[i][3])typed[j]=switches[i][2];
            }
        }
        return(typed);
     }
    };
    //
    string main(void){
        string typed;
        vector <string> switches;
        typed="AAAAA";
        switches.push_back("A:B");
        switches.push_back("B:C");
        switches.push_back("A: D");
    //
        CeyKaps p;
        cout<<"Original message: "<<p.decipher(typed,switches);
        cin>>"";  //pause console
    }
    Last edited by kes103; 05-29-2003 at 10:21 PM.

  13. #13
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    This is what I'm getting for output: [15 A's] + "Original Message:" + [5 A's]

    Is this close to what you're getting? Where is the duplication occuring for you?
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  14. #14
    Registered User
    Join Date
    Dec 2001
    Posts
    104
    .
    This is what I'm getting for output: [15 A's] + "Original Message:" + [5 A's]
    I'm getting the duplication here:
    Code:
    cout<<"typed[j]="<<typed[j];
    note: I added <<"typed[j]="
    I seem to have posted an older copy of the code instead of the latest

  15. #15
    Registered User
    Join Date
    Dec 2001
    Posts
    104
    OK, I finally see it. I was printing typed or letters twice without realizing it. Changed string main to int main, but this was never the problem. Approach was correct but I had logic errors, which I've corrected and code appears to now work correctly.
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  4. Replies: 3
    Last Post: 11-03-2002, 02:14 AM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM