-4

This code now works for single words. I just need it to work for a sentence now. I do have the appropriate header files it just doesn't let me include them for some reason. I have to use scanf_s. I have already tried scanf_s( "%s", word, 100), and it didn't work. Thanks for all the help so far.

void printIsVowel() {
    int isVowel = 0;
    int i;
    char word[40] = "";
    printf("Enter a Statement");
    scanf_s("%s", word, 40);
    for (i = 0; i < strlen(word); i++) {
        if (tolower(word[i]) == 'a' || tolower(word[i]) == 'e' || tolower(word[i]) == 'i') {
            isVowel++;
        } else if (tolower(word[i]) == 'o' || tolower(word[i]) == 'u') {
            isVowel++;
        }
    }
    printf("The previous statement has %d vowels.\n", isVowel);
}
int main() {
    printIsVowel();
    system("pause");
    return 0;
}
  • Why not use C++ I/O and C++ strings? There are a lot of reasons why you don't want to start to learn programming with C-style strings. – eesiraed Mar 25 '18 at 17:53
  • [Your rubber duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging) is curious about the amount of storage allocated with `char word[] = "";` It also wants to know how you intend to detect errors with `scanf_s("%s", word);` that don't immediately lead to a program halt. – user4581301 Mar 25 '18 at 18:14

3 Answers3

1

Why don't you use scanf instead? I think that will solve the problem.

Also, when I run the code from an online editor, it says that both 'scanf_s' and 'strlen' were not declared in this scope. Notify me if you meant to include other files.

Thanks!

0
char word[] = "";

creates word with exactly enough space to hold an empty string: one character for the terminating null. This is useless for user input because the user cannot input anything.

Rather than fix this, I fall back on the wisdom of the ancients: When in Rome, do as the Romans do. When in C++ use a C++ solution.

#include <iostream>
#include <string>
void printIsVowel() {

    int isVowel = 0;
    // int i; not needed with smart for loop.
    std::string word; // prefer a std::string to a character array. 
                      // string resizes to fit the input and many other useful things
    std::cout << "Enter a Statement"; // use std::cin and std::cout
    if (std::cin>> word) // always test that reads succeeded before using input
    {
        for (char ch: word) { // smarter, range-based for loop
            ch = tolower(ch); // why repeat? Do it once and cache
                              // good on you for using tolower, by the way
            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
                isVowel++;
            }
            // no need for the else. Do it all in one shot
        }
    }
    std::cout << "The previous statement has " << isVowel <<" vowels.\n";
}

int main() {

    printIsVowel();
    system("pause"); //avoid this. std::cin >> junkvar; is lighter weight and portable
    return 0;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • Woops Sorry i Meant to put embedded systems with c++. So I have to use scanf_S – Luis Maldonado Mar 25 '18 at 18:30
  • How about this: `const std::string vowels = "aeiou"; if (vowels.find(word[ch]) != std::string::npos) ++IsVowel;`? – Thomas Matthews Mar 25 '18 at 18:33
  • @LuisMaldonado a C++ implementation without `iostream` and `string` is not C++. While I can see cases (many, many of them) for avoiding dynamic allocation on an embedded system, if you have to avoid strings and things, code in C. – user4581301 Mar 25 '18 at 18:34
  • @ThomasMatthews Considered using a couple of algorithm solutions, but for five characters I didn't see much point. What Luis had was already obvious and easier to type. – user4581301 Mar 25 '18 at 18:38
  • it does have iostream and string and ctype.h it just for some reason wouldnt allow me to put it on this post – Luis Maldonado Mar 25 '18 at 18:50
  • I also have to use scanf_s since this is how the professor wants it. – Luis Maldonado Mar 25 '18 at 18:51
  • OK. Give the professor what they want and pass the class, but keep two things in mind. 1) They are likely teaching you C rather than C++, so augment your education with [some good books on C++](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) before you get some nasty surprises when looking for C++ jobs after graduating. 2) `scanf_s` is optional, not a mandatory part of C++, and you can't expect it to be present in all systems you program going forward. – user4581301 Mar 25 '18 at 19:05
  • He says it's gonna be important when working with controllers especially since they can't use cin – Luis Maldonado Mar 25 '18 at 19:18
  • He's confusing two issues. `cin` is going to be present if Standard-compliant C++ is available. If Standard compliant C++ is not available and Standard-compliant C is available, avoid C++ and code in C. Who knows what other nasty surprises you will find in the butchered C++ implementation. If you are coding in C, use `scanf` over `scanf_s` because `scanf_s` may not exist at all. I'm out of date in C these days. I don't know if `scanf_s` was optionally inserted into into C11. Not seeing any mention of it, though. – user4581301 Mar 25 '18 at 19:21
0

scanf_s needs a buffer size. Something like:

char word[40] = "";
printf("Enter a Statement: ");
scanf_s("%s", word, 40);
user4581301
  • 33,082
  • 7
  • 33
  • 54