Thread: Why does my calculator crash?

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    20

    Post Why does my calculator crash?

    I wrote a simple calulator in C, trying to teach myself the language because I'm accustomed to C++, but the program crashes once user inputs the second number for calculation. I'm wondering if there's something wrong with my code (though I can't seem to spot anything) or that Windows 7 doesn't get along with C. Can anyone tell me what's the issue is here?

    The code is as follows:

    Code:
    #include <stdio.h>
    
    int main(void){
    
    	int geti[3];
    
    	printf("Write something: ");
    		scanf("%d", geti[0]);
    	printf("write something else: ");
    		scanf("%d", geti[1]);
    
    	geti[2] = geti[0] + geti[1];
    
    	printf("Your sum is %d", geti[2]);
    	return 0;
    	}

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Do you get anyerror/warning messages when you compile? (you should).

    Jim

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    34
    you need to pass address like so
    Code:
    #include <stdio.h>
    
    int main(void){
    
    	int geti[3];
    
    	printf("Write something: ");
    	scanf("%d", geti[0]);
    	printf("write something else: ");
    		scanf("%d", geti[1]);
    
    	geti[2] = geti[0] + geti[1];
    
    	printf("Your sum is %d", geti[2]);
    	
    	
    	return 0;
    }

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You forgot to pass the address, like so:

    >>scanf("%d", &geti[0]);
    >>scanf("%d", &geti[1]);

    Out of curiosity, for someone knowing C++, why would you learn C now?
    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.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ahernan17 View Post
    I wrote a simple calulator in C, trying to teach myself the language because I'm accustomed to C++, but the program crashes once user inputs the second number for calculation. I'm wondering if there's something wrong with my code (though I can't seem to spot anything) or that Windows 7 doesn't get along with C. Can anyone tell me what's the issue is here?

    The code is as follows:

    Code:
    #include <stdio.h>
    
    int main(void){
    
    	int geti[3];
    
    	printf("Write something: ");
    		scanf("%d", geti[0]);
    	printf("write something else: ");
    		scanf("%d", geti[1]);
    
    	geti[2] = geti[0] + geti[1];
    
    	printf("Your sum is %d", geti[2]);
    	return 0;
    	}
    Shold be scanf("%d", &geti[0]); etc.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    20
    Well now that was a duh issue, the passing worked like a charm. Thanks for the help.

    And for the guy asking about why I would want to learn C, I just like the simplicity of the language and it never hurts to expand your tongue. Though I'm sure some would argue otherwise.
    Last edited by ahernan17; 10-23-2010 at 11:41 AM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Honestly, C is anything but simple.
    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.

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    20
    Like I said, some people would argue that. Though I am just a deviant.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's a fact. Whether or not you find it simple to understand, use and learn is another matter.
    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
    Sep 2010
    Posts
    20
    But worth it from what I've seen.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Interesting observation. How do you figure? Curiousness? Power? Flexibility?
    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.

  12. #12
    Registered User
    Join Date
    Sep 2010
    Posts
    20
    All of those, but mostly curiousness, considering things such as Unix, Linux, lots of GNU programs, SDL and OpenGL are built from C, I'd gather it will get any job done, when done right.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Unix/Linux is in C because C++ didn't exist / wasn't mature at the time.
    SDL / OpenGL is in C mainly, I believe, due to backwards compatibility for C (for some bizarre reason).
    A lot of GNU programs being C is mostly a mistake.

    I will say this, though. C is a very dangerous language, and it's a primitive language.
    It is not suited very well for today's demands for applications.
    Thus, using C for any modern applications for PC is a mistake, if for anything else than learning, or just as a hobby. But even as a hobby, it's really stretching its limits. C is a big secure hole that needs to be used properly in order not to cause significant security risks.

    Consider that C++ will also get any job done, in shorter time too usually, no less. It also creates more flexible design and less security holes.
    Games are very complicated pieces of software, and 99% of them are written in C++, and for good reason. C is out of the question for that 99%. Ponder on that.
    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.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Elysia: please stop preaching against C in the C programming forum. I can understand if you are suggesting that another language is better than C for a given problem at hand, but here the problem is an exercise to learn C.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, no trying to say don't learn C. Just trying to make the OP understand C and its limits.
    Trying to iron out misconceptions such as C, and only C, can do all these tasks. If you're going to learn C, you should understand what it is and what it isn't.
    So long as you're aware of that, it's fine.
    Right?

    I mean, a lot of people learning C are under the impression that C is the language, capable of everything. There must a reason why everything is written in C, right!?!
    It just turns out that it isn't true. We have other great languages, as well.
    So don't learn C because it is the language. You're just shooting yourself in the foot.
    Learn it because of curiosity, as a hobby, or where no other languages are available.
    Last edited by Elysia; 10-23-2010 at 12:17 PM.
    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.

Popular pages Recent additions subscribe to a feed

Tags for this Thread