Computer Science Canada

help with beginning code

Author:  deathscith [ Fri Oct 17, 2008 7:11 pm ]
Post subject:  help with beginning code

code:

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


should probably of stated more in the subject name, but it isnt really help needed for the hello world code, i use the compiler MinGW, and everytime i compile this program, and i've done this with devC++, the problem is that when i compile it, it creates an exe oviously, and when i open the exe it opens it in cmd promt, but it closes instantly... how do i get it to stay open?

Author:  deathscith [ Fri Oct 17, 2008 7:25 pm ]
Post subject:  Re: help with beginning code

alright, well could anyone help me with my quick solution? make it so it awaits a command to exit?
kinda like for turing (only language i know much of)

code:

var base : string := ""
var exitstate :string
loop
put "hello world"
get exitstate
if exitstate = base then
put ""
else
exit
end loop

Author:  wtd [ Fri Oct 17, 2008 7:28 pm ]
Post subject:  RE:help with hello world

This is a common issue.

http://wiki.compsci.ca/index.php?title=Window_closes_instantly

Author:  [Gandalf] [ Fri Oct 17, 2008 7:43 pm ]
Post subject:  RE:help with beginning code

Open it in the command line, cmd.exe on Windows. Simple C/C++ programs aren't meant to be run in they way you are attempting to run them.

Also, don't post "bump" 2 minutes after you post your help topic.

*edit* No refresh, wtd wins.

Author:  deathscith [ Fri Oct 17, 2008 8:24 pm ]
Post subject:  Re: help with beginning code

i'll remember that gandalf, and as for my problem, the link you said gave me this as my desired outcome
Quote:

This is the poorer choice, and only masks the problem. Add code to your program that prompts the user for input. The program will not complete until the user actually does input something, meaning that in the meantime the console window remains open.


but how do i do that?

Author:  wtd [ Fri Oct 17, 2008 8:28 pm ]
Post subject:  RE:help with beginning code

Good question. You have something new to learn.

Author:  deathscith [ Fri Oct 17, 2008 8:30 pm ]
Post subject:  Re: help with beginning code

where would i go about as to learning this? i've looked through a few of the beginners tuts and they never give this info...

Author:  Tony [ Fri Oct 17, 2008 8:37 pm ]
Post subject:  Re: help with beginning code

I just want to point out that...
deathscith @ Fri Oct 17, 2008 8:24 pm wrote:
the link you said gave me this as my desired outcome
Quote:

This is the poorer choice...

Author:  deathscith [ Fri Oct 17, 2008 9:27 pm ]
Post subject:  Re: help with beginning code

Tony @ Fri Oct 17, 2008 8:37 pm wrote:
I just want to point out that...
deathscith @ Fri Oct 17, 2008 8:24 pm wrote:
the link you said gave me this as my desired outcome
Quote:

This is the poorer choice...

yea, but its what i want it to do.

Author:  Ktomislav [ Sat Oct 18, 2008 8:24 am ]
Post subject:  Re: help with beginning code

code:

#include <iostream.h>
using namespace std;
main(void)
{
    int a;
    cout << "Hello World!";
    cin >>a;
    return 0;
}


That's the way i do.

Author:  michaelp [ Sat Oct 18, 2008 9:36 am ]
Post subject:  RE:help with beginning code

Also, it should be:

code:
#include <iostream>


not

code:
#include <iostream.h>

Author:  wtd [ Sat Oct 18, 2008 11:37 am ]
Post subject:  RE:help with beginning code

Congrats on just feeding deathscith an easy answer.

Author:  Ktomislav [ Sat Oct 18, 2008 4:20 pm ]
Post subject:  Re: RE:help with beginning code

michaelp @ 18.10.2008, 15:36 wrote:
Also, it should be:

code:
#include <iostream>


not

code:
#include <iostream.h>


Yes. I copied that code from the post above and i didn't check it if it is correct.

Author:  Rigby5 [ Fri Oct 24, 2008 5:48 pm ]
Post subject:  RE:help with beginning code

cin >> a
will wait until you hit the <ret> key.
If instead you use
int c = getch();
They any single key at all will work to end it.

