Console Closing Down

Pages: 1... 4567
Sorry I'm late to this topic. But I found this thread is interesting.
The things I can think of:

@salman (pg.2)
What makes them think of putting 80?

Maybe because the default console width is 80 chars (windows).

@Duoas (pg.3)
your post with a long code

What are the use of int argc, char **argv (and maybe the others) ?
I've seen them in some codes unused except in your code.

Currently, I'm using function for pause. I'll upgrade to class ASAP.
For Duoas, thank you very much for your explanations. I learned so much.

 
int main(int argc, char *argv[]);


argc is the nubmer of arguments that the application got when started,
*argv[] is an array of C-style string whict contains the arguments
(eg: when launching a program from a file argc[0] is the program path and argc[1] is the file path)
I'm still blur for the argc...

In the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <algorithm>
#include <string>
#include <windows.h>
using namespace std;

int main( int argc, char **argv )
  {
  TCHAR*              exepath;
  TCHAR*              cmdline;
  STARTUPINFO         startup_info;
  PROCESS_INFORMATION process_info;

  if (argc < 2) return 1;
  //... 

How would you expect argc is less than 2?
The check is there because argc should not be less than 2.
If it is, then the program regards that as an error, and exits with an error code if 1.
This is why main() is of type int. The return value can be detected by the OS, and typically 0 is OK, non-zero is an error of some sort (what non-zero error code mean is up to the programmer).
If you do not have a return in main, the compiler will automatically add a 'return 0' at then end.
Wow, miss a few weeks...

Umz
I don't know if you noticed what caused the problem, but cin.ignore(...) only works if your input stream is completely read and waiting for the user to type something. After using cin >> foo; that is not the case. Try thecoolguy98's method and see if that helps:
1
2
3
4
5
6
void pause()
  {
  cout << "Press ENTER to continue...";
  cin.sync();
  cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
  }


chu121su12
Don't worry so much about all the TCHAR and PROCESS_INFORMATION and the like --it's all Windows boiler-plate.

The main() functions arguments are
int argc The number of c-strings in the argv array
char* argv[] Each piece of the command-line that invoked your program.

The first item (index 0) is the name of your program.
The second item (index 1) is the first argument to your program.
Etc.

Try playing around with it:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

int main( int argc, char* argv[] )
  {
  cout << "arguments:\n";
  for (int cntr = 0; cntr < argc; cntr++)
    cout << cntr << ": " << argv[ cntr ] << '\n';
  return 0;
  }

The program name is also considered an 'argument' because it is part of the command used to invoke your program. Try running your program from different directories to see how it affects argv[0].

D:\prog\cc\foo\args> a.exe one two three
arguments:
0: a.exe
1: one
2: two
3: three

D:\prog\cc\foo\args> a Hello world!
arguments:
0: a
1: Hello
2: world!

D:\prog\cc\foo\args> cd ..

D:\prog\cc\foo> args\a "John Jacob Jingleheimer Schmidt"
arguments:
0: args\a
1: John Jacob Jingleheimer Schmidt

When you are done playing with the command-line, try double-clicking the executable from Explorer (or from the window manager if you are using Linux or Mac).
[edit] Oh, you'll have to add that pause function and use pause(); before return 0; before you try it from your WM. heh.[/edit]

Hope this helps.

[edit] Added "using namespace std" to the example. (Sorry I forgot that last time.) [/edit]
Last edited on
Hi!
I'm a C++ programming student, and I read through parts of this thread (if that bothers some people... I'm sorry)
I'm writing a program that ends by prompting the user to press any key, circumventing the normal “Press any key to continue…” prompt. So it doesn't have to wait for the newline - any key can terminate it.
Can anyone help me?
Thank you so much!
Answer is in pg.5.
And please end this discusion because it is about "how to keep console runing", not about "argc[]" and "how to display a string".
Sorry Hazer, but I'd want to ask one more oot questions.

Duoas, I understand (*very) your example. But I want to make sure the result in linux, since I'm not anywhere inside linux-user territory.

What will be the result of this code in linux?
1
2
3
4
5
6
7
8
9
#include <iostream>

int main( int argc, char* argv[] )
  {
  cout << "arguments:\n";
  for (int cntr = 0; cntr < argc; cntr++)
    cout << cntr << ": " << argv[ cntr ] << '\n';
  return 0;
  }
I tested your example and it works. Result on linux:
arguments: 0: /tmp/testfile
I presented the example being used on Windows because that is one of the assumptions behind this particular thread, but the code will work identically on Linux, OS X, Plan 9, whatever. (If it doesn't then your compiler is broken.)

(I also fixed an accidental omission above: namespace using std for cout.)

Don't forget the newlines\n.

:-)
hmm... Now I see..
Thanks!
Just start without debugging in the debugging menu
use conio.h as follow:
include<iostream.h>
include<conio.h>
int main(){
cout<<"Hello world";
return 0;
}
getch();
ಠ_ಠ
1
2
3
}
//(global scope)
getch();

I think I developed three new clots in my brain just by looking at that.
I'm thinking this thread is already long enough, and it's pretty much covered all the possible solutions to this problem. Including the half-assed ones and the ones that just don't work. Maybe it'd be a good idea to lock it, already?
agreed
*cough* Especially considering he returned 0 from main before he even got to the global scope (if that was even possible)...
LOL, this works (but not in the way it reads):

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <conio.h>
using namespace std;

int main()
  {
  cout << "Hello world";
  return 0;
  }

int c = getch();

X-)
Last edited on
include<iostream.h>


I lol'd.
#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
cout << "Hello world";
return 0;
}

int c = getch();


Hello Duoas,

I have tried that and it worked. But the problem was, it didn't display Hello World. I'm wondering though, how did that work? c wasn't mentioned anywhere in the code at all. :O
I think that int c = getch(); (as it is a declaration) is executed before main()
Pages: 1... 4567