Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Getting annoying complile error in C!

Options
  • 03-03-2008 9:59pm
    #1
    Registered Users Posts: 569 ✭✭✭


    Hi,

    Just learning C at the moment, i use VB in college but use the Dev-C++ from bloodshed.net program at home.

    Anyways i was told by my lecturer that one of the differences in the two programmes are in the compilers and that instead of 'void main()' in VB its 'int main()' in this program i'm using at home. That right?

    Going from there i keep getting this error when i go to compile and i don't know what it means or how to fix it...

    [Build Error] [main1.o] Error 1

    Its a simple program which goes as follows..

    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {
    float fahr;
    int lower, upper, step;

    printf("Enter the lower, higher and step values\n");

    for(fahr=lower; fahr<=upper; fahr=fahr+step)
    printf("%3.0f,%6.1f\n",fahr,(5.0/9.0)*(fahr-32));
    }

    any help much appreciated as its frustrating me alot!


Comments

  • Registered Users Posts: 3,568 ✭✭✭ethernet


    If you're declaring main() as returning an int, best shove in a return(0); statement before the closing } of the main() function (or just declare it as void).

    Are you taking in values? It's just that I see you using printf() but you say to enter values. Do you mean to use scanf()?


  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    Been a while since I've touched C but apart from last post (which is perfectly correct - you must return the type you declare so either code as void main() or shove in a return(0); at the end before the closing brace) it is unlikely that the code will handle floats and integers interacting as you've coded. Best declare them all as type float or use a casting function to change the type (can't remember the equivalent of cdbl() in C but google it and see what you get). There's probably more that I've missed but sure have to leave something for later posters :D

    -RD


  • Closed Accounts Posts: 592 ✭✭✭BubbleWrap85


    Wow, we did C++ in college. Didn't think anyonen actually used it in the real world though. It's only vaguely familiar to me anymore ha ha. What area of work do they use it in?!


  • Registered Users Posts: 569 ✭✭✭Bubba


    Wow, we did C++ in college. Didn't think anyonen actually used it in the real world though. It's only vaguely familiar to me anymore ha ha. What area of work do they use it in?!

    Take a guess at maybe a computer programmer. What do you think!?


  • Closed Accounts Posts: 413 ✭✭sobriquet


    Wow, we did C++ in college. Didn't think anyonen actually used it in the real world though.

    Really? Huh. Piles of it out there.

    OP, that compiles and executes fine for me with gcc, just a straight copy and paste (despite the fact that it doesn't do what you want it to). I'm inclined to think it's a configuration problem with Dev-C++. Plugging the error straight into google gets a few hits, follow those through.


  • Advertisement
  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Wow, we did C++ in college. Didn't think anyonen actually used it in the real world though. It's only vaguely familiar to me anymore ha ha. What area of work do they use it in?!
    What?! :confused: :eek:


  • Closed Accounts Posts: 592 ✭✭✭BubbleWrap85


    LOL sorry I guess that makes me sound quite thick! Suppose what I meant was, I never saw the use of a primary teacher (i.e. me!) knowing how to find the complex roots of equations, or how to use a big formula yokey to calculate fahrenheit as celsius and the likes!! Plus I was writing out programs for a full 14 hours before til 5am in the morning (no word of a lie!) the night/morning before an exam so I don't have the fondest of memories for it....


  • Closed Accounts Posts: 592 ✭✭✭BubbleWrap85


    PLUS, what's more, if I'd known this forum existed anytime in the last 3 years, WOW my life could have been so much easier! Though I never failed any of my exams anyhow so suppose I did okay :) I prefered what we did with Maple thought rather than C++


  • Registered Users Posts: 569 ✭✭✭Bubba


    sobriquet wrote: »
    Really? Huh. Piles of it out there.

    OP, that compiles and executes fine for me with gcc, just a straight copy and paste (despite the fact that it doesn't do what you want it to). I'm inclined to think it's a configuration problem with Dev-C++. Plugging the error straight into google gets a few hits, follow those through.

    Yeah i think now it is a configuration problem too.
    As i started created a new project and selected windows app. Then compiled and it STILL gave me the same problem! The build error! And i hadn't even made any changes to the code...there's defo something wrong!

    Any ideas on what to do?


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    LOL sorry I guess that makes me sound quite thick! Suppose what I meant was, I never saw the use of a primary teacher (i.e. me!) knowing how to find the complex roots of equations, or how to use a big formula yokey to calculate fahrenheit as celsius and the likes!! Plus I was writing out programs for a full 14 hours before til 5am in the morning (no word of a lie!) the night/morning before an exam so I don't have the fondest of memories for it....
    Its a major language for developing software - you were only introduced to VERY BASIC stuff, hence you probably didn't realise what its correctly for. Its not for doing maths like Maple, though it can - its for developing software. More than likely the browser you are using to view this webpage was developed in C++ or an equivalent langauge. :)


  • Advertisement
  • Closed Accounts Posts: 592 ✭✭✭BubbleWrap85


    I know twas basic, but even still, twas confusing enough and considered somewhat "useless" enough for someone that had no interest in it! Still though, I always did say I liked maths! And apologies, no offence was intended by my "who uses it in the real world" comment. Computer programmers = hot! I'm just jealous because I found it hard!!!


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    I know twas basic, but even still, twas confusing enough and considered somewhat "useless" enough for someone that had no interest in it! Still though, I always did say I liked maths! And apologies, no offence was intended by my "who uses it in the real world" comment. Computer programmers = hot! I'm just jealous because I found it hard!!!
    Thats fair enough - you either love it or hate it suppose - most would hate it - and of course we are hot! - anyways better not take over the OPs thread.


  • Registered Users Posts: 569 ✭✭✭Bubba


    Webmonkey wrote: »
    Thats fair enough - you either love it or hate it suppose - most would hate it - and of course we are hot! - anyways better not take over the OPs thread.

    Yeah wouldn't mind some help...:p


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Bubba wrote: »
    Yeah wouldn't mind some help...:p
    for(fahr=lower; fahr<=upper; fahr=fahr+step);

    But you haven't initiated lower to anything - its random memory - no wonder it will crash - guessing this may be the problem :)

    As well you must return a integer since you have a return type which is only proper C


  • Closed Accounts Posts: 592 ✭✭✭BubbleWrap85


    yes yes sorry OP for taking over there! Wish I could be of help but alas, I am not. Hope these boyos are more use! Anyhow, time to go before the mods kick me off for "off topic" stuff! :) Good luck!


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    You also must set Upper and Next to something.

    Edit - Great he gone :( - off to bed I am then...


  • Closed Accounts Posts: 413 ✭✭sobriquet


    Bubba wrote: »
    Any ideas on what to do?

    It's not the initialization that's causing the problem, the program will compile and run fine, though not work the intended way. You'll need to initialize the upper/lower/step variables to something sensible or use scanf or fgets to allow the user to supply them, it works fine then.

    As to configuring Dev-C++, I don't know tbh. Plug that error into google, have a look at what comes up, and read up on the DC++ docs to set it up. You need to make sure the it's pointing at the right linker and libraries, and that the exist on your system. I can't say because I don't know what Dev-C++ installs itself or depends on.


  • Registered Users Posts: 17,577 ✭✭✭✭Mr. CooL ICE


    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      float fahr;
      int lower, upper, step;
      
      printf("Enter the lower, higher and step values\n");
      
      for(fahr=lower; fahr<=upper; fahr=fahr+step)
      printf("%3.0f,%6.1f\n",fahr,(5.0/9.0)*(fahr-32));
    }
    
    If all the program is doing is echoing the answer to the screen, then don't use "int main()"... use "void main()" instead.

    Also, after the line
    printf("Enter the lower, higher and step values\n");
    
    theres no line of code to read in the values. Try putting in something like
    scanf("%d %d %d", lower, higher, step");
    




    Take note of your loop arguments. I can't see what you're trying to do with
    for(fahr=lower; fahr<=upper; fahr=fahr+step)
    
    For a start it probably wouldn't work, depending on your IDE. Never use floats as loop controllers. Always use integers


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    sobriquet wrote: »
    It's not the initialization that's causing the problem, the program will compile and run fine, though not work the intended way. You'll need to initialize the upper/lower/step variables to something sensible or use scanf or fgets to allow the user to supply them, it works fine then.

    As to configuring Dev-C++, I don't know tbh. Plug that error into google, have a look at what comes up, and read up on the DC++ docs to set it up. You need to make sure the it's pointing at the right linker and libraries, and that the exist on your system. I can't say because I don't know what Dev-C++ installs itself or depends on.
    The program will compile correctly but you certainly have to initialise the variables some how, either at run time or scanning them in. As well like said above, these should be integers for stepping through the loop.
    As far as I know with DevC++, you must use int void() - void main() will not allow you to compile, and I don't think void main() is part of the proper C specification though a lot of compilers allow you to use it.

    OP, maybe something like the following would work (not tested though):
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
    	float fahr = 0.0;
    	int lower, upper, step;
    
    	printf("Enter Lower Value: ");
    	scanf("&#37;d",&lower);
    	printf("Enter Upper Value: ");
    	scanf("%d",&upper);
    	printf("Enter Step Value: ");
    	scanf("%d",&step);
    
    	for(fahr=lower; fahr<=upper; fahr=fahr+step)
    		printf("%3.0f,%6.1f\n",fahr,(5.0/9.0)*(fahr-32)); //Automatic Casting here to float
    
    	return 0;
    
    }
    


  • Closed Accounts Posts: 413 ✭✭sobriquet


    Take note of your loop arguments. I can't see what you're trying to do with
    for(fahr=lower; fahr<=upper; fahr=fahr+step)
    
    For a start it probably wouldn't work, depending on your IDE. Never use floats as loop controllers. Always use integers
    I can; it does work. It just inlines the incrementation of fahr and allows floating point step values. It's idiomatic of course to use integers, but I've been in situations before where I needed to use floats, and seen it in other code. It's not categorically wrong.
    Webmonkey wrote: »
    The program will compile correctly but you certainly have to initialise the variables some how, either at run time or scanning them in.
    It compiles and runs, but prints nothing; gcc will initialize the variables to zero and the loop terminates straight away. You're right that it's a bad idea to depend on it though. Initalizing lower, upper and step to proper values makes the code do what it's intended.

    Those aren't the errors. It's a Dev-C++ configuration problem.


  • Advertisement
  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    sobriquet wrote:
    Those aren't the errors. It's a Dev-C++ configuration problem.

    Yeah perhaps but no harm on letting the OP know whats good practice and what isn't. What he done isn't. Hopefully he can rectify the problem anyways :)


  • Closed Accounts Posts: 413 ✭✭sobriquet


    Webmonkey wrote: »
    Yeah perhaps but no harm on letting the OP know whats good practice and what isn't. What he done isn't. Hopefully he can rectify the problem anyways :)

    Oh absolutely. Funnily enough, compiling the OPs code with gcc -Wall only marks the lack of a return as a warning and (by way of a quick google) it turns out you've to use -Wuninitialized to warn for them, and -Wall doesn't include it.


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    sobriquet wrote: »
    Oh absolutely. Funnily enough, compiling the OPs code with gcc -Wall only marks the lack of a return as a warning and (by way of a quick google) it turns out you've to use -Wuninitialized to warn for them, and -Wall doesn't include it.
    Quite strange alright - you'd imagine it a common enough warning


  • Registered Users Posts: 2,534 ✭✭✭FruitLover


    sobriquet wrote: »
    It compiles and runs, but prints nothing; gcc will initialize the variables to zero and the loop terminates straight away.

    I don't think that's the case, as they're defined within the main() function - I was under the impression that variables would only be zeroed outside of a function?

    Edit: just checked this, a printout of vars defined within main() shows random contents. So this program is worse than broken - it's unpredictable!
    Never use floats as loop controllers. Always use integers

    I can't think of any reason for this...?


  • Closed Accounts Posts: 413 ✭✭sobriquet


    FruitLover wrote: »
    Edit: just checked this, a printout of vars defined within main() shows random contents. So this program is worse than broken - it's unpredictable!

    Fair enough and you're right that it's a bad idea. I'm certain though that gcc did that - terminated the loop straight away. As to the variable initialization, for that to happen I assumed the vars must have been zeroed. Perhaps it's the version of gcc on Ubuntu? Possibly.

    C - enough rope to shoot yourself with.


Advertisement