I know there were a couple of other questions on this from a while back, but I looked over those and still couldn't figure it out. I'm trying to take user input in the form of a string, then loop through that string converting all of the uppercase to lowercase so that I can display it all in lowercase.
Where am I going wrong?
int main()
{
cout << "Enter Text: ";
string Text;
getline(cin, Text);
for(int i=0; i<Text.length(); i++)
{
if(islower(Text[i]) == false)
{
tolower(Text[i]);
i++;
}
Text[i] = Text[i];
}
cout << "Your text is: ";
cout << Text;
cout << "\n";
}
I'm very new to C++ and I'd be lying if I said I had much of an idea even where I was going wrong. Line 11, where the for loop is says that it's trying to compare two different signs, but I don't know what that means or if that's the source of my problem. Line 15 where the tolower() is says that it's 'ignoring return value of function declared with pure attribute' but I still don't know what that means either.
Please help.
A few points:
tolower returns the lowercase character if it exists ('A' becomes
'a', 'a' is unchanged, '9' is unchanged, etc.)
The line Text[i] = Text[i]; does not do anything, you want Text[i]
= tolower(Text[i]);
There is no need to check if each character is lowercase, tolower
will handle that for you
Simplified:
#include <iostream>
using namespace std;
int main() {
cout << "Enter Text: ";
string Text;
getline(cin, Text);
for (int i = 0; i < Text.length(); i++)
Text[i] = tolower(Text[i]);
cout << "Your text is: ";
cout << Text;
cout << "\n";
}
I'd suggest using the std library algorithm function transform to simplify and to make the code easier to read for you and others.
#include <iostream> //for cout and getline
#include <algorithm> //for transform
int main()
{
cout << "Enter Text: ";
string Text;
getline(cin, Text);
//This will iterate over each character [Text.begin()-Text.end()] and then
//replace it by a call to tolower with itself as a parameter
transform(Text.begin(), Text.end(), Text.begin(), ::tolower);
cout << "Your text is: ";
cout << Text;
cout << "\n";
}
EDIT:
As Remy pointed out to correct way of implenting this is by using a proxy lambda since
the behavior of std::tolower is undefined if the argument's value is
neither representable as unsigned char nor equal to EOF.
transform(Text.begin(), Text.end(), Text.begin(),
[](unsigned char c){ return std::tolower(c); });
I'd recommend looking at the ascii tables to see the codes for upper case and lower case characters.
http://www.asciitable.com/
bool islower(char in)
{
return !(char >= 'A' && char <= 'Z'); //we do this so if its a not an alphabet character we don't get false positives
}
char tolower(char in)
{
return char-'A' + 'a'; //essentially get its distance from the start of the
//alphabet and add this distance to the lowercase (only works on uppercase)
}
Its all just about working with the ascii values to get what you want since all characters are essentially integers.
Related
I need to write a sentence in Pig Latin form and I am almost done it successfuly except for 1 case and I almost give up
for example :
If my word starts at a\e\o\u\i the word will look like easy -> easyway , apple -> appleway
and if it doesnt start with a letter that I wrote above
it will look like that: box -> oxbay , king -> ingkay
I succeed with the bolded part but in the first part with a\e\o\u\i letter at the beginning , I dont know where to put the w before and need some help with it
This is my code , thanks in advance
#include <iostream>
//Since those are used in ALL function of program, it wont hurt to set it to global
//Else it is considered EVIL to declare global variables
const int maxLine = 100;
char phraseLine[maxLine] = { '\0' };
void pigLatinString();
using namespace std;
void main()
{
// Displayed heading of program
cout << "* You will be prompted to enter a string of *" << endl;
cout << "* words. The string will be converted into *" << endl;
cout << "* Pig Latin and the results displayed. *" << endl;
cout << "* Enter as many strings as you would like. *" << endl;
//prompt the user for a group of words or press enter to quit
cout << "Please enter a word or group of words. (Press enter to quit)\n";
cin.getline(phraseLine, 100, '\n');
cout << endl;
// This is the main loop. Continue executing until the user hits 'enter' to quit.
while (phraseLine[0] != '\0')
{
// Display the word (s) entered by the user
cout << "You entered the following: " << phraseLine << endl;
// Display the word (s) in Pig Latin
cout << "The same phrase in Pig latin is: ";
pigLatinString();
cout << endl;
//prompt the user for a group of words or press enter to quit
cout << "Please enter a word or group of words. (Press enter to quit)\n";
cin.getline(phraseLine, 100, '\n');
}
return;
}
void pigLatinString() //phraseLine is a cstring for the word, maxline is max length of line
{ //variable declarations
char tempConsonant[10];
tempConsonant[0] = '\0';
int numberOfConsonants = 0;
char previousCharacter = ' ';
char currentCharacter = ' ';
bool isInWord = 0;
// for loop checking each index to the end of whatever is typed in
for (int i = 0; i < maxLine; i++)
{
//checking for the end of the phraseline
if (phraseLine[i] == '\0')
{//checking to see if it's in the word
if (isInWord)
{//checking to see that there wasn't a space ahead of the word and then sending the cstring + ay to the console
if (previousCharacter != ' ')
cout << tempConsonant << "ay" << endl;
}
return;
}
// this covers the end of the word condition
if (isInWord)
{// covers the condition of index [i] being the space at the end of the word
if (phraseLine[i] == ' ')
{
// spits out pig latin word, gets you out of the word, flushes the temp consonants array and resets the # of consonants to 0
cout << tempConsonant << "ay";
isInWord = 0;
tempConsonant[0] = '\0';
numberOfConsonants = 0;
}
cout << phraseLine[i] ;
}
else
{//this covers for the first vowel that makes the switch
if (phraseLine[i] != ' ')
{// sets the c string to what is in the phraseline at the time and makes it capitalized
char currentCharacter = phraseLine[i];
currentCharacter = toupper(currentCharacter);
// this takes care of the condition that currentCharacter is not a vowel
if ((currentCharacter != 'A') && (currentCharacter != 'E') &&
(currentCharacter != 'I') && (currentCharacter != 'O') && (currentCharacter != 'U'))
//this sets the array to temporarily hold the consonants for display before the 'ay'
{//this sets the null operator at the end of the c string and looks for the next consonant
tempConsonant[numberOfConsonants] = phraseLine[i];
tempConsonant[numberOfConsonants + 1] = '\0';
numberOfConsonants++;
}
else
{// this sets the boolean isInWord to true and displays the phraseline
isInWord = 1;
cout << phraseLine[i];
}
}
else
{
cout << phraseLine[i] ;
}
}
previousCharacter = phraseLine[i];
}
return;
}
You have two conditions to consider. if your word starts with a vowel, just add "way" to the end of the word, else move the first letter and add "ay" to the end.
This is a task that can be made a lot simpler by using std::string instead of C-strings. This is because you are now no longer concerned with exceeding your length or losing the null character. It also allows easier access to the Standard Library algorithms.
#include <algorithm>
#include <iostream>
#include <string>
std::string make_pig_latin(const std::string& word) {
std::string vowels("aeiou");
std::string newWord(word);
if (newWord.find_first_not_of(vowels) == 0) {
// Word starts with a consanant
std::rotate(newWord.begin(), newWord.begin() + 1, newWord.end());
newWord += "ay";
} else {
newWord += "way";
}
return newWord;
}
int main() {
std::cout << make_pig_latin("apple") << '\n'
<< make_pig_latin("box") << '\n'
<< make_pig_latin("king") << '\n'
<< make_pig_latin("easy") << '\n';
}
The function above highlights how you can go about structuring your conversion. You just need to know if your word starts with a vowel or not, and take the appropriate action.
Output:
appleway
oxbay
ingkay
easyway
I did not get the impression that you have to care about words like 'phone'.
Looking through your code, you should try to do a better job at separating your concerns. Pig Latin is easier done one word at a time, but you have string splitting code and a lot of "not Pig Latin" code in your Pig Latin function. Your main can handle getting input. You should probably have a separate function to break the line up into individual words, using std::vector to hold the words would be best since it can grow on demand and doesn't have to know a specific capacity up front. You then iterate through your array of words and translate them individually. Depending on what your actual requirements are, it's possible that you don't even have to store the translated words, just print them directly to the screen.
Here's the same program, but now it can separate words. Note how the pig latin function doesn't have to change (much, I added upper-case vowels just because I didn't want to bothered converting words) in order for the added functionality to be added.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
std::string make_pig_latin(const std::string& word) {
std::string vowels("aeiouAEIOU");
std::string newWord(word);
if (newWord.find_first_not_of(vowels) == 0) {
// Word starts with a consanant
std::rotate(newWord.begin(), newWord.begin() + 1, newWord.end());
newWord += "ay";
} else {
newWord += "way";
}
return newWord;
}
int main() {
std::string phrase(
"A sentence where I say words like apple box easy king and ignore "
"punctuation");
std::istringstream sin(phrase);
std::vector<std::string> words(std::istream_iterator<std::string>(sin), {});
for (auto i : words) {
std::cout << make_pig_latin(i) << ' ';
}
}
Output:
Away entencesay hereway Iway aysay ordsway ikelay appleway oxbay easyway ingkay andway ignoreway unctuationpay
My job is write a program what converts a sentence into lower and upper case.
#include <iostream>
using namespace std;
int main ()
{
int current;
string sent;
cout << "Please enter a sentence: ";
cin >> sent;
for (int i = 0; i < sent.length(); i++)
{
current = (int) sent[i];
cout << "Your sentence in all lower case: "<< sent;
cout << endl;
if (current >= 65 && current <= 90)
{
current += 32;
sent[i] = (char) current;
cout << "Your sentence in all upper case: " << sent;
}
}
return 0;
}
The output should be:
Please enter a sentence: I Eat Apples!
Sentence in all lower case: i eat apples!
Sentence in all upper case: I EAT APPLES!
However I keep getting a lowercase "i" for the lower case and the upper case I get an upper case I, how come my code doesn't print out the full sentence? I don't get what I did wrong or where the wrong doing is.
The input operator >> separates on white-space (space, newline, tab, etc.). If you want to read a whole line use std::getline:
cout << "Please enter a sentence: ";
getline(cin, sent);
On an unrelated note, please don't use magic numbers like 65 or 32. If you mean characters then use the actual character literals like 'A' or 'a' - 'A' (please note that e.g. 'a' - 'A' is not valid in all encodings, it works in ASCII which is the most common encoding, but it's really not portable). This is also assuming this is a school assignment, otherwise you should be using e.g. std::toupper and some suitable standard algorithm function.
You should have two loops. One loop will be used to convert all letters of the sentence to the lower case and the other loop will be used to convert all letters of the sentence to the upper case.
Also to enter a sentence you should use standard function std::getline. And you need to include header <string>.
For example
#include <iostream>
#include <string>
//^^^^^^^^^^^^^^^
using namespace std;
int main()
{
string sentence;
cout << "Please enter a sentence: ";
getline( cin, sentence );
//^^^^^^^^^^^^^^^^^^^^^^
cout << "Your sentence in all lower case: ";
for ( string::size_type i = 0; i < sentence.length(); i++ )
{
char c = sentence[i];
if ( c >= 'A' && c <= 'Z' )
{
c |= ' ';
}
cout << c;
}
cout << endl;
cout << "Your sentence in all upper case: ";
for ( string::size_type i = 0; i < sentence.length(); i++ )
{
char c = sentence[i];
if ( c >= 'a' && c <= 'z' )
{
c &= ~' ';
}
cout << c;
}
cout << endl;
return 0;
}
The program output might look like
Please enter a sentence: I Eat Apples!
Your sentence in all lower case: i eat apples!
Your sentence in all upper case: I EAT APPLES!
Pay attention to using the blank character ' ' instead of magic number 32 to convert letters to lower or upper case. This allows to apply the approach also to EBCDIC characters.
You could substitute the loops for range based for loops. For example
for ( char c : sentence )
{
if ( c >= 'A' && c <= 'Z' ) c |= ' ';
cout << c;
}
cout << endl;
your code is quite strange to convert to upper/lower case, there are algorithms int STL to do that easily in C++ . Check out --> How to convert std::string to lower case?
And also , as others mentioned, use getline to get your line from an input stream
Hope this help =)
The extraction operator >> considers spaces (whitespace, tabs, newline etc.) as terminating the value being extracted.
Therefore writing:
std::string str;
std::cin >> str;
If we input This is a string only This will be extracted.
To get a whole line, you could use the function std::getline by including <string> that takes an input stream as first argument, a string as second argument and optionally a delimiter as third argument:
std::string sentence;
std::cout << "Please enter a sentence: ";
std::getline(std::cin, sentence);
To convert all the characters to uppercase or lowercase, there exists a library that you can use <cctype>, it has std::toupper and std::tolower among others.
This can easily be accomplished by using a loop:
std::string sentence_upper(sentence);
for (unsigned int i = 0; i != sentence_upper.length(); ++i) {
sentence_upper[i] = std::toupper(sentence_upper[i]);
}
Or if you have a C++11 compliant compiler you can use range-based for loop:
std::string sentence_upper(sentence);
for (auto &ch : sentence_upper)
ch = std::toupper(ch);
Putting it all together:
int main()
{
// Get input from user
std::string sentence;
std::cout << "Please enter a sentence: ";
std::getline(std::cin, sentence);
// Create new string, convert to upper
std::string sentence_upper(sentence);
for (unsigned int i = 0; i != sentence_upper.length(); ++i) {
sentence_upper[i] = std::toupper(sentence_upper[i]);
}
// Output converted string
std::cout << "Your sentence in upper case:\n";
std::cout << sentence_upper << '\n';
}
I'm trying to write three functions for an assignment
changeUp changes a string to uppercase
changeDown changes a string to lowercase
changeRev reverses the case of each character
Each function must accept a pointer to a string or C-string as an argument and must make the changes above by iterating over each character in the input string.
When entering AbCd, the first function should return ABCD; the second function should return abcd; and the reverse function should return aBcD.
I cannot figure out how to get the last function to work - it just returns all uppercase letters.
I have tried copying the input to a variable to save it - you can see my commented out areas where I tried to do this, but I really don't know what I'm doing, I'll just be honest. I got all kinds of errors when trying to copy (line, line2).
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void changeUp(char *line)//Change to upper case
{
for (int i = 0; line[i]; i++)
{
if (islower(line[i])) line[i] = toupper(line[i]);
}
}
void changeDown(char *line)//Change to lower case
{
for (int i = 0; line[i]; i++)
{
if (isupper(line[i])) line[i] = tolower(line[i]);
}
}
void changeRev(char *line)//Reverse the cases
{
for (int i = 0; line[i]; i++)
{
if (isupper(line[i])) line[i] = tolower(line[i]);
else if (islower(line[i])) line[i] = toupper(line[i]);
}
}
void main()
{
const int SIZE = 81; //For character space
char line[SIZE], line2[SIZE];//Character array
cout << "Enter a string of characters :" <<endl;//Takestring input from user
cin.getline(line, SIZE);
//strcpy_s(line, line2);
changeUp(line);
cout << "Changed to Upper Case " << line << endl;//Output string in all caps
//strcpy_s(line, line2);
changeDown(line);
cout << "Changed to Lower Case " << line << endl;//Output string in lower case
//strcpy_s(line, line2);
changeRev(line);
cout << "The rverse of the characters is " << line << endl;//Output reverse of string entered
system("pause");
return;
}
it is bad practice to include: "using namespace std", it is better to use std::cout and that goes for all functions from that namespace. Also you were sending a pointer to function so each time each function was modifying the string, therefore before your reverse condition you were passing all lowercase therefore that is why you were getting all caps. I think something like the solution bellow would be better approach.
#include <iostream>
#include <string>
using namespace std;
string changeUp(string line)//Change to upper case
{
for (int i = 0; line[i]; i++)
{
if (islower(line[i])) line[i] = toupper(line[i]);
}
return line;
}
string changeDown(string line)//Change to lower case
{
for (int i = 0; line[i]; i++)
{
if (isupper(line[i])) line[i] = tolower(line[i]);
}
return line;
}
string changeRev(string line)//Reverse the cases
{
for (int i = 0; line[i]; i++)
{
if (isupper(line[i])) line[i] = tolower(line[i]);
else if (islower(line[i])) line[i] = toupper(line[i]);
}
return line;
}
void main()
{
const int SIZE = 81; //For character space
char line[SIZE], line2[SIZE];//Character array
cout << "Enter a string of characters :" << endl;//Takestring input from user
cin.getline(line, SIZE);
//strcpy_s(line, line2);
cout << "Changed to Upper Case " << changeUp(line) << endl;//Output string in all caps
//strcpy_s(line, line2);
cout << "Changed to Lower Case " << changeDown(line) << endl;//Output string in lower case
//strcpy_s(line, line2);
cout << "The rverse of the characters is " << changeRev(line) << endl;//Output reverse of string entered
system("pause");
return;
}
You're almost there. The problem is that you are operating on the same variable and are overwriting the input line each time you run your functions on it. When you run changeRev you are "reversing" the result of the input string after you have just modified it to lowercase using changeDown. Reversing this string naturally makes it all uppercase.
Creating a copy is the right thing to do, but you have have just gotten the order of the arguments wrong. The destination is first, the source second. Then make sure to only operate on the copy, line2 so that the user's original input remains unmodified in the line variable so that you can use it again later.
strcpy_s(line2, line);
changeUp(line2);
cout << "Changed to Upper Case " << line2 << endl;/
strcpy_s(line2, line);
changeDown(line2);
cout << "Changed to Lower Case " << line2 << endl;
strcpy_s(line2, line);
changeRev(line2);
cout << "The reverse of the characters is " << line2 << endl;
The reason is that the functions are modifying the string in-place. Hence the string goes from AbCd => ABCD => abcd => ABCD. You lose the original string in the first call.
The solution is to make a copy of the string every time and call the function on the copy.
#include <iostream>
#include <string.h>
#include <string>
#include <fstream>
using namespace std;
int get_ascii_int(char ch);
int get_offset_ascii(char ch2, int offset);
//int print_string_ints(string test_string);
int method3_substitution_abc();
//above are the function declarations
int main()
{
string test_string;//input of string
char ch = 0;//used in method1
char ch2 = 0;//used in method2
int index1 = 0;//used in for loop method1
int index2 = 0;//used in for loop method2
int offset = 0;//input of how much to offset
int new_ascii = 0;//the new ascii with offset
int ascii_value1 = 0;//the ascii value of the char
int option;//the menu choice of encryption method
int decision;//the decision to save or display
ofstream method1;//method 1 text file
ofstream method2;//method 2 text file
ofstream method3;//method 3 text file
string test_string_copy;//copy of string method 2
//Below is a description of the methods of encryption
cout << "There are three methods of encryption, listed below, to choose from: " << endl;
cout << "1. Converting characters into the corresponding ASCII values. " << endl;
cout << "2. Shifting characters right/left using the ASCII value of the characters ";
cout << "and a set offset amount. " << endl;
cout << "3. Using a reverse alphabet, so each letter will be replaced with the letter ";
cout << "on the opposite end of the alphabet. For example, A would become Z. " << endl;
cout << "Which encryption method would you like to use, 1, 2, 3? ";
cin >> option;
switch (option)
{
case '1':
method1.open("method1.txt");
cout << "Input a word or name: ";
getline(cin, test_string);
for (; index1 < test_string.size(); index1++);
{
ascii_value1 = get_ascii_int(test_string[index1]);
}
cout << "Would you like to display the file or save it, enter 1 for display or 2 for save?";
cin >> decision;
if (decision == '1')
{
cout << "The encrypted code is " << ascii_value1 << endl;
}
else
{
if (method1.is_open())
{
method1 << "The encrpyted code is " << ascii_value1 << endl;
method1.close();
}
else
cout << "Unable to open file." << endl;
}
break;
case '2':
method2.open("method2.txt");
cout << "Input a word or name: ";
getline(cin, test_string);
test_string_copy = test_string;
for (; index2 < test_string_copy.size(); index2++);
{
new_ascii = get_offset_ascii(test_string_copy[index2], ch2);
}
cout << "Would you like to display the file or save it, enter 1 for display or 2 for save?";
cin >> decision;
if (decision == '1')
{
cout << "The encrypted code is " << new_ascii << endl;
}
else
{
if (method2.is_open())
{
method2 << "The encrypted code is " << new_ascii << endl;
method2.close();
}
else
cout << "Unable to open file." << endl;
}
break;
case '3':
method3.open("method3.txt");
method3_substitution_abc();
break;
}
return 0;
}
//listed below are the function definitions
int get_ascii_int(char ch)
{
return ((int)ch);
}
int get_offset_ascii(char ch2, int offset)
{
int new_offset_value;//the value after adding the determined offset to the ascii value of the letter
new_offset_value = (int)ch2 + offset;
(char)new_offset_value;
return (new_offset_value);
}
//int print_string_ints(string test_string)
//{
//for (int i = 0; i < test_string.size(); i++)
//{
//(int)test_string[i++];
//}
//return 0;
//}
int method3_substitution_abc()
{
char test_string[100];
cout << "Enter a name or phrase: ";
cin >> test_string;
if (isupper((int)test_string))
{
int stalpha = 65;//start of alphabet
int endalpha = 90;//end of alphabet
char b[100];//array to reverse the alphabet
for (int i = 0; test_string[i] != '\0'; i++)
{
b[i] = endalpha - (test_string[i] - 65);
}
}
else if (islower((int)test_string))
int stalpha = 97;//start of alphabet
int endalpha = 122;//end of alphabet
char b[100];//array to reverse the alphabet
for (int i = 0; test_string[i] != '\0'; i++)
{
b[i] = endalpha - (test_string[i] - 97);
}
return 0;
}
I am trying to write this encryption program. And I am just getting really confused on why it won't run.
For example the switch statement is not running correctly, it will go to the correct case and then skip the input of the string?
This is my first experience with C++ so I struggle to debug.
I am having issues with saving the file to a text file after the user chooses to save or display? It has to be done after every case in the switch statement.
I also know the for loops I am using are not correct for method 1 and 2? Could someone check those out and tell me what the issue is. I am pretty sure it has to do with the parameters for the for loop.
And I don't know if I should use a string or an array for this? (In the part where the user inputs a string
At least the first problem you've identified (with the switch statement) is pretty simple and clear.
You've defined option as an int, so when you read it it's read as an integer. That means if the user enters 1, it'll have the value 1, which is different from the value '1'. '1' is a character that will (for example) print as 1, but its value is actually 49 in most character sets.
You have two obvious choices: either change option to be a char, so it'll be read as a character instead of an integer, or else change the values in the switch statement from '1', '2', etc., to just 1, 2, etc.
At a guess, the problem you're seeing from getline is a fairly common one when you mix a string extractor (e.g., cin >> my_string;) with std::getline. The string extractor extracts a string from the stream, but leaves the new-line character in the stream buffer. Then when you call std::getline, it reads that new-line as an empty string, so it doesn't wait for you to enter more input.
If you really have to mix the two this way, you probably want to add a call to std::cin.ignore to read and ignore any data up to and including the new-line character. Then when you call std::getline, it'll actually read some data.
My assignment says:
---Rewrite the program by grouping the calculations into functions. In particular, you should introduce at least the following functions:
---A function named toLowerCase that takes a single character as an input parameter and returns a character. The returned value should be identical to the input unless the input is an upper-case letter, in which case the returned value should be the lower-case equivalent to that letter.
---Another function named toLowerCase, this one taking a string as an input parameter and returning a string. The returned string should be identical to the input except that all upper-case letters have been converted to lowercase.
----A function named readText that takes a string as an output parameter (no return value) and that reads multiple lines of input from cin until either hitting end-of-input or encountering an empty line. (Note: readText should not write anything to cout.)
----A function named countCharacter that takes two parameters as input and return an integer. The first input parameter will be a string and the second a character. The returned value should be the number of time that character occurs in the string (zero if the character occurs nowhere in the string). This function should work as described for all legal characters (i.e., even though this program will only use it to count lower-case letters, it should work for lower-case letters, upper-case letters, punctuation, etc.)
As you introduce each function, replace the code in main() by calls to your new functions as appropriate.
I keep getting error: expected primary-expression before char
#include<iomanip>
#include<iostream>
#include<string>
using namespace std;
void readText(string& text);
void toLowercase(string& text);
void countcharacter(string& text, char []);
char ToLowerCase();
int main (int argc, char** argv)
{
string userinput; //Initialize string name userinput
cout << "Enter text to be analyzed, ending with an empty line or end-of-input:" << endl;
readText(userinput); //Read text from user
cout << "You've entered: \n" << userinput << '\n'; // Read input, line by line, until end of input or an empty line
toLowercase(userinput); //Output user input and if upper-case, convert to lower-case
cout << "Lower case version of what you said: \n" << userinput << '\n';
countcharacter(userinput); //Count characters in userinput
ToLowerCase();
return 0;
}
void readText(string& text)
{
string line;
getline (cin, line);
while (cin && line != "")
{
text = text + line + "\n";
getline (cin, line);
}
/*for(std::string line; getline(std::cin, line) && !'\n' ; )
input += line + '\n'; */
}
void toLowercase(string& text)
{
const int ucLcOffset = 'a' - 'A';
for (int i = 0; i < text.size(); ++i)
{
char c = text[i];
if (c >= 'A' && c <= 'Z')
text[i] = text[i] + ucLcOffset;
}
}
void countcharacter(string& text, char c)
{
// Count and report on each alphabetic character
int totalCount = 0;
for (c = 'a'; c <= 'z'; ++c)
{
// Count how many times c occurs in the text
int charCount = 0;
for (int i = 0; i < text.size(); ++i)
{
if (text[i] == c)
++charCount;
}
// report on character c
cout << c << ":" << charCount << " " << flush;
if ((c - 'a') % 10 == 9)
cout << "\n";
totalCount = totalCount + charCount;
}
// How many characters are left over?
cout << "\nother:" << text.size() - totalCount << endl;
}
char ToLowerCase()
{
char c = 'A';
char Conversion;
const int ucLcOffset = 'a' - 'A';
if (c >= 'A' && c <= 'Z')
Conversion = c + ucLcOffset;
cout << "Your conversion for character A is: " << Conversion << '\n';
}
Line 28 error. 'countcharacter(userinput); //Count characters in userinput'
The line:
#inlcude<iostream>
Should be:
#include <iostream>
Syntax highlighting is your friend.
Also... countcharacter() takes two arguments:
void countcharacter(string& text, char []);
But you only provide one:
countcharacter(userinput);
#inlcude<iostream>
You spelt include wrong.
countcharacter(userinput); //Count characters in userinput
countcharacter() takes two parameters, not one.
It seems you want to count the occurrences of each lower case character ('a'-'z'). In this case you don't need to pass a second argument to countcharacter(). Change:
void countcharacter(string& text, char []);
to
void countcharacter(string& text);
and
void countcharacter(string& text, char []);
to
void countcharacter(string& text);
You will also have to declare char c in countcharacter().
It also seems you should change char ToLowerCase() to void ToLowerCase() as you don't seem to be returning anything.
I'm not a C++ guru, but the line
void countcharacter(string& text, char []);
Appears to be missing a parameter name.
Post the exact code you fed to your compiler. You wouldn't get "error: expected primary-expression before char" for what you posted, you'd get "error: invalid preprocessing directive #inlcude".
Copy-and-paste it, don't re-type it.
EDIT: fixed errror message