-2

Some weird error comes up after I enter a string. Any help would be appreciated. What I have so far:

#include <stdio.h>
#include <string.h>

void main()
{
    int i;
    char line[100];

    printf("Enter a string: \n");
    gets(line);

    for (i = 0; line[i] != '\0'; i++)
    {
        if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' || line[i] == 'o' || line[i] == 'u' || line[i] == 'A' || line[i] == 'E' || line[i] == 'I' || line[i] == 'O' || line[i] == 'U')
        {
            puts(line[i]);
        }
    }
    system("pause");
}
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
DaftTommy
  • 1
  • 1
  • 4
    What is the error? Details, man, details ... – AntonH Feb 13 '17 at 19:25
  • 4
    Turn on full warnings. The argument to `puts()` must be `char*`, but `line[i]` is a `char`. You want `putc()`. – Barmar Feb 13 '17 at 19:26
  • Unhandled exception at 0x0f5067cf (msvcr100d.dll) in assignmen5.exe: 0xC0000005: Access violation writing location 0xffffffcc. And another code window pops up with a bunch of code – DaftTommy Feb 13 '17 at 19:31
  • As Barmar says: you are trying to use a vowel as a memory address: hence the access violation. – Weather Vane Feb 13 '17 at 19:37
  • I tried using put c but it gives error: too few arguments for call – DaftTommy Feb 13 '17 at 19:48
  • 1
    @DaftTommy I guess that `putchar` was meant, which goes to `stdout`, whereas `putc` goes to a file stream that needs to passed as another argument. Please get accustomed to reading man pages. But you could have used `putc` like this `putc(line[i], stdout);` – Weather Vane Feb 13 '17 at 20:42
  • Do you need help with the algorithm or with your compilation/runtime issues? – Rishi Feb 14 '17 at 04:46

1 Answers1

1

void main() is not a valid entry point. You probably meant int main(void).

line[i] in puts(line[i]); isn't a char *, let alone a pointer to a string; it's just a character. This is most likely to cause problems.

system("pause"); is annoying for end users, non-portable and potentially dangerous. Consider using your IDE to place a break-point on your return 0; statement, when you insert that. That way your end users won't be bothered by unnecessary pauses in their scripts, error messages about unknown commands or worse, security breaches due to a malicious pause.sh being placed somewhere...

autistic
  • 1
  • 3
  • 35
  • 80
  • `void main()` is perfectly valid in some systems, most notably freestanding ones. http://stackoverflow.com/a/31263079/584518. Though of course the presence of `system()` does suggest that this is a hosted system. Even still, the C99/C11 standards are unclear about which forms of main() that are allowed even for hosted systems. – Lundin Feb 16 '17 at 12:34