Function to make output lowercase or uppercase wont run
Nov 6, 2019 at 12:20am UTC
Hi there,
my program supposed to convert output to either uppcase or lowercase and count commas and consonants.
My code can run but when I do cout in the main function, the lower and upper function wont do anything.
here's my attempt.
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;
int CountCommas(char m[])
{
int commas = 0;
for (int i = 0; i < strlen(m); i++)
{
if (m[i] == ',' )
commas++;
else ;
}
return commas;
}
int CountCons(char m[])
{
int cons = 0;
for (int i = 0; i < strlen(m); i++)
{
if (m[i] == 'a' || m[i] == 'o' || m[i] == 'e' || m[i] == 'i' || m[i] == 'u' ||
m[i] == 'A' || m[i] == 'O' || m[i] == 'E' || m[i] == 'I' || m[i] == 'U' );
else if (m[i] >= 'a' && m[i] <= 'z' || m[i] >= 'A' && m[i] <= 'Z' )
cons++;
}
return cons;
}
void ConvToUp(char m[])
{
for (int i = 0; i < strlen(m); i++)
toupper(m[i]);
}
void ConvToLo(char m[])
{
for (int i = 0; i < strlen(m); i++)
tolower(m[i]);
}
int main()
{
char fn[80];
int cons = 0, commas = 0, choice;
cout << "Please enter file name: " ;
cin >> fn;
cout << "Input 1 for lower case output and 2 for upper case output" << endl;
cin >> choice;
ifstream infile(fn);
if (!infile) cout << "Invalid file" << endl;
else
{
char line[80];
while (infile.getline(line, 80))
{
if (choice == 1)
{
ConvToLo(line);
}
else if (choice == 2)
{
ConvToUp(line);
}
cout << line << endl;
commas += CountCommas(line);
cons += CountCons(line);
}
infile.close();
}
cout << endl;
cout << "Commas: " << commas << endl;
cout << "Consonant: " << cons << endl;
return 0;
}
Nov 6, 2019 at 12:36am UTC
You are not assigning the result of
tolower and
toupper to the current string's element in your conversion functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <iostream>
#include <cstring>
void ConvertToUpper(char []);
int main()
{
char str[] { "A StRiNg" };
ConvertToUpper(str);
std::cout << "The string is: " << str << '\n' ;
}
void ConvertToUpper(char str[])
{
for (int i = 0; i < strlen(str); i++)
{
str[i] = toupper(str[i]);
}
}
The string is: A STRING
Nov 6, 2019 at 12:46am UTC
wow, i see, thank you very much.
Topic archived. No new replies allowed.