Thread: Need help (do while)

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    21

    Need help (do while)

    Here is a code not working because of "do" command

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
        int count_a = 0, count_e = 0, count_o = 0, count_i = 0, count_u = 0;
        char ch;
        do
        {
            ch = getche();
            switch (ch)
            {
            case'a':count_a++;
                break;
            case'e':count_e++;
                 break;
            case'o':count_o++;
                break;
         case'i':count_i++;
             break;
         case'u':count_u++;
                break;
        }
            printf("\n a exist %d times", count_a);
            printf("\n e exist %d times", count_e);
            printf("\n o exist %d times", count_o);
            printf("\n i exist %d times", count_i);
            printf("\n u exist %d times", count_u);
    }
    Please what's wrong why I can't compile it ?
    I'm using codeblocks and microsoft visual 2015. Thank you.
    and please don't answer like open a book and read again.. thanks

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You should post the compiler errors you recieve.

    Quote Originally Posted by nuklon View Post
    and please don't answer like open a book and read again.
    Well, if you did, you would see that "do" requires a corresponding "while", which is not present in your code.

  3. #3
    Registered User
    Join Date
    Apr 2016
    Posts
    21
    Quote Originally Posted by Matticus View Post
    You should post the compiler errors you recieve.



    Well, if you did, you would see that "do" requires a corresponding "while", which is not present in your code.
    I know I'm asking too much, can you please do an simple fast example on my code to get it working it would be great, thanks anyway...

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by nuklon View Post
    I know I'm asking too much, can you please do an simple fast example on my code to get it working it would be great, thanks anyway...
    No can do. This is very basic stuff that you need to work out on your own, if you're ever going to learn the language.

    If you did open a book (or find a tutorial), you would see at least one working example that can easily be applied to your program.

  5. #5
    Registered User
    Join Date
    Apr 2016
    Posts
    21
    Found a mistake
    Code:
    Thanks I found the mistake. #include <stdio.h>#include <stdlib.h>
    #define test 10
    int main()
    
    
    {
    	int count_a = 0, count_e = 0, count_o = 0, count_i = 0, count_u = 0, i = 0;
    	char ch;
    
    
    	do
    	{
    		ch = getche();
    		switch (ch)
    		{
    		case'a':count_a++;
    			break;
    		case'e':count_e++;
    			break;
    		case'o':count_o++;
    			break;
    		case'i':count_i++;
    			break;
    		case'u':count_u++;
    			break;
    		}
    	} while (ch!='.');
    
    
    		printf("\n a exist %d times", count_a);
    		printf("\n e exist %d times", count_e);
    		printf("\n o exist %d times", count_o);
    		printf("\n i exist %d times", count_i);
    		printf("\n u exist %d times", count_u);
    	}

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're on the right track. What exactly is the mistake? You should describe the problem(s) you are having clearly.

    Note that "getche" is non-standard, consider using "getchar()" instead (and making "ch" an int: FAQ).

Popular pages Recent additions subscribe to a feed

Tags for this Thread