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)));
Printable View
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.
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();
}
Using >> to a char buffer is not typically safe (try std::string instead).
And you're only upping the first letter of each row, in case you wanted to up every letter of every word.
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
No, of course not... The C++ I/O really could have been done better.
You can use cin.get or cin.read to fetch X amount of characters from the stream. That way you won't have a buffer overrun if the filename is too long.
string FileName;
cin.get (FileName);
Doesn't compile either.
Needs a char array, and also specify the size of the array.
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();
}
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.
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:Quote:
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