| chu121su12 (31) | |||
|
Sorry I'm late to this topic. But I found this thread is interesting. The things I can think of: @salman (pg.2)
Maybe because the default console width is 80 chars (windows). @Duoas (pg.3)
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. | |||
|
|
|||
| Bazzy (6281) | |||
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) | |||
|
|
|||
| chu121su12 (31) | |||
|
I'm still blur for the argc... In the code:
How would you expect argc is less than 2? | |||
|
|
|||
| Faldrax (324) | |
|
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. | |
|
|
|
| Duoas (6732) | |||||
|
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:
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 arraychar* 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:
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
|
|||||
| bugaboo (1) | |
|
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! | |
|
|
|
| Hazer (38) | |
|
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". | |
|
|
|
| chu121su12 (31) | |||
|
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?
| |||
|
|
|||
| Hazer (38) | |
|
I tested your example and it works. Result on linux: arguments: 0: /tmp/testfile | |
|
|
|
| Duoas (6732) | |
|
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. :-) | |
|
|
|
| chu121su12 (31) | |
|
hmm... Now I see.. Thanks! | |
|
|
|
| Timbo1 (91) | |
| Just start without debugging in the debugging menu | |
|
|
|
| fme (3) | |
|
use conio.h as follow: include<iostream.h> include<conio.h> int main(){ cout<<"Hello world"; return 0; } getch(); | |
|
|
|
| helios (10125) | |||
ಠ_ಠ
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? | |||
|
|
|||
| Duoas (6732) | |
| agreed | |
|
|
|
| firedraco (5413) | |
| *cough* Especially considering he returned 0 from main before he even got to the global scope (if that was even possible)... | |
|
|
|
| Duoas (6732) | |||
LOL, this works (but not in the way it reads):
X-) | |||
|
Last edited on
|
|||
| Tsuruya (5) | ||
I lol'd. | ||
|
|
||
| bluezor (298) | ||
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
| ||
|
|
||
| Bazzy (6281) | |
I think that int c = getch(); (as it is a declaration) is executed before main()
| |
|
|
|