Thread: Array

  1. #1
    Unregistered
    Guest

    Array

    #include <conio.h>
    #include <stdio.h>

    char phrase[50];
    int count;

    int cvowel();

    void main()
    {
    clrscr();
    printf("Enter a phrase :");
    gets(phrase);
    count = cvowel();
    printf("The number of vowels are : %i\n",count);
    printf("New Phrase :%s ",phrase);
    getch();
    }
    int cvowel()
    {
    int value=0;
    int index;

    for(index= 0;index<0;index++);
    {
    if (((phrase[index] == 'a') || (phrase[index] == 'e')) ||
    (phrase[index] == 'i') || (phrase[index] == 'o') ||
    (phrase[index] == 'u'))
    {
    value++;
    phrase[index]='X';
    }

    }

    return value;
    }

    I cant make this program run on one full phrase but i can run with a single character...

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    202
    Well, first of all, main is supposed to return an int. Fix that. Secondly, use fgets(), which is like gets(), only safer, as it prevents the user from entering morecharacters than your array can handle. Also, what is the getch() doing at the end of main()?

    starX
    www.axisoftime.com

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    in cvowel()
    erase the ; at the end of for
    for(index= 0;index<0;index++);<--------

    Then change it like this;

    for(index = 0; index < 50; index++)

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    Actually this is a correction to what starX said

    There are three ways to write main with no arguments
    int main()
    {
    ...
    ...
    return 0;
    }

    main()
    {

    return 0;
    }

    void main()
    {


    }

    Also fgets is for reading strings from a FILE stream.
    Also I think that getch is for waiting for the user to end program
    by pressing some key

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    3

    Question

    What is the “;” doing after “for(index= 0;index<0;index++);” ?

    It will cancel the loop and only the 1st character will be checked and counted.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    202
    ozgulker, fgets can read from stdin, and everything I've ever read tells me that main returns an int, it may work other ways, but I know it is supposed to return an int.

    starX
    www.axisoftime.com

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    I'm sorry about fgets, starX. Yes you are right about it.

    However for operating systems, main is just another regular function which returns or do not return anything. (it maybe int, char, long, float etc, nothing).

    Also the last return statement at the end of main returns that value to operating systems. It is possible for example in UNIX to write code that uses that return value, so it is not a dummy statement.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM