Thread: Confused Student

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    28

    Confused Student

    Hi,

    I've just recently started a C For Engineers class at my university, and am completely and utterly confused. ...If I am asking questions that are in the FAQs, I'm sorry, I have no idea what I'm looking for-

    Here's my problem. This C For Engineers class covers Engineering-related programming problems. Our professor is going over basic C functions and is teaching us how to solve mathematical problems using C, and I completely understand everything he is going over. However, he has completely neglected to discuss anything involving the tools required to create these programs. (Yes, I understand the process one must go through in order to create a program, that is not what I'm having a problem with) We've all been supplied with a copy of Microsoft Visual Studio .NET Pro, and were basically told to figure it out ourselves. ...I, however, have a mac, running OSX. So, naturally, I'd instead acquire XCode.

    Without any of us knowing what to do, he has assigned a programming assignment, with a due date within the next few days. ...We must turn in a smooth-running program.

    It's an incredibly simple program... A simple math problem. But I need to somehow, compile, debug, and test this program, so it meets his specifications and I get a decent grade. It's a short bit of text that I can't figure out how to compile in XCode (being that XCode is primarily set up to create OSX applications.)

    I've created a new C file, have input all of my code, and now want to compile. ...But the compile option is greyed out, and inaccessable.



    How do I compile this incredibly simple script on my mac?
    I seem to have hit a brick wall.

    All of the tutorials I've found on the internet teach you how to make, compile, and debug applications for OSX- I just want to test this short bit of code for this class- and nothing more.


    Any suggestions??


    Thanks, (I do realize this has become a bit lengthy, sorry.)

    Heather

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    I've never used XCode, but it most likely requires you to create a project, add files to the project, and then compile the project.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I think XCode is just an IDE built around GCC. If that's the case, you could always just use GCC and forgo the use of XCode entirely.

    Just open up a terminal window, and type in something like:
    Code:
     gcc myfile.c -o myprogram
    That will compile your C file (myfile.c) into an executable file (myprogram).

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    If bithub's suggestion works for you, and you are able to use gcc on your machine, if you are going to debug your program, you will need provide the debugging info in the executable. You can do this by using the -ggdb flag. And if you are using functions from the math library, be sure to link it using the -lm flag - its not enough to just #include math.h.

    Code:
    gcc -ggdb -o myprogram -lm myprogram.c
    ~/
    Last edited by kermit; 01-22-2005 at 03:48 PM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    28
    Wow, thanks for all of the help...

    The gcc compile command worked great on an already debugged example c file I generated. ...It generated an executable file that worked fine.

    However, when I try to compile the assignment, it comes up with errors, and as a result, doesn't generate an executable file. So, I obviously must debug it. Kermit, I tried using the debug command that you gave me, but I come up with the same error... And as for the -lm flag and the #include math.h, I was never instructed to use either of those commands in the class (yet)- the only #include I'm using is the #include stdio.h - so I have no idea what to do with those... (I'm completely new to this.)

    Any idea as to how I can debug this?
    (Or if I've got it all wrong, please... explain it to me... heh)


    Thanks again!

    Heather Miller

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You can post your code, and we can help you spot your mistakes and offer solutions - that's mostly what we do here. Just make sure you use code tags [ code ] at the beginning of your code and [ /code ] at the end of your code (don't put spaces between the [ and c, etc...). They will preserve your indentation and put it in a more approrpiate font.

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    68
    If your only including "stdio.h" you wont need to link to the Math library. Well to help debugging what errors are you getting and what is the code you have?

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    28
    Wow, great. Thanks again for the help.

    This is my first program, so granted, there will be a lot of unnecessary stuff as well as errors...
    (That, and the assignment was really stupid.)

    Problem: Write a program that takes the length and width of a rectangular yard and the length and width of a rectangular house situated in the yard. The program should calculate and display the time required to cut the grass at the rate of two square feet a second.


    This, (after only one lecture) is what I've come up with. Pardon the errors:

    Code:
    #include <stdio.h>
    
    #define CUT_RATE 2.0
    
    int main( void )
    {
    	double lyard; /*INPUT: length of yard*/
    	double wyard; /*INPUT: width of yard*/
    	double lhouse; /*INPUT: length of house*/
    	double whouse; /*INPUT: width of house*/
    	
    	double Ayardt; /*OUTPUT: total area of yard w house*/
    	double Ahouse; /*OUTPUT: area of house*/
    	double Ayard; /*OUTPUT: area of yard wout house*/
    	double time; /*OUTPUT: total time required to cut grass at given speed*/
    	
    	/*Enter dimensions of yard and house*/
    	printf("Enter the length of the rectangular yard in feet> ");
    	scanf("%1f", &lyard);
    	
    	printf("Enter the width of the rectangular yard in feet> ");
    	scanf("%1f", &wyard);
    	
    	printf("Enter the length of the rectangular house in feet> ");
    	scanf("%1f", &lhouse);
    	
    	printf("Enter the width of the rectangular house in feet> ");
    	scanf("%1f", &whouse);
    	
    	/*Calculate the total area of the yard*/
    	Ayardt = lyard * wyard
    			
    	/*Calculuate the area of the house*/
    	Ahouse = lhouse * whouse
    			
    	/*Calculate the area of the yard without the house*/
    	Ayard = Ayardt - Ahouse
    			
    	/*Calculate the total time req to cut lawn*/
    	time = Ayard / CUT_RATE
    			
    	/*Display the area of the yard without the house and the total time req to cut yard*/
    	printf("Total area of yard without house: %0.2f ¥nTotal cutting time of yard: %0.2f ¥n", Ayard, time);
    	
    	return 0;

    The only error that comes up in the terminal is:
    "project1.c: In function `main':
    project1.c:52: error: parse error before "Ahouse""

    Does that mean that there's only one error? Or is it that this is only one out of a number of errors?



    Thanks again for all of the help!
    (This is more than I could have ever expected in class... We don't even have text books yet)

    Heather

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You're missing semicolons at the end of your assignment statements in which you perform a mathematical operation. There were several instances of this mistake, but it would appear that the compiler only reported the first.

  10. #10
    Registered User
    Join Date
    Jul 2004
    Posts
    68
    Also isnt it %f instead of %1f in the scanf's

  11. #11
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    >>Also isnt it %f instead of %1f in the scanf's
    No, %f would only be correct if the type of the variables were float instead of double. It should be %lf using the letter l instead of the number 1.

  12. #12
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Something else to consider when you use gcc is to invoke the compiler with the -Wall (warnings all) flag.

    Sample output without -Wall:

    Code:
    kermit@mainbox ~/cprogs $ gcc -o eg eg.c
    eg.c: In function `main':
    eg.c:33: error: parse error before "Ahouse"
    And now output with -Wall:

    Code:
    kermit@mainbox ~/cprogs $ gcc -Wall -o eg eg.c
    eg.c: In function `main':
    eg.c:19: warning: float format, double arg (arg 2)
    eg.c:22: warning: float format, double arg (arg 2)
    eg.c:25: warning: float format, double arg (arg 2)
    eg.c:28: warning: float format, double arg (arg 2)
    eg.c:33: error: parse error before "Ahouse"
    eg.c:13: warning: unused variable `Ahouse'
    eg.c:14: warning: unused variable `Ayard'
    eg.c:15: warning: unused variable `time'
    It's a helpful flag (-Wall) to use.

    ~/

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    28
    Thanks for all of your help-

    I now have my first (strange) working program.
    I've added a couple of things to it- so now, instead of it just giving you an answer in seconds, it gives you both an answer in seconds and an answer in minutes.

    Thanks again for all of your help!

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    28
    Hi,

    Again, thanks for all of the help... But a new problem has arisen, and I don't quite know how to go about it. (Like I mentioned earlier, we have no book, and some things are very unclear in this class... The professor seems to be of little help sometimes.)

    Alright, I'll restate the problem:

    Problem: Write a program that takes the length and width of a rectangular yard and the length and width of a rectangular house situated in the yard. The program should calculate and display the time required to cut the grass at the rate of two square feet a second.

    ...The one requirement that I didn't understand was having only two inputs. ...There are four variables, yet, our professor only wants two input functions. ...Meaning that he wants the length and width of the yard put in in the same input function, and the length and the width of the house to be put into the same input function as well.

    ...How would I go about this?
    I don't want anyone to write out any code for me, I'd just like to figure it out. If anyone could link me to a website that explains this, or can explain it to me, I'd be very grateful.

    This is the working program that I have so far:



    Code:
    #include <stdio.h>
    
    #define CUT_RATE 2.0
    #define MINUTE 60
    
    int main( void )
    {
    	double lyard; /*INPUT: length of yard*/
    	double wyard; /*INPUT: width of yard*/
    	double lhouse; /*INPUT: length of house*/
    	double whouse; /*INPUT: width of house*/
    	
    	double Ayardt; /*OUTPUT: total area of yard w house*/
    	double Ahouse; /*OUTPUT: area of house*/
    	double Ayard; /*OUTPUT: area of yard wout house*/
    	double timeS; /*OUTPUT: total time required to cut grass at given speed IN SECONDS*/
    	double timeM; /*OUTPUT: total time required to cut grass at given speed IN MINUTES*/
    	
    	/*Enter dimensions of yard and house*/
    	printf("Enter the length of the rectangular yard in feet> ");
    	scanf("%lf", &lyard);
    	
    	printf("Enter the width of the rectangular yard in feet> ");
    	scanf("%lf", &wyard);
    	
    	printf("Enter the length of the rectangular house in feet> ");
    	scanf("%lf", &lhouse);
    	
    	printf("Enter the width of the rectangular house in feet> ");
    	scanf("%lf", &whouse);
    	
    	/*Calculate the total area of the yard*/
    	Ayardt = lyard * wyard;
    			
    	/*Calculuate the area of the house*/
    	Ahouse = lhouse * whouse;
    			
    	/*Calculate the area of the yard without the house*/
    	Ayard = Ayardt - Ahouse;
    			
    	/*Calculate the total time req to cut lawn SECONDS*/
    	timeS = Ayard / CUT_RATE;
    	
    	/*Calculuate total time req to cut lawn MINUTES*/
    	timeM = timeS / MINUTE;
    			
    	/*Display the area of the yard without the house and the total time req to cut yard*/
    	printf("Total area of yard without house: %0.2f \nTotal cutting time of yard: %0.2f seconds, or %0.2f minutes\n", Ayard, timeS, timeM);
    	
    	return 0;
    }

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    28
    Okay, another little bit of information...

    I was previously compiling and executing the file on a mac. Now, I've tried it in Visual Studio as well as in Mircacle C, and it seems as if I'm getting errors.

    Does anyone see any errors that I'm missing?



    This is the build log:

    Build Log


    ------- Build started: Project: blah, Configuration: Debug|Win32 -------

    Command Lines


    Creating temporary file "c:\Documents and Settings\Heather\My Documents\Visual Studio Projects\blah\Debug\RSP000003.rsp" with contents
    [
    /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /Gm /EHsc /RTC1 /MLd /Yu"stdafx.h" /Fp"Debug/blah.pch" /Fo"Debug/" /Fd"Debug/vc70.pdb" /W3 /c /Wp64 /ZI /TP
    ".\blah.cpp"
    ]
    Creating command line "cl.exe @"c:\Documents and Settings\Heather\My Documents\Visual Studio Projects\blah\Debug\RSP000003.rsp" /nologo"

    Output Window


    Compiling...
    blah.cpp
    c:\Documents and Settings\Heather\My Documents\Visual Studio Projects\blah\blah.cpp(71) : fatal error C1010: unexpected end of file while looking for precompiled header directive

    Results


    Build log was saved at "file://c:\Documents and Settings\Heather\My Documents\Visual Studio Projects\blah\Debug\BuildLog.htm"
    blah - 1 error(s), 0 warning(s)






    I see something in there about compiling a cpp file? When I saved it as a c file? I don't know if that's the problem?
    Last edited by verd; 01-24-2005 at 02:11 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Updating in a sequential file?
    By Ronnyv1 in forum C Programming
    Replies: 1
    Last Post: 03-24-2009, 04:41 PM
  2. Database assignment is Killing me!
    By Boltrig in forum C Programming
    Replies: 2
    Last Post: 11-29-2007, 03:56 AM
  3. LinkList Sorting in C
    By simly01 in forum C Programming
    Replies: 3
    Last Post: 11-25-2002, 01:21 PM
  4. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM
  5. A few tips please...
    By gems in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2002, 02:28 PM