Hi,
First up, it doesn't compile:
In function 'int main()':
23:28: error: 'strlen' was not declared in this scope
67:28: error: 'strcpy_s' was not declared in this scope
In function 'int CountConst(char*)':
128:36: error: 'strlen' was not declared in this scope
128:17: warning: unused variable 'end' [-Wunused-variable] |
So you need to #include the files for those.
Is this an assignment? If so, what were the specific requirements ? Are you allowed to use
std::string
- you have it included. If you can there are lots of handy things in that container. You could avoid C-style coding then.
Just on that, don't
#include <string>
then use
string
as a variable name.
Are you allowed to use the STL? There heaps of really good algorithms in there that would radically change your code.
To be honest, you have some other terrible variable names:
numofconst
and
CountConst
- apparently that means : number of
consonants not number of
constants
And here:
1 2 3 4 5 6 7
|
for (; string[start] != '\0'; start++)
{
if (tolower(string[start]) == 'b'
|| tolower(string[start]) == 'd'
|| tolower(string[start]) == 'c'
|| tolower(string[start]) == 'd'
|| tolower(string[start]) == 'f'
|
start
is a bad name in this context, you increment it, so it is no longer the start. Perhaps
Position
might be better?
IMO, these confusing names come from the misguided idea that one needs to abbreviate variable names everywhere.
A for loop is not good on line 129, for loops are best when one knows exactly how many times to loop. Consider using a while loop there.
With lines 131 to 152, consider negating the test for a vowel, rather than testing for all the consonants. It's easier to test for it not being one of 5 things, rather than it is being one of 21 things. One can negate a condition like this
if !(condition) {}
This:
numofconst += 1;
is better written, with the variable name change and the increment operator:
++NumOfConsonants;
With your switch , you show an option 9 on the menu, but there is no
case 9:
!! I know you have the while condition on line 26, but IMO it is better to do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
bool Quit = false;
while (!Quit) {
// show menu
// get menu choice
switch (choice) {
// the options as you have them
// ...
// ...
case 9:
Quit = true;
break;
default: // catch bad input
std::cout << "Bad Input Try Again\n";
break;
}
}
|
Each of you switch cases should call a function to do their work, this makes the code much easier to read.
This:
1 2
|
count = 0;
for (; count != length; count++)
|
should be:
for (int count = 0; count < length; count++)
Important to understand the difference there.
Some style points:
Your code will display a lot better on this forum if you configure your editor to convert tabs to 4 spaces say. This site converts tabs to 8 spaces, so that results in much more unwanted indenting.
Consider putting comments
before statements, not at the end of them. And split long comments into separate lines. Thus avoid having code which is 2 screens wide when posted here. There is a kind of a guide that code should not be more than 80 chars per line. Although it is not something to be set in stone, it does have some advantages, most notably where there is no word wrap. Note the compiler doesn't care if you press the enter key in a statement, it is looking for semi-colons to end statements. I make use of that all the time, especially for functions with multiple parameters: I put each parameter on it's own line.
Good Luck !!
Some of the comments are worthless:
1 2
|
check = false; //set check to false
cout << "the jumbled string is: " << string2 << endl; //print the jumbled version to the screen
|