Thread: need help with a program

  1. #31
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    Code:
     #include<stdio.h>
    void function1();
    void function2();
    void main()
    {
    char variable1;
    char repeat;
    do
    {
    printf("Please press 'b' if you wish to do something \n or\n please press 'v' if you wish to do something else ");
    scanf("%c",&variable1);
    if (variable1=='b')
    {
    function1();
    }
    else
    {
    if (variable1=='v')
    {
    function2();
    }
    printf("Do you wish to repeat the program? (y/n)");
    scanf("%c",&repeat);
    }
    while(repeat=='y');
    }
     
     
     
     
     
    void function1()
    {
    printf("function1 has been utilised");
    }
     
     
     
     
    void function2()
    {
    printf("function2 has been utilised");
    }
    
    


  2. #32
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Learn to indent your code!!!

    Code:
    #include<stdio.h>
    void function1();
    void function2();
    void main()
    {
        char variable1;
        char repeat;
        do
        {
            printf("Please press 'b' if you wish to do something \n or\n please press 'v' if you wish to do something else ");
            scanf("%c",&variable1);
            if (variable1=='b')
            {
                function1();
            }
            else
            {
                if (variable1=='v')
                {
                    function2();
                }
                printf("Do you wish to repeat the program? (y/n)");
                scanf("%c",&repeat);
            }
            while(repeat=='y');
        }
    
        void function1()
        {
            printf("function1 has been utilised");
        }
    
        void function2()
        {
            printf("function2 has been utilised");
        }
    Code:
    Compiling: main.c
    H:\SourceCode\Projects\testc\main.c:4:6: warning: return type of 'main' is not 'int' [-Wmain]
    H:\SourceCode\Projects\testc\main.c: In function 'main':
    H:\SourceCode\Projects\testc\main.c:28:5: error: expected 'while' before 'void'
    H:\SourceCode\Projects\testc\main.c:36:5: error: expected declaration or statement at end of input
    Tim S.
    Last edited by stahta01; 02-02-2012 at 09:10 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #33
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    my code is actually indented on visual studios but when i copy n paste it here it comes out like that. How does your look more neat and have numberse etc? Did you still use the square bracket code tag?

    Do you have any advice concerning the errors with my code and why it isnt working properly?

  4. #34
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by David_Baratheon View Post
    How does your look more neat and have numberse etc? Did you still use the square bracket code tag?
    I removed all non-ASCII code and did use the normal code tags.
    Note: On firefox I must do F5 (refresh) to see the syntax colored high lighting.
    If you have any control code in your code posts like bold etc. then your post will not use this website syntax high lighting.

    Can not help you with your code not working since you have not posted code that compiles without error.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #35
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    so my code works perfectly in your compiler? Strange. Is that what you mean to say?

    hopefully i will figure out how to post code up neater here. sorry if its messy for anyone to read

    what non ascii code did i have?

  6. #36
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    What is your program supposed to do? Also, void main needs to be int main(void) and return 0; at the end

  7. #37
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    Quote Originally Posted by camel-man View Post
    What is your program supposed to do? Also, void main needs to be int main(void) and return 0; at the end
    the program is supposed to ask you whether you'd like to do A or B, and if you choose A its supposed to activate function A, and if you choose B its supposed to activate function B, and once its finished its supposed to ask you whether you'd like to have another go, and if you select yes it will ask you to choose A or B again and if you select no the program ends.

    As far as I knew you can void main() but int void is better practice, but the program should still work with void main() am i right in thinking that? Im looking for the reason that the program wont work at all

  8. #38
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    If I were you I would change it to int main(void) now before you get into any bad habits(also it will save you from hearing it from everyone else on the forum ). Your code will not compile for a number of reasons. One being that you need to supply parameters in your functions at the bottom. If you are not passing anything then put void in the parens. Another thing check your curly braces, make sure none are left open.

  9. #39
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    ok will change it

    Ive added this at the bottom of the main function:

    Code:
     fflush(stdin);
    getchar();
    
    will that solve one of the issues you mentioned?

    will check through the curly brackets. thanks

  10. #40
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    how do i change it to void main?

    Dp I put int Where I initially declare the function, the part where the function is defined, or both?

  11. #41
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    take out fflush(stdin) and what you should do is put a leading space in your scanf function example scanf(" %c", &variable1); do that for any scanf function that accepts a character input. This allows scanf to ignore any leading whitespace, therefore the '\n' character will be ignored when looping around and getting your next char variable. Just change void main() to int main(void) and return 0; at the bottom of main just inside the ending curly brace.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 11-03-2010, 12:45 PM
  2. Help converting array program to link list program
    By hsmith1976 in forum C++ Programming
    Replies: 0
    Last Post: 02-14-2010, 09:50 PM
  3. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  4. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  5. Replies: 18
    Last Post: 11-13-2006, 01:11 PM