I need to know this for a notepad-like program I'm trying to make.
Thanks in advance.
I need to know this for a notepad-like program I'm trying to make.
Thanks in advance.
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
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
@NeonBlack I use the regular string type and I have tried the getchar() function, but i couldn't get it to work.
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
ok well then how do i get the last character?
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/
@neonblack uh, no..........
I'm trying to find away for the ".find" function to work, what do you think?
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()).
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
sorry I didn't know that i was supposed to use both of them.
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"); } }
You are overwriting the data, `text', for every line you process.
Soma
yes i understand that, but I don't know how not to overwrite text. I've already tried
but that didn't work.Code:while(getline(cin,text))