Toon posts:

[c++] Command line arguments

Pagina: 1
Acties:
  • 41 views sinds 30-01-2008

Verwijderd

Topicstarter
#include <iostream.h>
#include <fstream.h>

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

if(argv[1] == "-u")
{
cout<<"Het werkt.\n";
}

}

De bedoeling van het bovenstaande script is dat er als het eerste command line argument -u is er een actie plaatsvind. Het probleem is echter dat hij altijd, dus ook als er geen argument is, Het werkt weergeeft. Weet iemand hoe dit opgelost kan worden???

  • whoami
  • Registratie: December 2000
  • Laatst online: 15:14
strings (char arrays) vergelijken doe je niet met == , maar met strcmp.
Het eerste element in een arrray zit niet op positie 1, maar op positie 0
Check ook eerst of er wel argumenten zijn (met argc).

https://fgheysels.github.io/


  • _the_crow_
  • Registratie: September 2000
  • Laatst online: 30-03-2025

_the_crow_

Rare vogel

Het eerste argument heeft index 0. :)
Janoz schreef op 18 december 2003 @ 14:13:
hmm .. afaik stond op positie 0 de naam van de executable.
Inderdaad |:(
Net ff gecheckt.

[ Voor 73% gewijzigd door _the_crow_ op 18-12-2003 14:15 ]

Schrödingers cat: In this case there are three determinate states the cat could be in: these being Alive, Dead, and Bloody Furious.


  • Janoz
  • Registratie: Oktober 2000
  • Laatst online: 15:26

Janoz

Moderator Devschuur®

!litemod

hmm .. afaik stond op positie 0 de naam van de executable.

Ken Thompson's famous line from V6 UNIX is equaly applicable to this post:
'You are not expected to understand this'


  • whoami
  • Registratie: December 2000
  • Laatst online: 15:14
Janoz schreef op 18 december 2003 @ 14:13:
hmm .. afaik stond op positie 0 de naam van de executable.
Yep, da's waar. Was ik vergeten. :+

Maar goed, strings vergelijken doe je toch met strcmp, en je checkt ook best wel of er wel iets is op argv[1].

https://fgheysels.github.io/


  • curry684
  • Registratie: Juni 2000
  • Laatst online: 12-05 22:23

curry684

left part of the evil twins

C++:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream.h>
#include <fstream.h>
#include <string.h>

int main(int argc, char * argv[])
{
if(argc >= 2 && strcmp(argv[1], "-u") == 0)
  {
  cout<<"Ik moet een boek kopen of een tutorial lezen voor de basisbeginselen van C/C++.\n";
  }
}

Professionele website nodig?


  • drm
  • Registratie: Februari 2001
  • Laatst online: 09-06-2025

drm

f0pc0dert

C:
1
2
3
if ( argc > 1 && strcmp ( argv[1], "-u" ) == 0 ) {
   /* woei! */
}


Checken of je genoeg argumenten hebt doe je dus met argc (argument count).
edit:
:w curry O+

[ Voor 9% gewijzigd door drm op 18-12-2003 14:23 ]

Music is the pleasure the human mind experiences from counting without being aware that it is counting
~ Gottfried Leibniz


  • curry684
  • Registratie: Juni 2000
  • Laatst online: 12-05 22:23

curry684

left part of the evil twins

drm is spuit elf :)

Professionele website nodig?


  • .oisyn
  • Registratie: September 2000
  • Laatst online: 15:32

.oisyn

Moderator Devschuur®

Demotivational Speaker

enorm laat :P

C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <vector>
#include <string>
#include <algorithm>

int main (int argc, char ** argv)
{
    std::vector<std::string> args (argv, argv + argc);

    if (args[1] == "-u")
    {
        // blaat
    }

    // of:
    if (std::find (args.begin (), args.end (), "-u") != args.end ())
    {
        // blaat
    }
}

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.

Pagina: 1

Dit topic is gesloten.