Author:  md [ Sat Oct 25, 2008 12:38 am ]
Post subject:  RE:help with beginning code

cout and cin are not in the standard name space, problem number one.

waiting for needless input at the end of your program is such a bad practice that anyone who recommends it should be shot. Or failing that banned from programming. Ever.

Instead you should run your program from a command line, like command line programs are mean't to be run. Compilers are command like programs too! So if you compile your code using a compiler on the command line you already have a ready to use window to run your just created executable in! Plus using the command line will get you much more familiar with the compiler and many other different parts of your system which are really quite handy.


Rigby5, getch is a windows only function. Unless you include some additional libraries which are well beyond a hello world program, getch will not exist.

Author:  btiffin [ Sat Oct 25, 2008 2:04 pm ]
Post subject:  Re: RE:help with beginning code

md @ Sat Oct 25, 2008 12:38 am wrote:

waiting for needless input at the end of your program is such a bad practice that anyone who recommends it should be shot. Or failing that banned from programming. Ever.

hear, all ye good people, hear what this brilliant and eloquent speaker has to say!

Author:  wtd [ Sat Oct 25, 2008 10:33 pm ]
Post subject:  RE:help with beginning code

Would a frontal lobotomy be acceptable?

Author:  Rigby5 [ Mon Oct 27, 2008 12:11 am ]
Post subject:  RE:help with beginning code

The getch() function is in curses.h, so it available to Linux programmers as well.
But there is also getchar() in C++.
The point is that you often need to have data on individual key presses, not just after the <return> key is entered.

And this problem of disappearing console windows can also come up when you run a batch script from the command line.

Author:  wtd [ Mon Oct 27, 2008 1:39 am ]
Post subject:  RE:help with beginning code

Care to provide evidence of well-written command-line applications that can close the terminal window they're being run in?

Author:  md [ Mon Oct 27, 2008 4:30 pm ]
Post subject:  Re: RE:help with beginning code

Rigby5 @ 2008-10-27, 12:11 am wrote:
The getch() function is in curses.h, so it available to Linux programmers as well.
But there is also getchar() in C++.
The point is that you often need to have data on individual key presses, not just after the <return> key is entered.

And this problem of disappearing console windows can also come up when you run a batch script from the command line.


curses.h is not a standard library, but part of (usually) the ncurses library. Likewise getchar() is part of cstdlib, again not standard C++. While knowing keystrokes can be helpful in some (limited) cases, it's actually quite easy to get single characters using std::cin.get(). However, knowing key presses is usually not needed and where it is you are usually better off using standard libraries to handle it for you (ncurses is actually the defacto standard).

Author:  btiffin [ Mon Oct 27, 2008 4:48 pm ]
Post subject:  Re: RE:help with beginning code

wtd @ Mon Oct 27, 2008 1:39 am wrote:
Care to provide evidence of well-written command-line applications that can close the terminal window they're being run in?
Rigby5 wrote:

...
And this problem of disappearing console windows can also come up when you run a batch script from the command line.

wtd; I'm not in Windows right now, so I can't test this; but I'm assuming Rigby5 has experienced windows batch files (be they WSHell, or vb or js or .bat or what have) that include
code:
cmd.exe /c
versus
code:
cmd.exe /k
Maybe?

Note; I'm ignoring the well-written part of your post. Not to bash MS or anything, but are there any well-written Windows command-line applications? Seeing as the Windows command console is not well-written (imho - it's junk and costs time and effort, further holding back humanity's quest for a productive computing experience).

Cheers

Author:  Rigby5 [ Tue Oct 28, 2008 12:35 am ]
Post subject:  RE:help with beginning code

Unlike Linux, Windows does not really have a console.
With Linux you have stdin, stdout, and stderr as 0, 1, and 2.
But with windows, each application gets a consecutive number, so a batch run from the command line, gets 3, 4, and 5, etc.
Depending on whether you call, execute, etc., you can open and additional cmd line window or not.
It just is not at all the same as a Linux console, and you might as well not think of Windows as even having a cmd line really. It is just a text based window.


: