Point me in the right direction

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • If I remember right, getch() is defined in conio.h, which is a nonstandard C/C++ header file (although if you looked at the actual implementation of getch(), it uses standard C/C++ code). I can't remember if conio.h was included with the Dev C++ package, but assuming it is, to use getch(), you would just add "#include <conio.h>" after the line that says "#include <iostream.h>" and before the line that says "return 0;" you would add "getch();".

      If that doesn't work, another way you could do it is by adding the line "#include <stdlib.h>" after "#include <iostream.h>" and add "system("pause");" before "return 0;". system() executes a command that would normally be executed at the command prompt/console. If you're working on a Windows system, that should give you a little message that says "Press any key to continue.", which will give you a chance to view your output without having the DOS window disappear too soon. Just note that the argument you give to system() ("pause" in this case) is dependent upon what OS you're running the program on, so if you compiled it under Linux and tried to run it, it wouldn't work the same way.

      Hope that helps.
    • ok this is what i got... im getting 4 errors now im using the second method you told me

      #include <iostream.h>
      #include <stdlib.h>
      main()
      {
      cout << "Hello World!";
      system(pause);
      return 0;
      }

      see anything wrong?

      thanks
      C4l3B
    • Oh, you'll get some real doozies when you're off doing your own projects. I especially love when you forget to initialize a class variable, and the program doesn't crash until it's no where near where you should look. Those are fun.

      Seriously, though, I love debugging, and if you post any code that's giving you trouble, we'll help out.

      Just make sure you learn pointers thoroughly.
      -Larrik Jaerico

      www.LarrikJ.com
    • I don't mean to hijack your thread, but while we're talking about pointers and other related issues, I was wondering if anyone has come across a free or relatively cheap programming tool that's comparable to Electric Fence. Electric Fence is basically a C library that replaces the standard malloc/calloc/realloc calls, but the version put out by the author is for Unix-like systems only. Anytime you try to access memory that is not within your allocated block of memory, Electric Fence will cause a segmentation fault on the exact line where the memory access occurred in your program. When it seg faults under a debugger like GDB, you can easily step up to the point in your source file where the seg fault happened, then do what you normally do in a debugger like examining the contents of variables and what not. I use it everyday at where I work when I develop new software, and it has been by the far the single most effective tool in finding and eliminating buffer overruns and underruns (two of the most hard to find errors in C/C++ programming, but arguably the ones that cause the most headaches). I was just wondering if anyone has come across a comparable tool that will work under Windows.
    • Hey. Actually what you will want to do is change your main to something like:

      main() {
      cout << "Hello World!";
      system( "pause" );
      return 0;
      }

      That will force it to wait for you to hit a key before exiting.

      The system() function is very useful for CLI C++ programing. Another useful one is:

      system( "cls" );

      which will clear the screen of all text. By adding the system( "pause" ) to your program you should be able to run it by double clicking and be good to go.

      Jonmark
    • Ok im working on this temperature conversion

      #include <iostream>

      using namespace std;

      int main()
      {
      cout << "Temperature Conversion Program." << endl;
      cout << "Enter degrees Celsius from 0 to 100: " << flush;

      double degreesCelsius;
      cin >> degreesCelsius;

      double degreesFahrenheit = degreesCelsius*(9.0/5.0)+32;

      cout << degreesCelsius << " degrees Celsius = ";
      cout << degreesFahrenheit << " degrees Fahrenheit" << endl;
      }

      it works then when i go to enter the number between 0 to 100 i press enter and the dos screen goes away any suggestions? thanks
      C4l3B
    • Put on its own line just before the }
      (ie, it's the last command the program executes).

      That's a windows thing. A more portable solution would be something like "gets(new char[100]);" which CAN crash, but at that point, who cares?
      -Larrik Jaerico

      www.LarrikJ.com
    • Hey man im really sry to keep bugging yall but it still doesnt work im picking up my books tomarow hopefully ill get it straightend out thanks a bunch Larrik for keeping in on this thread. Heres what i have i think its in the right order... its giveing me the error:

      17 untitled1.cpp
      implicit declaration of function `int system(...)'

      And heres what i have typed in

      #include <iostream>

      using namespace std;

      int main()
      {
      cout << "Temperature Conversion Program." << endl;
      cout << "Enter degrees Celsius from 0 to 100: " << flush;

      double degreesCelsius;
      cin >> degreesCelsius;

      double degreesFahrenheit = degreesCelsius*(9.0/5.0)+32;

      cout << degreesCelsius << " degrees Celsius = ";
      cout << degreesFahrenheit << " degrees Fahrenheit" << endl;
      system("pause");
      }


      the << endl; is on the same line as one above it

      thanks a bunch :þ
      C4l3B
    • Sorry, put

      #include <stdlib.h>

      At the beginning of the file (with the other #include, but on its own line).

      And it turns out the system("pause"); is indeed ANSI C (ie., not Windows-specific), but it's results may be different from one OS to the next.
      -Larrik Jaerico

      www.LarrikJ.com
    • Hey boss - I hope some of your books are:

      The C++ Programming Language by Bjarne Stroustrup
      The C++ Standard Library by Nicolai Josuttis
      Practical C++ by Rob McGregor
      C/C++ Programmer's Reference by Herbert Schildt

      All great references in my opinion. If you don't have 'em I suggest you get them.

      Your last problem was simply not knowing where all of these functions are defined - that's what the references are for. There are include files for all sorts of stuff - tons of code that's already written for you to use. You just have to know where to look.

      Have fun!

      Richard
      "Your job is not to die for your country. Your job is to make some other poor sod die for his."