seems like my "tolower()" is not working

Hello im doing this exercise where i have to open 1 text file and if a letter is uppercase i have to convert it to lowercase and write new content in another file.
So i wrote this function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Bad{};

void file_to_lower(string file_name){

	ifstream ifs{ file_name+".txt" }; 
	if (!ifs) throw Bad{};
	ofstream ofs{ file_name + "_lower.txt" };
	if (!ofs) throw Bad{};
	char ch;
	while (ifs.get(ch)){
		if (isupper(ch)) tolower(ch);

		ofs << ch;
	}

}


The problem is that im getting new file but the content is the same. Still all upper case characters - not a single 1 converted. Why is this happening? Is "tolower()" even working? Or what did i do wrong?

PS: i tested that i successfully got inside my if statement so as far as i understand char should get transformed into lower case one.


All code together if it means anything
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class Bad{};

void file_to_lower(string file_name){

	ifstream ifs{ file_name+".txt" }; 
	if (!ifs) throw Bad{};
	ofstream ofs{ file_name + "_lower.txt" };
	if (!ofs) throw Bad{};
	char ch;
	while (ifs.get(ch)){
		tolower(ch);
		if (isupper(ch)) tolower(ch);

		ofs << ch;
	}

}



int main(){

	bool done = false;
	while (!done){
		try{
			string name;
			cout << "Enter file name to open  ";
			cin >> name;
			file_to_lower(name);
			cout << "new file created : " << name + "_lower.txt\n";
			done = true;
		}
		catch (Bad b){
			cout << "Sorry file could not be opened, try again\n\n";
		}
	}
	system("pause");

}
Last edited on
tolower takes it's argument by value and returns the lower case version. You discard the returned value. Do something with it instead.
just as Mr cire said, u need to store the result returned from the function tower()
you could replace if (isupper(ch)) tolower(ch); with if(isupper(ch)) ch = tolower(ch);
Thank you very much, this gives me good lesson to check out what function actually does :)
Thanks to both of u guys :)
Last edited on
Topic archived. No new replies allowed.