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

[noob] Program stops working for no apparent reason

Options
  • 10-01-2010 7:53pm
    #1
    Registered Users Posts: 180 ✭✭


    Im currently learning c and im using devc++ . I made a simple calculator program that will compile and run ok but then when i type in a number i get an error saying that simple calc.exe has stopped working.

    Does anyone here know what might be causing this problem?
    I didnt post the code because I wasnt sure if im allowed to.


Comments

  • Closed Accounts Posts: 5,082 ✭✭✭Pygmalion


    Darkphenom wrote: »
    Im currently learning c and im using devc++ . I made a simple calculator program that will compile and run ok but then when i type in a number i get an error saying that simple calc.exe has stopped working.

    Does anyone here know what might be causing this problem?
    I didnt post the code because I wasnt sure if im allowed to.

    You can post it, and probably should since I doubt anyone can help without seeing it :P


  • Registered Users Posts: 180 ✭✭Darkphenom


    Pygmalion wrote: »
    You can post it, and probably should since I doubt anyone can help without seeing it :P

    #include <stdio.h>
    #include <conio.h>

    int main (void)
    {
    int num1;
    int num2;
    int sum;

    printf("Enter the two numbers you want to add:");

    scanf("%d", num1);
    scanf("%d", num2);

    sum = num1 + num2;

    printf("The answer is: %d", sum);
    getch();

    }


  • Closed Accounts Posts: 5,082 ✭✭✭Pygmalion


    Darkphenom wrote: »
    #include <stdio.h>
    #include <conio.h>
    
    int main (void)
    {
        int num1;
        int num2;
        int sum;
        
        printf("Enter the two numbers you want to add:");
        
        [b]scanf("%d", &num1);
        scanf("%d", &num2);[/b]
        
        sum = num1 + num2;
        
        printf("The answer is: %d", sum);
        getch();
        
    }
    

    I'm not entirely certain but I think this is the solution (lines in bold are changed, now passes address of variable instead of it's value to scanf).


  • Registered Users Posts: 3,862 ✭✭✭mikhail


    Yep, that looks like the problem alright.

    As an aside, I'd recommend against using scanf in general.


  • Registered Users Posts: 180 ✭✭Darkphenom


    Yes the program works and thanks for the solution

    mikhail wrote: »
    Yep, that looks like the problem alright.

    As an aside, I'd recommend against using scanf in general.

    what can you use instead of scanf?


  • Advertisement
  • Closed Accounts Posts: 4,564 ✭✭✭Naikon


    Darkphenom wrote: »
    Yes the program works and thanks for the solution




    what can you use instead of scanf?

    Whatever you do, don't use gets() or puts(), these are HORRIBLY unsafe for getting input from the user in a C program. Use scanf to get the hang of things.
    You will understand pointers a bit handier of you understand that the '&' symbol means giving the address(ie where it lives in RAM)to the scanf function and
    not the value. A pointer is like a shortcut on the desktop, it POINTS to a location, where a value lives. Learning C can be troublesome when it comes to silly
    mistakes. But it's a much better language to learn how the machine you are programming on works. Java and the scripting langauges hide this abstraction.

    Move onto others when you get the basics of C. I am waffling on here. To answer your question, you should get values from the user by doing the following:
    fgets(buffer, sizeof(buffer), stdin) 
    sscanf(buffer, "%d", &num_1);
    
    fgets is another similar function. buffer is the variable holding say "53", sizeof determines size, and stdin is a fancy word for your input source such as your keyboard.
    Now, line one reads the 53 entered by the user into a string datatype(not int), then the string is parsed(ie broken down)into an integer by sscanf and stored in num_1.

    This means that if the user types "Dave" where he should type a number, you can add more code to detect and prompt him to enter a proper value.
    Converting any value typed in such as "Dave" will give a wrong result, but it won't cause any crashes. Make sure the length of arrays and such are ALWAYS checked.

    I take it you are in college if you presume you can't post the code. Not posting the code in this instance would be the same as not posting that 2+2=4.
    Unless it's something patented by a company or person, you should share code where possible. It's a shame colleges assume sharing code is always "cheating".
    Peer review is a good thing, as many eyes are better than one when it comes to determining bugs or other nasties. Sorry if this comes across as a bit cyptic, I
    know well how tricky C was at the start. The more you practice though, the easier programming in C will become.


  • Registered Users Posts: 180 ✭✭Darkphenom


    Naikon wrote: »
    Whatever you do, don't use gets() or puts(), these are HORRIBLY unsafe for getting input from the user in a C program. Use scanf to get the hang of things.
    You will understand pointers a bit handier of you understand that the '&' symbol means giving the address(ie where it lives in RAM)to the scanf function and
    not the value. A pointer is like a shortcut on the desktop, it POINTS to a location, where a value lives. Learning C can be troublesome when it comes to silly
    mistakes. But it's a much better language to learn how the machine you are programming on works. Java and the scripting langauges hide this abstraction.

    Move onto others when you get the basics of C. I am waffling on here. To answer your question, you should get values from the user by doing the following:
    fgets(buffer, sizeof(buffer), stdin) 
    sscanf(buffer, "%d", &num_1);
    
    fgets is another similar function. buffer is the variable holding say "53", sizeof determines size, and stdin is a fancy word for your input source such as your keyboard.
    Now, line one reads the 53 entered by the user into a string datatype(not int), then the string is parsed(ie broken down)into an integer by sscanf and stored in num_1.

    This means that if the user types "Dave" where he should type a number, you can add more code to detect and prompt him to enter a proper value.
    Converting any value typed in such as "Dave" will give a wrong result, but it won't cause any crashes. Make sure the length of arrays and such are ALWAYS checked.

    I take it you are in college if you presume you can't post the code. Not posting the code in this instance would be the same as not posting that 2+2=4.
    Unless it's something patented by a company or person, you should share code where possible. It's a shame colleges assume sharing code is always "cheating".
    Peer review is a good thing, as many eyes are better than one when it comes to determining bugs or other nasties. Sorry if this comes across as a bit cyptic, I
    know well how tricky C was at the start. The more you practice though, the easier programming in C will become.

    Thanks for all the help. No I'm not in college yet still in secondary school with a bit of extra time in transition year to do learn things like programming and such. The reason I wasn't sure about posting code was because i got the impression this would be a really strict part of the forum thats all.


  • Closed Accounts Posts: 5,082 ✭✭✭Pygmalion


    Darkphenom wrote: »
    Thanks for all the help. No I'm not in college yet still in secondary school with a bit of extra time in transition year to do learn things like programming and such. The reason I wasn't sure about posting code was because i got the impression this would be a really strict part of the forum thats all.

    It can be, but as long as you wrote the code yourself and you aren't trying to cheat on homework or do anything stupid/illegal always assume you can post code :P


  • Closed Accounts Posts: 9 dannystaple


    Because the pointer thing can be quite tricky for those new to C, I spent some time writing about it, hoping that my metaphors make it easier to understand. The simple thing is to consider having the address of a house without holding the whole house, one is clearly more portable and less cumbersome than the other.
    Even more interesting is the idea of a pointer, like in this case, to somewhere to store something. Pulling a shelf out or a storage shed and bringing it to somebody would probably work pretty badly, but telling them which shed and which shelf to go store it in (an address), would be more useful.

    My article on this subject: http://www.squidoo.com/understanding-passing-by-reference

    I hope it helps out!


  • Closed Accounts Posts: 4,564 ✭✭✭Naikon


    +1 on the C programming book by K&R.

    That book is the defacto reference for C. Nicely condensed into less than 300 pages. It won't make you a master of C overnight, but it does give a pretty
    good grounding of the important concepts. It's a bit hard to read at first for a new programmer, but it won't bore you to tears like a certain 1500+ page
    Java book will. If you want to understand C well, get that book and practice the questions in it. Then move onto "real" projects. Sourceforge is good.

    Don't get too uptight about pointers, they take a little getting used to considering most langs these days do not allow you such fine grained control over
    memory. Practice some exercises on pointers and you will understand the concepts and cement them into your long term memory with time. If you need
    automatic memory managment libgc is worth looking into, even if it is a little cyptic. Don't worry about that stuff for the moment though.

    Good luck.


  • Advertisement
Advertisement