I'm confused with pointers. I'm not sure how to get the vowels to show up in Case A.
Also, I would need help by how to get the program to run again after case A instead of ending the program after each case. Should it be in if/else statement instead of switch? Or use while loop with switch statement?
Thanks and I appreciate any hints/help! :)
#include <fstream>
#include <iostream>
using namespace std;
char* getStringFromUser();
int consonants(char* str); //function to print out consonants
int vowels(char* str); //function to print out vowels
void stringCopy(char* str1, char* str2);
int main ()
{
//declare variables to use for storing user's input
char choice; //first menu
char line;
//declare variables to use for program
bool goagain = true;
int getNumStrings();
fstream dataFile; //input stream for file
// introduction to the program
cout << "\n\n------------------------------------------------" << endl;
cout << "\n\nVowels & Consonants" << endl;
dataFile.open("stringFile.txt", ios::out | ios::app); //open file
if (! dataFile) // test for error
cout << "Error opening file.\n";
cout << "\n\nPlease select from the following menu items:\n";
cout << "\t A) Count the vowels of a string.\n";
cout << "\t B) Count the consonants of a string.\n";
cout << "\t C) Count both vowels and consonants of a string.\n";
cout << "\t D) Enter a string in the file.\n";
cout << "\t E) Count the number of strings in the file.\n";
cout << "\t F) Exit this program.\n\n";
cout << "\t Enter A, B, C, D, E, or F: ";
cin >> choice; //user choice of menu
switch(choice)
{
case 'A': cout << getStringFromUser();
// cout << vowels(line, vowels);
break;
case 'B': cout << "choice B";
break;
case 'C': cout << "choice C";
break;
case 'D': cout << "choice D";
break;
case 'E': cout << "choice E";
break;
case 'F': cout << "\n\nGoodbye!" << endl;
break;
default: cout << "That is an invalid choice.\n";
}
dataFile.close(); //close text file
return 0;
}//end of function
char* getStringFromUser()
{
char line[100];
cout << "Enter a string: ";
cin.ignore();
cin.getline(line, 100, '\n');
}
int vowels(char* str)
{
char vowelsArray[] = {'A', 'a', 'E', 'e', 'I', 'i', 'O', 'o', 'U', 'u', '\0'};
char *vowelsPtr = vowelsArray;
int numVowels = 0;
int count;
cout << "The vowels in the string are: ";
while(*str != '\0')
{
for(count = 0; count <10; count++)
{
if (*str == *vowelsPtr)
{
numVowels++; //Point to next element
cout << "The vowels in the string are: " << *vowelsPtr << endl;
break;
}
}
str++;
vowelsPtr = vowelsArray; //set vowelsPtr to first element again
}
cout << "\n\n";
return numVowels;
}