0

Ok, this is my code:

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

char arr[] = {'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'};
int y = 0;
string x;
const char* z;

int main()
 {
cin >> x;

z = x.c_str();

if (x.length() == 1)
{
for (y == 0; y <= 20; y++)
    {
    if (*z == arr[y])
        {
        cout << "Consonant" << endl;
        break;
        }
    else
        {
        cout << "Not a consonant" << endl;
        break;
        }
    }
}
}

So this should tell me if the character I entered is a consonant or not, but the problem is it tells me it's a consonant only when I enter "b", then if I enter every other element from that array it will tell me "Not a consonant".

I don't know how to fix this, I think the problem is that if (*z == arr[y]), for example "c" is the second element in the array, since y = 0 the program will check if it's "b", and then y = 1 and should check for "c", but the program checks for "b" and then goes in that "else" and tells me that it's not a consonant, then the program ends.

I don't have an idea on how to make this work.

petxd
  • 27
  • 7
  • `for (y == 0; y <= 20; y++)`? The `y == 0` seems like a typo? – Some programmer dude Sep 16 '17 at 12:29
  • I also recommend you read [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) by Eric Lippert, and learn how to use a debugger to step through the program line by line. Because if you do the problem should become very obvious. – Some programmer dude Sep 16 '17 at 12:31
  • I don't really understand what are you talking about, if you're talking about that "int y = 0;", I'm getting the same problem if it's just "int y;". – petxd Sep 16 '17 at 12:32
  • The loop initialization is irrelevant to your problem, but it *is* something you should fix. If `y` was a local variable (as it *should* be) then that would not work and would lead to *undefined behavior*. The problem with your code, the problem you're asking about, you need to use a debugger to find it out. – Some programmer dude Sep 16 '17 at 12:34
  • I can run the program, the problem is that it doesn't work how I wanted to. – petxd Sep 16 '17 at 12:35
  • Hint: check what the `break;` statement does in your loop. – sinclair Sep 16 '17 at 12:43
  • @petxd Step through your code line by line with the debugger. Then you should get a grasp what's actually going on. – user0042 Sep 16 '17 at 12:45
  • You don't understand... I know how is my code working, BUT I don't know how to make it like I want.. – petxd Sep 16 '17 at 12:56
  • If I remove the break, for example the code will check for the every character in the array the character I entered. – petxd Sep 16 '17 at 12:57
  • I removed the "break;", and entered x = g, this is what I get: – petxd Sep 16 '17 at 12:58
  • Not a consonant Not a consonant Not a consonant Consonant Not a consonant x 17 – petxd Sep 16 '17 at 12:58
  • You get to the `else` too early. You have to check **all** the values in the array before you can know if it perhaps matches the last one. – Bo Persson Sep 16 '17 at 13:07
  • And how can I make the program go to "else" after it checks all the values ? – petxd Sep 16 '17 at 13:09
  • I would create a function `is_consonant` that returns `true` when you get a match and returns `false` if you complete the for-loop without a match. – Bo Persson Sep 16 '17 at 13:11
  • Ok, thanks for the idea! – petxd Sep 16 '17 at 13:12
  • A simple solution: A boolean flag. Initialize to false (for no consonant found). If a consonant is found then set the flag to true, and break out of the loop. Once the loop is finished, check the flag, if true then a consonant was found, otherwise it was not. – Some programmer dude Sep 16 '17 at 13:52

1 Answers1

0

There are several problems with your program, technically and logically.

Technical problems

If you are using C++, avoid using C functionality that has been replaced by C++ functionality, i.e., dont't use C strings when you can use C++ std::string.

If you have the urge to use a C array do it but a std::vector would be the better choice.

The next problem is you for-loop. Learn the difference between = (assignment) and == (equality). y==0 is a expression that return true is y is equal to 0, false otherwise. y=0 assignes y to 0. In your for-loop you want to do the latter.

Logical problems

Ask yourself what you want to achieve? Here is what you are trying to do:

  1. Get a character as user input.
  2. Check if this character is a consonant.
  3. Output a corresponding message.

Your code is poorly designed from point 1 on: Why do you let the user input a string if you are only interessted in characters (since you are testing if the string is of length 1 and want to compare it to an array of characters)?

And here comes your biggest logical problem: In every loop step you test if the input is equal to a character. If it's not, you output "Not a consonant" and exit the loop. Let's investigate for the input of h:

  • First loop execution: Is h equal to b? -> No -> "No consonant" and leave the loop

Consider this approach that avoides the use of C functions and solves your problem:

Edit: Since you had problems compiling my solution, I added a version not using C++11 features:

C++11

#include <iostream> /* std::cout, std::cin */
#include <vector>   /* std::vector */

int main()
{
    std::vector<char> consonants {'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 
                                  'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 
                                  'x', 'y', 'z'};
    char c;

    std::cin >> c;

    for(const auto& letter: consonants){
        if(c == letter){
            std::cout << "Consonant" << std::endl;
            return 0;
        }
    }

    std::cout << "Not a consonant" << std::endl;
}

Compile with the -std=c++11 flag.

Classical solution

#include <iostream> /* std::cout, std::cin */
#include <vector>   /* std::vector */

int main()
{
    const char arr[] = {'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 
                        'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 
                        'x', 'y', 'z'};

    std::vector<char> consonants(arr, arr+sizeof(arr)/sizeof(arr[0]));

    char c;

    std::cin >> c;

    for(int i=0;i<consonants.size();i++){
        if(c == consonants[i]){
            std::cout << "Consonant" << std::endl;
            return 0;
        }
    }

    std::cout << "Not a consonant" << std::endl;
}
dtell
  • 2,488
  • 1
  • 14
  • 29
  • "Could not convert ' {'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'} ' from < brace-enclosed initilalizer list > to 'vector '" - error – petxd Sep 16 '17 at 13:30
  • Compile with `g++ -std=c++11 yoursource.cpp` or see [What is the easiest way to initialize a std::vector with hardcoded elements?](https://stackoverflow.com/questions/2236197/what-is-the-easiest-way-to-initialize-a-stdvector-with-hardcoded-elements) for further information. – dtell Sep 16 '17 at 13:35