#include <iostream>
#include <cstring>
#include <string>
#include <istream>
using namespace std;
//Function prototypes
int numVowels(char *str);
int numConsonants(char *str);
int main()
{
char string[100];
char inputChoice, choice[2];
int vowelTotal, consonantTotal;
//Input a string
cout << "Enter a string: " << endl;
cin.getline(string, 100);
do
{
//Displays the Menu
cout << " (A)
Count the number of vowels in the string"<<endl;
cout << " (B)
Count the number of Consonants in the string"<<endl;
cout << " (C)
Count both the vowels and consonants in the
string"<<endl;
cout << " (D)
Enter another string" << endl;
cout << " (E)
Exit program" << endl;
//Inputting choice
cout << "Enter choice:
";
cin.getline(choice, 2);
inputChoice = choice[0]; //a single
char variable will now equal the choice user pic from
c-string
switch (toupper(inputChoice)) //a
switch case, any user input char that is lower case will be now
uppercase
{
case
'A'://Function call to get number of Vowels
vowelTotal = numVowels(string);
cout << "Number of vowels is: " <<
vowelTotal << endl;
break;
case 'B':
//Function call to get number of consonants
consonantTotal = numConsonants(string);
//Outputting number of Consonants
cout << "Number of Consonants is: "
<< consonantTotal << endl;
break;
case 'C':
//Function call to get number of Vowels
vowelTotal = numVowels(string);
//Function call to get number of
consonants
consonantTotal = numConsonants(string);
//Outputting Both Vowels and Consonants
cout << "Number of vowels is: " <<
vowelTotal << endl;
cout << "Number of Consonants is:"
<< consonantTotal << endl;
break;
case 'D':
//Inputting another string
//Input a string
cout << "Enter a string: " <<
endl;
cin.getline(string, 100);
break;
case 'E':
exit(0);
break;
}//End of Switch
} while (inputChoice != 'E');//End Do while
}//End main
//Function Definitions
int numVowels(char *str)
{
int count = 0;//Local variable accumulates the total
number of vowels
while (*str != '\0')
{
if (*str == 'a' || *str == 'e' ||
*str == 'i' || *str == 'o' || *str == 'u')
count++;
str++;
}//End While
//Returns count value to function call
return count;
}
int numConsonants(char *str)
{
int count = 0;
//Checks for all characters in C-string
while (*str != '\0')
{ //Checks for consonants
if (*str != 'a' && *str !=
'e' && *str != 'i' && *str != 'o' && *str
!= 'u')
count++;
str++;
}//End While
//Returns count value to function call
return count;
}
This is the code to my homework probably. the Program works but the problem is that if i enter more than 100 chars my program will crash because of my array setting. how would i set it to where i can input any amount of chars and wouldn't worry about the set limit. is there away to dynamically allocate memory for cstrings? Thank you.
Instead of dynamically allocating, We can increase the size of the character array. Your program is correct. We need change the size of the character array in the program. In the following screen shots you can see. I changed the size of the character array to 1000. It means it can store upto 1000 characters. If we want more size we can increase the size of the character array. In dynamic allocate we need to specify the memory we need to use malloc,calloc for allocating space. And we need to use free function for realeasing of memory. Instead of doing all this work we can increase the size of character array.
INPUT:
In the first input i entered 104 characters. It is executed. And in the second output i entered more than 104 characters. Just we need to increase the size of character array.That's it.
#include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...
#include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() { char string[100]; char inputChoice, choice[2]; int vowelTotal, consonantTotal; //Input a string cout << "Enter a string: " << endl; cin.getline(string, 100); do { //Displays the Menu cout << " (A) Count the number of vowels in the string"<<endl; cout << " (B) Count...
#include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int num1, num2, result; while(ch == 'Y'){ cout << "Enter first number: "; cin >> num1; while(1){//for handling invalid inputs if(cin.fail()){ cin.clear();//reseting the buffer cin.ignore(numeric_limits<streamsize>::max(),'\n');//empty the buffer cout<<"You have entered wrong input"<<endl; cout << "Enter first number: "; cin >> num1; } if(!cin.fail()) break; } cout << "Enter second number: "; cin >> num2; while(1){ if(cin.fail()){ cin.clear(); cin.ignore(numeric_limits<streamsize>::max(),'\n'); cout<<"You have entered wrong input"<<endl; cout <<...
CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...
#include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...
I need to update this code: #include <iostream> #include <string> #include <cctype> using namespace std; int main() { string s; cout<< "Enter a string" <<endl; getline (cin,s); cout<< s <<endl; int vowels=0,consonants=0,digits=0,specialChar=0; for (int i=0; i<s.length(); i++) { char ch=s[i]; if (isalpha(s[i])!= 0){ s[i]= toupper(s[i]); if (ch == 'a'|| ch == 'e'|| ch == 'i'|| ch == 'o' || ch == 'u') vowels++; else consonants++; } else if (isdigit(s[i])!= 0) digits++; else specialChar++; } cout<<"Vowels="<<vowels<<endl; cout<<"Consonants="<<consonants<<endl; cout<<"Digits="<<digits<<endl; cout<<"Special Characters="<<specialChar<<endl;...
#include <iostream> #include <sstream> #include <string> using namespace std; int main() { const int index = 5; int head = 0; string s[index]; int flag = 1; int choice; while (flag) { cout << "\n1. Add an Item in the Chores List."; cout << "\n2. How many Chores are in the list."; cout << "\n3. Show the list of Chores."; cout << "\n4. Delete an...
#include <iostream> #include <string> using namespace std; int main() { int number; int sum = 0; while(true) { cout << "Please enter a number between 1 and 11: "; cin >> number; if (number >= 1 && number <= 11) { cout << number << endl; sum = sum + number; //only add the sum when number is in range: 1-11, so add wthin this if case } else { cout << number << endl; cout << "Out of range;...
#include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car { private: string reportingMark; int carNumber; string kind; bool loaded; string choice; string destination; public: Car() { reportingMark = ""; carNumber = 0; kind = "Others"; loaded = 0; destination = "NONE"; } ~Car() { } void setUpCar(string &reportingMark, int &carNumber, string &kind, bool &loaded, string &destination); }; void input(string &reportingMark, int &carNumber, string &kind, bool &loaded,string choice, string &destination); void output(string &reportingMark, int &carNumber,...
***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName) { cout << "Usage: " << executableName << " [-c] [-s] string ... " << endl; cout << " -c: turn on case sensitivity" << endl; cout << " -s: turn off ignoring spaces" << endl; exit(1); //prints program usage message in case no strings were found at command line } string tolower(string str) { for(unsigned int i = 0; i < str.length(); i++)...
#include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure char start_state, to_state; char symbol_read; }; void read_DFA(struct transition *t, char *f, int &final_states, int &transitions){ int i, j, count = 0; ifstream dfa_file; string line; stringstream ss; dfa_file.open("dfa.txt"); getline(dfa_file, line); // reading final states for(i = 0; i < line.length(); i++){ if(line[i] >= '0' && line[i] <= '9') f[count++] = line[i]; } final_states = count; // total number of final states // reading...