Thread: Help- Problems running outside of compiler

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    43

    Help- Problems running outside of compiler

    I wrote a calculator program earlier, and I got everything to work fine in the compiler. However, I have run into a problem when running it outside of the compiler. After it displays the sum/difference/product/quotient of the given numbers, it closes the window before I can see the solution. I've tried putting system("pause") before return 0; in each block of code for the operations, but it does nothing but generate errors. Here's all the necessary code:
    Code:
    #include <iostream.h>
    
    int mult(int x, int y);
    
    int add(int x, int y);
    
    int sub(int x, int y);
    
    int div(int x, int y);
    
    int main()
    
    {
    
    	int a;
    
    	cout<<"What would you like to do?"<<endl;
    	
    	cout<<"1.Add"<<endl;
    	
    	cout<<"2.Subtract"<<endl;
    	
    	cout<<"3.Multiply"<<endl;
    	
    	cout<<"4.Divide"<<endl;
    
    	cin>>a;
    
    	if(a==1)
    
    	{
    
    		int x, y;
    
    		cout<<"Please enter two numbers to be added:"<<endl;
    
    		cin>>x>>y;
    
    		cout<<"The sum of your two numbers is "<<add(x, y)<<endl;
    
    		return 0;
    
    	}
    Same thing 3 more times, but changed slightly for the different operations...
    Code:
    }
    
    int add(int x, int y)
    
    {
    
    	return x+y;
    
    }
    Again, same thing over and over for different operations.

    If anybody can tell me what I can put in there to get it to stay open long enough for me to read the output, I will be very thankful... Also, can somebody tell me what I can use instead of int so I can use decimals instead of being restricted to integers? Thank you in advance

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    did you include the right header file for system?

    I think it might be in <windows.h> but I'm not sure.

    otherwise use <conio.h> and getch()

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    43
    Hm... Well, if its not <iostream.h>, then its "stdlib.h", because I think thats the only other header file I've used. Thanks

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    you're welcome

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    you could also check the FAQ

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    <stdlib.h>

    Or better, <iostream> and <cstdlib>.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Student Forever! bookworm's Avatar
    Join Date
    Apr 2003
    Posts
    132
    I too had created a calculator a long time back and had the same problem.A program stops before completing its function if it cannot handle an exeption or does not understand a loop.Hence,always debug your project.
    Also,I see that just like me,you allow the user to enter only 2 nos. I would advise u to create 2 different steps for your user to enter the 2 nos.The easier the program is to run,the lesser bugs it will have.

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    43
    Well... I got it all worked out now... Except that the answers don't come out in decimals... But it'll run outside of the compiler so its all good... And CornedBee, some people (like me) still need to use the .h for header files, so don't go around telling people to do it without .h or you'll confuse people.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Derek: there are free compilers on the web (like mingw32) that have modern standard libraries (no .h), so don't tell me you have to use .h. If you're in a programming class then tell your teacher that the compilers are outdated and that they should get new ones.

    And I'm not confusing people. I see many people who are confused because there are two header types, and you seemingly have to use one type, then the other. I go around and tell them to use one and forget about the other. How is that confusing?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    May 2003
    Posts
    43
    It isn't confusing for me, but some people can be very easily confused... And I'm not taking a programming class, I'm using a compiler I got a few years ago. And I program on my other computer, which does not have an internet connection, so I can't download a new compiler for it... And I don't want to waste a cd-r just so I can get a new one when the one I have works fine.

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Not too many people really care what you do, but it is standard to use the new headers.

    As for the compiler, you could use a cd-rw. But if you want to use the one since a few years ago, feel free.

  12. #12
    Registered User
    Join Date
    May 2003
    Posts
    43
    Yes, I could use a cd-rw. Problem is, I don't know where the heck it is!
    Programmer's Law:

    If your program doesn't work, look for the part that you didn't think was
    important.

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>And I don't want to waste a cd-r
    But, they cost like next to nothing now-a-days

    In answer to your question, (as already pointed out): http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    Registered User
    Join Date
    May 2003
    Posts
    43
    Oh yeah... Got it all fixed last night. And I got it to accept decimal numbers Won't give the answers in decimals, but I still got it to work!

    And I already have a stack of cd-rs with next to nothing on them in front of me so I don't want to waste anymore.
    Programmer's Law:

    If your program doesn't work, look for the part that you didn't think was
    important.

  15. #15
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Originally posted by Derek5272
    It isn't confusing for me, but some people can be very easily confused... And I'm not taking a programming class, I'm using a compiler I got a few years ago. And I program on my other computer, which does not have an internet connection, so I can't download a new compiler for it... And I don't want to waste a cd-r just so I can get a new one when the one I have works fine.
    Unfortunately, I am like him in this. I have the ones without the .h, but they usually don't work right. I'll be buying MSVC++ .NET 2003 when it comes out... huge update, hopefully those dang template bugs are fixed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Internal Compiler Error
    By Elysia in forum C++ Programming
    Replies: 20
    Last Post: 07-05-2008, 03:59 AM
  2. Dev C++ Compiler, Indentation?
    By Zeusbwr in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2004, 06:13 AM
  3. OpenScript2.0 Compiler
    By jverkoey in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2003, 01:52 PM
  4. Compiler Design... Anyone That Can Help!
    By ComputerNerd888 in forum C++ Programming
    Replies: 3
    Last Post: 09-27-2003, 09:48 AM
  5. Running Into Two Problems...Help Needed
    By Halo in forum C Programming
    Replies: 5
    Last Post: 04-18-2002, 08:14 AM