Function that gets the last character of a string.

This is a discussion on Function that gets the last character of a string. within the C++ Programming forums, part of the General Programming Boards category; I need to know this for a notepad-like program I'm trying to make. Thanks in advance....

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    151

    Function that gets the last character of a string.

    I need to know this for a notepad-like program I'm trying to make.

    Thanks in advance.

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    1. What kind of string?
    2. What have you tried?
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    operator[] - C++ Reference

    look at the [] string operator

    put the location of the last character in there..

    . there is also http://www.cplusplus.com/reference/string/string/at/ , but its the same thing really

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    @NeonBlack I use the regular string type and I have tried the getchar() function, but i couldn't get it to work.

  5. #5
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    That's not what getchar is for. Check out rodrigo's first link and also look up string::size() and see what you can come up with.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    ok well then how do i get the last character?

  7. #7
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Quote Originally Posted by NeonBlack View Post
    That's not what getchar is for. Check out rodrigo's first link and also look up string::size() and see what you can come up with.
    Do you know how to get any arbitrary character?
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    if you know how big it is, then you know where the last character lies.

    the i-th character of a string is at string[i-1], since index starts at 0

    look at this page, it has absolutely everything you can do with a string.
    http://www.cplusplus.com/reference/string/string/

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    @neonblack uh, no..........

  10. #10
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    I'm trying to find away for the ".find" function to work, what do you think?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,125
    rodrigorules provided a link; did you read what was linked?

    If you aim is to access the last character in a string, find() is not the way. You just need to use operator[] with size() (or length()).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    sorry I didn't know that i was supposed to use both of them.

  13. #13
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    Well i now have what I wanted, but now it only saves the line where the end character is.
    here is my code
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main(){
    	string text;
    	string name;
    	ofstream test;
    	string extension;
    	ifstream view;
    	char nv;
    	char yn;
    	int c;
    	char n;
    
        cout<<"Do you want to view a file or make new one<n/v>?\n";
        cin>> nv;
        cin.ignore();
        if(nv == 'n'){
    	cout<<"Enter some text\n";
        do{
        getline(cin, text);
        c=text.size()-1;
        }while(text[c] != '~');
    	cout<<"enter the file name and path(ex. c:/users/bob/filename)\n";
        getline(cin, name);
    	cout<<"Enter the file extension(if you don't know one put '.txt'\n";
    	cin>> extension;
    	string file = name + extension;
    	test.open(file.c_str());
    	if(!test.is_open()){
    	    cout<<"This file could not be opened.\n";
    	}
    	if(test.is_open()){
    	test<<text;
    	test.close();
    	}
        }
    
        if(nv == 'v'){
            string open;
        cout<<"enter the file name and path(ex. c:/users/bob/filename)\n";
    	getline(cin, name);
    	cout<<"Enter the file extension(if you don't know one put '.txt'\n";
    	cin>> extension;
    	string file = name + extension;
    	view.open(file.c_str());
    	if(!view.is_open()){
    	    cout<<"This file could not be opened.\n";
    	}
    	if(view.is_open()){
    	while(getline(view,open))
        cout<<open<<endl;
        cout<<"Do you want to edit this file?<y/n>\n";
        cin>> yn;
        view.close();
        if(yn == 'y'){
            ofstream edit;
            cout<<"Enter some text\n";
            cin.ignore();
            getline(cin,text);
            edit.open(file.c_str());
            edit<<text;
            edit.close();
        }
        }
    	system("pause");
    }
    }

  14. #14
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,359
    You are overwriting the data, `text', for every line you process.

    Soma

  15. #15
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    yes i understand that, but I don't know how not to overwrite text. I've already tried
    Code:
    while(getline(cin,text))
    but that didn't work.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:19 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM

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