can someone please help me figure out whats wrong with my program code? #include
ID: 645521 • Letter: C
Question
can someone please help me figure out whats wrong with my program code?
#include <iostream>
#include <cstring>
#include <cctype>
#include <iomanip>
using namespace std;
int countVowels(char str[100]);
int countConso(char str[100]);
void toUpper(char *str);
void toLower(char str[100]);
int countVowels(char str[100])
{ int v=0;
for(int i=0;str[i]!='';i++)
{
if(str[i]=='a' || str[i]=='A' || str[i]=='e' || str[i]=='E' || str[i]=='i' || str[i]=='I' || str[i]=='o' || str[i]== 'O' || str[i]=='u' || str[i]=='U')
v++;
}
return v;
}
int countConso(char str[100])
{ int c=0;
for(int i=0;str[i]!='';i++)
{
if(str[i]!=' ' && str[i]!='a' && str[i]!='A' && str[i]!='e' && str[i]!='E' && str[i]!='i' && str[i]!='I' && str[i]!='o' && str[i]!= 'O' && str[i]!='u' && str[i]!='U')
c++;
}
return c;
}
void toUpper(char str[100])
{ char st[100];int j=0;
for(int i=0;str[i]!='';i++)
{if(str[i]>=97 && str[i]<=122)
st[j++]=str[i]-32;
else
st[j++]=str[i];
}
st[j]='';
strcpy(str,st);
void toLower(char str[100]);
{
char st[100];int j=0;
for(int i=0;str[i]!='';i++)
{if(str[i]>=65 && str[i]<=90)
st[j++]=str[i]+32;
else
st[j++]=str[i];
}
st[j]='';
strcpy(str,st);
void main();
{
char x;
char str[100];
int v,c;
cout<<"Enter your string";
gets(str);
do
{
L:
cout<<" A) Count the number of vowels in the String B) Count the number of consonants in the String C) Convert the string to uppercase D) Convert the string to lowercase E) Display the current String F) Enter another String M) Display this menu X) Exit the Program Enter your choice: ";
cin >> x;
switch(x)
{
case 'A':
case 'a':
v = countVowels(str);
cout<<"Number of Vowels in the String is :"<<endl;
break;
case 'B':
case 'b':
c = countConso(str);
cout<<"Number of Consonants in the String is :"<< endl;
break;
case 'C':
case 'c':
toUpper(str);
break;
case 'D':
case 'd':
toLower(str);
break;
case 'E':
case 'e':
break;
case 'F':
case 'f':
cout<<"Enter the new String";
gets(str);
break;
case 'M':
case 'm':
goto L;
break;
case 'X':
case 'x':
exit(0);
}
{
while(x != 'X' || x != 'x');
gets(str);
}
Explanation / Answer
Note:
The program works perfectly. Only things that are changed are:
After each function definition there is no proper closing is done, so the function definitions are properly closed.
At the output to display the convert upper, lower or present value you did not put the values at cout after calling each function respectively. That is the reason, at the output; the values are not printed out.
do..while when used, you did not close the braces properly. They were opened instead of closing.
Finally, did not close the main function. So, the closing brace is added.
The changed/added code is highlighted in bold letters. Please go through.
Program code:
// WokingWithVowelsConsonants.cpp : Defines the entry point for the console application.
#include "stdafx.h"//optional, is used depending on the editor
#include <iostream>
#include <cstring>
#include <cctype>
#include <iomanip>
using namespace std;
int countVowels(char str[100]);
int countConso(char str[100]);
void toUpper(char *str);
void toLower(char str[100]);
int countVowels(char str[100])
{
int v=0;
for(int i=0;str[i]!='';i++)
{
if(str[i]=='a' || str[i]=='A' || str[i]=='e' || str[i]=='E' || str[i]=='i' || str[i]=='I' || str[i]=='o' || str[i]== 'O' || str[i]=='u' || str[i]=='U')
v++;
}
return v;
}
int countConso(char str[100])
{
int c=0;
for(int i=0;str[i]!='';i++)
{
if(str[i]!=' ' && str[i]!='a' && str[i]!='A' && str[i]!='e' && str[i]!='E' && str[i]!='i' && str[i]!='I' && str[i]!='o' && str[i]!= 'O' && str[i]!='u' && str[i]!='U')
c++;
}
return c;
}
void toUpper(char str[100])
{
char st[100];int j=0;
for(int i=0;str[i]!='';i++)
{
if(str[i]>=97 && str[i]<=122)
st[j++]=str[i]-32;
else
st[j++]=str[i];
}
st[j]='';
strcpy(str,st);
}
void toLower(char str[100])
{
char st[100];int j=0;
for(int i=0;str[i]!='';i++)
{
if(str[i]>=65 && str[i]<=90)
st[j++]=str[i]+32;
else
st[j++]=str[i];
}
st[j]='';
strcpy(str,st);
}
int main()
{
char x;
char str[100];
int v,c;
cout<<"Enter your string >> ";
gets(str);
do
{
L:
cout<<" *****************Menu******************* ";
cout<<" A) Count the number of vowels in the String B) Count the number of consonants in the String C) Convert the string to uppercase D) Convert the string to lowercase E) Display the current String F) Enter another String M) Display this menu X) Exit the Program Enter your choice: ";
x=getchar();
switch(x)
{
case 'A':
case 'a':
v = countVowels(str);
cout<<"Number of Vowels in the String is: "<<v<<endl;
break;
case 'B':
case 'b':
c = countConso(str);
cout<<"Number of Consonants in the String is: "<<c<< endl;
break;
case 'C':
case 'c':
toUpper(str);
cout<<"After converting to upper case: "<<str<<endl;
break;
case 'D':
case 'd':
toLower(str);
cout<<"After converting to lower case: "<<str<<endl;
break;
case 'E':
case 'e':
cout<<str<<endl;
break;
case 'F':
case 'f':
cout<<"Enter the new String>> ";
gets(str);
break;
case 'M':
case 'm':
goto L;
break;
case 'X':
case 'x':
exit(0);
}
}while(x != 'X' || x != 'x');
gets(str);
system("pause");
return 0;
}
Sample Output:
Enter your string >> Go home
*****************Menu*******************
A) Count the number of vowels in the String
B) Count the number of consonants in the String
C) Convert the string to uppercase
D) Convert the string to lowercase
E) Display the current String
F) Enter another String
M) Display this menu
X) Exit the Program
Enter your choice: M
*****************Menu*******************
A) Count the number of vowels in the String
B) Count the number of consonants in the String
C) Convert the string to uppercase
D) Convert the string to lowercase
E) Display the current String
F) Enter another String
M) Display this menu
X) Exit the Program
Enter your choice: A
Number of Vowels in the String is: 3
*****************Menu*******************
A) Count the number of vowels in the String
B) Count the number of consonants in the String
C) Convert the string to uppercase
D) Convert the string to lowercase
E) Display the current String
F) Enter another String
M) Display this menu
X) Exit the Program
Enter your choice: B
Number of Consonants in the String is: 3
*****************Menu*******************
A) Count the number of vowels in the String
B) Count the number of consonants in the String
C) Convert the string to uppercase
D) Convert the string to lowercase
E) Display the current String
F) Enter another String
M) Display this menu
X) Exit the Program
Enter your choice: C
After converting to upper case: GO HOME
*****************Menu*******************
A) Count the number of vowels in the String
B) Count the number of consonants in the String
C) Convert the string to uppercase
D) Convert the string to lowercase
E) Display the current String
F) Enter another String
M) Display this menu
X) Exit the Program
Enter your choice: D
After converting to lower case: go home
*****************Menu*******************
A) Count the number of vowels in the String
B) Count the number of consonants in the String
C) Convert the string to uppercase
D) Convert the string to lowercase
E) Display the current String
F) Enter another String
M) Display this menu
X) Exit the Program
Enter your choice: E
go home
*****************Menu*******************
A) Count the number of vowels in the String
B) Count the number of consonants in the String
C) Convert the string to uppercase
D) Convert the string to lowercase
E) Display the current String
F) Enter another String
M) Display this menu
X) Exit the Program
Enter your choice: F
Enter the new String
*****************Menu*******************
A) Count the number of vowels in the String
B) Count the number of consonants in the String
C) Convert the string to uppercase
D) Convert the string to lowercase
E) Display the current String
F) Enter another String
M) Display this menu
X) Exit the Program
Enter your choice: F Lets have fun
Enter the new String >>
*****************Menu*******************
A) Count the number of vowels in the String
B) Count the number of consonants in the String
C) Convert the string to uppercase
D) Convert the string to lowercase
E) Display the current String
F) Enter another String
M) Display this menu
X) Exit the Program
Enter your choice: A
Number of Vowels in the String is: 4
*****************Menu*******************
A) Count the number of vowels in the String
B) Count the number of consonants in the String
C) Convert the string to uppercase
D) Convert the string to lowercase
E) Display the current String
F) Enter another String
M) Display this menu
X) Exit the Program
Enter your choice: B
Number of Consonants in the String is: 7
*****************Menu*******************
A) Count the number of vowels in the String
B) Count the number of consonants in the String
C) Convert the string to uppercase
D) Convert the string to lowercase
E) Display the current String
F) Enter another String
M) Display this menu
X) Exit the Program
Enter your choice: C
After converting to upper case: LETS HAVE FUN
*****************Menu*******************
A) Count the number of vowels in the String
B) Count the number of consonants in the String
C) Convert the string to uppercase
D) Convert the string to lowercase
E) Display the current String
F) Enter another String
M) Display this menu
X) Exit the Program
Enter your choice:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.