Thread: Help with project (New to C)

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    17

    Unhappy Help with project (New to C)

    I am having trouble with a lab in a beginning C class. I have completed most of it, but I cannot get the outputToScreen function to do what it's supposed to (which is show the values on the screen). Can anyone help me with this. Here's the code so far, and I'm only concerned with getting the outputToScreen function to show the correct values on screen.

    Thanks in advance.

    Code:
    #include<stdio.h>
    int getUserInput();
    int calculateArea(int, int);
    int calculatePerimeter(int, int);
    int outputToScreen (int, int, int, int);
    
    
    #pragma warning(disable:4996) 
    
    void main()  
    {
    	int area = 0;
    	int perimeter = 0;
    	int length = 0;
    	int width = 0;
    	printf("\nPlease enter the length of the rectangle as a whole number: ");
    	length = getUserInput();
    	printf("\nPlease enter the width of the rectangle as a whole number: ");
    	width = getUserInput();
        area = calculateArea(length, width);
        perimeter = calculatePerimeter (length, width);
    }
    
    int getUserInput()
    {int input = 0;
    scanf("%d%*c",&input);
    
    return input;
    }
    
    
    int calculateArea(int len, int wid)
    {
    int area = 0;
    area = len * wid;
    return area;
    }
    
    int calculatePerimeter (int len, int wid)
    {int perimeter = 0;
    perimeter = len * 2 + wid * 2;
    return perimeter;
    }
    
    int outputToScreen (int length, int width, int area, int perimeter)
    {
    printf ("\nWhen length is %d", length);
    printf ("and width is %d,", width);
    printf (" area is %d", area);
    printf ("and perimeter is %d.", perimeter);
    return main;
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The function seems correct, but you aren't calling it.
    Further, outputToScreen is trying to return something that doesn't exist.
    Void main is undefined: http://cpwiki.sourceforge.net/Void_main
    Don't strip argument names from your function declarations.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    You never called outputToScreen() in main.

    Also, this makes no sense:
    return main;
    Anyone knows what this means
    scanf("&#37;d%*c",&input);

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    17
    That scanf is exactly how the professor wanted us to keep it. We aren't allowed to change that.

    So instead of return main should it be return 0, or what should it return?

    Also, void main is how he wanted us to keep it.

    What should the outputToScreen function look like for it to work correctly?

    How do I set up outputToScreen in main to call it correctly?

    Sorry for the silly questions, but this is my first day.

  5. #5
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    >That scanf is exactly how the professor wanted us to keep it. We aren't allowed to change that.
    everyone I know will just do scanf("&#37;d",&input);
    I don't know what %*c is.

    >So instead of return main should it be return 0, or what should it return?
    yes you can return 0 or just declare that function as void instead of int and don't return anything.

    >Also, void main is how he wanted us to keep it.


    >What should the outputToScreen function look like for it to work correctly?
    Put it at the end of main before you return 0
    Code:
     int main()
    {
        /*code.....................*/
    
        printf("\nPlease enter the width of the rectangle as a whole number: ");
        width = getUserInput();
        area = calculateArea(length, width);
        perimeter = calculatePerimeter (length, width);
        outputToScreen();
        return 0;
    }
    Also add a return 0 at the end of outputToScreen
    Code:
    int outputToscreen( /*--------*/)
    {
          /*
    
    
    
            */
            return 0;
    }
    Last edited by abh!shek; 06-02-2008 at 12:06 PM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by tristangreer View Post
    So instead of return main should it be return 0, or what should it return?
    Why should it return anything?

    Also, void main is how he wanted us to keep it.
    No. It's undefined, non-standard and it's stupid and idiotic. Change it or bug your professor to change it.
    Void main is a non-standard extension to many compilers and will not work correctly across compilers. It may even give you a bad result when compiled in your compiler. Therefore, you should NOT use void main, ever.

    What should the outputToScreen function look like for it to work correctly?
    Who says it isn't working correctly?

    How do I set up outputToScreen in main to call it correctly?
    You need to learn how to call functions, but you've already called two functions!

    Quote Originally Posted by abh!shek View Post
    >That scanf is exactly how the professor wanted us to keep it. We aren't allowed to change that.
    everyone I know will just do scanf("%d",&input);
    I don't know what %*c is.
    It eats the newline after extracting the number.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    17
    Thank you for showing me how to call it. Now the only problem I have is some crazy math. When I input the numbers for length and width, I get insane numbers. Is there a problem with my outputToScreen function, or it is in the calculate functions. Here's the updated code:

    Code:
    #include<stdio.h>
    int getUserInput();
    int calculateArea(int, int);
    int calculatePerimeter(int, int);
    int outputToScreen ();
    
    
    #pragma warning(disable:4996) 
    
    void main()  
    {
    	int area = 0;
    	int perimeter = 0;
    	int length = 0;
    	int width = 0;
    	printf("\nPlease enter the length of the rectangle as a whole number: ");
    	length = getUserInput();
    	printf("\nPlease enter the width of the rectangle as a whole number: ");
    	width = getUserInput();
        area = calculateArea(length, width);
        perimeter = calculatePerimeter (length, width);
    	outputToScreen();
    	return 0;
    }
    
    int getUserInput()
    {
    int input = 0;
    scanf("&#37;d%*c",&input);
    
    return input;
    }
    
    
    int calculateArea(int len, int wid)
    {
    int area = 0;
    area = len * wid;
    return area;
    }
    
    int calculatePerimeter (int len, int wid)
    {
    int perimeter = 0;
    perimeter = (len * 2) + (wid * 2);
    return perimeter;
    }
    
    int outputToScreen (int length, int width, int area, int perimeter)
    {
    printf ("\nWhen length is %d", length);
    printf (" and width is %d,", width);
    printf (" area is %d", area);
    printf (" and perimeter is %d.", perimeter);
    return 0;
    }
    Thanks for your help so far.

  8. #8
    Registered User
    Join Date
    Jun 2008
    Posts
    17

    Thumbs up Fixed it!

    I fixed the code on my own. Here's what the final code looks like.

    Code:
    #include<stdio.h>
    int getUserInput();
    int calculateArea(int, int);
    int calculatePerimeter(int, int);
    int outputToScreen (int, int, int, int);
    
    
    #pragma warning(disable:4996) 
    
    int main(void)  
    {
    	int area = 0;
    	int perimeter = 0;
    	int length = 0;
    	int width = 0;
    	printf("\nPlease enter the length of the rectangle as a whole number: ");
    	length = getUserInput();
    	printf("\nPlease enter the width of the rectangle as a whole number: ");
    	width = getUserInput();
        area = calculateArea(length, width);
        perimeter = calculatePerimeter (length, width);
    	outputToScreen(length, width, area, perimeter);
    	return 0;
    }
    
    int getUserInput()
    {
    int input = 0;
    scanf("%d%*c",&input);
    
    return input;
    }
    
    
    int calculateArea(int len, int wid)
    {
    int area = 0;
    area = len * wid;
    return area;
    }
    
    int calculatePerimeter (int len, int wid)
    {
    int perimeter = 0;
    perimeter = (len * 2) + (wid * 2);
    return perimeter;
    }
    
    int outputToScreen (int length, int width, int area, int perimeter)
    {
    printf ("\nWhen length is %d", length);
    printf (" and width is %d,", width);
    printf (" area is %d", area);
    printf (" and perimeter is %d.", perimeter);
    return 0;
    }
    Thanks for everything!

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Slight note:
    ouputToScreen does not need to return anything at all because you aren't doing anything with it (plus what it returns makes no sense - what's it for?), so you can make the return for the function void and not return anything.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    Use Following Code

    Quote Originally Posted by Elysia View Post
    Why should it return anything?


    No. It's undefined, non-standard and it's stupid and idiotic. Change it or bug your professor to change it.
    Void main is a non-standard extension to many compilers and will not work correctly across compilers. It may even give you a bad result when compiled in your compiler. Therefore, you should NOT use void main, ever.


    Who says it isn't working correctly?


    You need to learn how to call functions, but you've already called two functions!


    It eats the newline after extracting the number.
    hai...

    Code:
    #include<stdio.h>
    int getUserInput();
    int calculateArea(int, int);
    int calculatePerimeter(int, int);
    int outputToScreen (int, int, int, int);
    
    
    #pragma warning(disable:4996) 
    
    void main()  
    {
    	int area = 0;
    	int perimeter = 0;
    	int length = 0;
    	int width = 0;
    	printf("\nPlease enter the length of the rectangle as a whole number: ");
    	scanf("%d", &length);
    	printf("\nPlease enter the width of the rectangle as a whole number: ");
    	scanf("%d", &width);
      area = calculateArea(length, width);
      perimeter = calculatePerimeter (length, width);
      printf ("When length is %d", length);
      printf ("  and width is %d\n,", width);
      printf (" area is %d\n", area);
      printf ("and perimeter is %d.", perimeter);
    }
    
    int getUserInput()
    {int input = 0;
    scanf("%d%*c",&input);
    
    return input;
    }
    
    
    int calculateArea(int len, int wid)
    {
    int area = 0;
    area = len * wid;
    return area;
    }
    
    int calculatePerimeter (int len, int wid)
    {int perimeter = 0;
    perimeter = len * 2 + wid * 2;
    return perimeter;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM