What's that int doing there? It looks like you're trying to define a toupper() function or something.Code:cout.put((int toupper(ch)));
What's that int doing there? It looks like you're trying to define a toupper() function or something.Code:cout.put((int toupper(ch)));
@Elisya
I was thinking about to use something like:
transform (ch.begin(), ch.end(), ch.begin(), toupper);
but what you're saying sounds easier, thanks.
@Cpjust
Yeah i was a bit confused 'bout it , thanks for pointing it out , i'll fix it.
Last edited by Ducky; 12-16-2007 at 03:43 AM.
I'm done with it and im glad.
Im gonna use it to write subtitles and want to offer it to those who are writing them also.
It seems to work with other extentions than .txt like .srt...
Thanks again for the help for everybody.
Code:#include <iostream> #include <fstream> using namespace std; int main() { cout << "Enter file name : "; char FileName[99]; cin >> FileName; ifstream TheFile(FileName); ofstream TheCopy; TheCopy.open ("YourUppercasedCopy.srt",ios::app); string ch; while (getline(TheFile,ch)) { ch[0] = (char)toupper(ch[0]); TheCopy << ch << endl; } TheCopy.close(); TheFile.close(); }
Last edited by Ducky; 12-16-2007 at 04:45 AM.
I tried to do it with std::string first but it didnt compile that way i dont know why.
And upping every word was just a step in between because i didnt know how to up only the first word.Code:cout << "Enter file name : "; string FileName; cin >> FileName; ifstream TheFile(FileName); <-- compile error here
Last edited by Ducky; 12-16-2007 at 05:58 AM.
string FileName;
cin.get (FileName);
Doesn't compile either.
References
Code:std::string fileName; std::getline(std::cin, fileName); std::ifstream fin(fileName.c_str());
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Cool, thanks.
string fileName;
getline(cin,fileName);
ifstream TheFile(fileName.c_str());
It needed .c_str. Never heard of it.
C style casts are discuraged in C++.Code:ch[0] = (char)toupper(ch[0]);
C++ casts make your intent (and effect) more explicit. So I'd probably change that line to:
You're also missing some header files like <string> and <cctype>Code:ch[0] = static_cast<char>( toupper(ch[0]) );
Thanks Cpjust , i changed my code like you said.
I also liked to add a check if the letter is uppercase after a dot "." .
Im just not sure how to go about it.
Can i do the two things in one while() statement or shall i have to use two?
Can i use only getline() for the two or i need getline() and get()?
Should i use three files for it or i can do it with two?
Take File1 , uppercase every line , put it in File2
Take File2 , uppercase after "." put it in File3
Code:#include <iostream> #include <fstream> #include <string> #include <cctype> using namespace std; int main() { cout << "Enter file name : "; string FileName; cin >> FileName; ifstream TheFile(FileName.c_str()); ofstream TheCopy; TheCopy.open ("Your_Uppercased_Copy.srt",ios::app); string str; size_t offset; while (getline(TheFile,str)) { str[0] = static_cast<char>( toupper(str[0])); TheCopy << str << endl; } while((offset = TheFile.find(".")) != -1) { str = put.(char( toupper(offset + 1)); TheCopy << str << endl; } TheCopy.close(); TheFile.close(); }
Last edited by Ducky; 12-19-2007 at 05:36 AM.
TheFile is a ifstream which doesn't have the find method. You can find things in a string. Then it would be wiser to compare against std::string::npos which may or may not equal signed value -1.Code:while((offset = TheFile.find(".")) != -1)
However, you should do all the capitalization before you output anything to TheCopy - in the first while loop where you actually have the string that you want to work with.
By the way, if this is normal text I'd expect a space after '.' unless there is a typographical error. If that is the case you might need to adjust your logic to find the first non-whitespace character after a full stop. You also fail to take into account that '.' may be the last character of the string.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Ok, thanks Anon!
I didnt find out how to use std::string::npos to find a character in a string but i find this method.
Ok now what i can do is up the first letter of a line and up after any(one) character i want.
What i cant seem to find is how to uppercase after two characters like ". " (dot and space).
If i add "(|| str[i] == '. ')" to "if (str[i] == '.')" it's not working. Why?Code:int main() { cout << "Enter file name : "; string FileName; cin >> FileName; ifstream TheFile(FileName.c_str()); ofstream TheCopy; TheCopy.open ("Your_Uppercased_Copy.srt",ios::app); string str; char ch; while (getline(TheFile,str)) { str[0] = static_cast<char>( toupper(str[0])); for(int i = 0; str[i]; i++) if (str[i] == '.') str[i+1] = static_cast<char>( toupper(str[i+1])); TheCopy << str << endl; } TheFile.close(); TheCopy.close(); }
To get this right... what you're saying is that given input like:What i cant seem to find is how to uppercase after two characters like ". " (dot and space).
You expect output like:Code:hello .world 123 abc 5678
Is this correct?Code:Hello .World 123 Abc 5678
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)