I have a program due. It worked when everything was in one function main(). I have to separate functions. When I changed the program to just being separate functions, it worked with the exception of not moving the input to the separate string and cstring functions, I think because the value isn't passing for one reason or another. It's probably something easy. Please help. I'm sure I'm just not passing values correctly but I tried what I thought would work and it didn't, I get build errors. Here is the code.
// declare applicable header files necessary
#include <iostream>
#include <string>
#include <cstring>
#include <istream>
using namespace std;
string inputString;
char userInput[50];
string getInput();
void stringManipulation();
char convertStringToCstring();
void cStringManipulation();
void main()
{
getInput();
stringManipulation();
convertStringToCstring();
cStringManipulation();
}
string getInput()
{
/* Declare string variable for user input to be computed. */
string inputString;
// Request user to enter the string
cout << "Enter a string: ";
// Obtain the user string
getline(cin, inputString);
// Output what the user has entered for their string
cout << endl
<< "The string you entered is: \"" << inputString << "\""
<< endl << endl;
return inputString;
}
void stringManipulation()
{
// Let know this portion is based on C String types
cout << "The following output demonstrates functions performed on "
<< endl << "C string type input." << endl << endl;
// Based on string type, output number of total characters in the string
cout << " This string has " << inputString.size() << " total characters."
<< endl << endl;
// Establish variables for computation based on string type
int totalStringChars = inputString.size();
int stringVowels = 0;
int stringSpaces = 0;
int stringConsonants = 0;
int stringOthers = 0;
// Use a for statement with switch to determine number of vowels in string
for (int i=0; i < totalStringChars; i )
{
if (inputString[i]=='A' || inputString[i]=='a' ||
inputString[i]=='E' || inputString[i]=='e' ||
inputString[i]=='I' || inputString[i]=='i' ||
inputString[i]=='O' || inputString[i]=='o' ||
inputString[i]=='U' || inputString[i]=='u' ||
inputString[i]=='Y' || inputString[i]=='y')
{
stringVowels ;
}
else if (inputString[i]==' ')
{
stringSpaces ;
}
else if (static_cast<int>((inputString[i]>=33 && inputString[i]<=64) ||
(inputString[i]>=91 && inputString[i]<=96) ||
(inputString[i]>=123 && inputString[i]<=126)))
{
stringOthers ;
}
else
{
stringConsonants ;
}
}
// Based on string type, output number of vowels in the string
cout << " The string contains " << stringVowels << " vowels." << endl;
// Based on string type, output number of spaces in the string
cout << " The string contains " << stringSpaces << " spaces." << endl;
// Based on string type, output number of consonants in the string
cout << " The string contains " << stringConsonants << " consonants." << endl;
// Based on string type, output number of other characters and numbers in the string
cout << " The string contains " << stringOthers << " other characters and numbers." << endl << endl;
}
char convertStringToCstring()
{
/************************************...
// Use c_str and strcpy_s to copy inputString variable to userInput array
char userInput[50];
strcpy_s(userInput, inputString.c_str());
return userInput[50];
/************************************...
}
void cStringManipulation()
{
// Let know this portion is based on C String types
cout << "The following output demonstrates functions performed on "
<< endl << "C C-string type input." << endl << endl;
// Based on C-string type, output number of total characters in the string
cout << " This string has " << strlen(userInput) << " total characters."
<< endl << endl;
// Establish variables for computation based on string type
int totalCstringChars = strlen(userInput);
int cStringVowels = 0;
int cStringSpaces = 0;
int cStringConsonants = 0;
int cStringOthers = 0;
// Use a for statement with switch to determine number of vowels in string
for (int i=0; i < totalCstringChars; i )
{
if (userInput[i]=='A' || userInput[i]=='a' ||
userInput[i]=='E' || userInput[i]=='e' ||
userInput[i]=='I' || userInput[i]=='i' ||
userInput[i]=='O' || userInput[i]=='o' ||
userInput[i]=='U' || userInput[i]=='u' ||
userInput[i]=='Y' || userInput[i]=='y')
{
cStringVowels ;
}
else if (userInput[i]==' ')
{
cStringSpaces ;
}
else if (static_cast<int>((userInput[i]>=33 && userInput[i]<=64) ||
(userInput[i]>=91 && userInput[i]<=96) ||
(userInput[i]>=123 && userInput[i]<=126)))
{
cStringOthers ;
}
else
{
cStringConsonants ;
}
}
// Based on C-string type, output number of vowels in the string
cout << " The string contains " << cStringVowels << " vowels." << endl;
// Based on C-string type, output number of spaces in the string
cout << " The string contains " << cStringSpaces << " spaces." << endl;
// Based on C-string type, out put number of consonants in the string
cout << " The string contains " << cStringConsonants << " consonants." << endl;
// Based on string type, output number of other characters and numbers in the string
cout << " The string contains " << cStringOthers << " other characters and numbers." << endl << endl;
}
Tags: