Why does my calculator crash?

This is a discussion on Why does my calculator crash? within the C Programming forums, part of the General Programming Boards category; I wrote a simple calulator in C, trying to teach myself the language because I'm accustomed to C++, but the ...

  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
    2,919
    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
    Posts
    23,033
    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 12:41 PM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    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
    Posts
    23,033
    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
    Posts
    23,033
    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
    Posts
    23,033
    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
    22,312
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    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 01: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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21