How to compare the value of string command = "%USERNAME%"

This is a discussion on How to compare the value of string command = "%USERNAME%" within the C++ Programming forums, part of the General Programming Boards category; Ok this is what I have. string command = "%USERNAME%"; system(command.c_str()); that will display the current user logged on. Now ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    26

    How to compare the value of string command = "%USERNAME%"

    Ok this is what I have.

    string command = "%USERNAME%";
    system(command.c_str());

    that will display the current user logged on.

    Now is where I need help.
    How can I check the value of string command = "%USERNAME%";

    In english this is what I would Like to do

    Code:
    if ( string command ="%USERNAME%" = Administrator) {
                        cout << "administrator is logged on" << endl;
                       }
                           else {
                       cout << "Someone else is logged on" << endl;
    }
    of course this isnt going to work, but what do I have to do to compare the output of string command = "%USERNAME%";

    I hope you get what I mean, thanks!
    I think what I have to do is convert that to a value of another string then compare, but I dont know how.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    string command = "%USERNAME%";
    
    if(command == "Administrator")
    
    {
    
       cout << "administrator is logged on" << endl;
    
    }
    else
    
    {
      cout << "Someone else is logged on" << endl;
    }


    You mean something simple like that? Or do you have something else in mind?
    You're only born perfect.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    26
    lol, yeah, that was simple, I thought I did that, anyways thanks, everytime I learn somthing, even that basic, i'll remember it for next time.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by GUIPenguin
    lol, yeah, that was simple, I thought I did that, anyways thanks, everytime I learn somthing, even that basic, i'll remember it for next time.

    Well, if you set the value of a string equal to "%USERNAME%", it's equal to "%USERNAME%".

    If you want the name of the environment variable USERNAME, you can use getenv():

    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    
    int main()
    {
    
      std::string command = "%USERNAME%";
      std::cout << "command:   <" << command << ">" << std::endl;
    
      char *c_command;
      c_command = getenv("USERNAME");
      if (c_command) {
        std::cout << "c_command: <" << c_command << ">" << std::endl;
      }
      else {
        std::cout << "No environment variable named <" << "USERNAME" << ">" << std::endl;
      }
      return 0;
    }
    Of course, to test the value of c_command, you can use strcmp(). (Or you can set a c++ string value equal to c_command and then use == for comparison.)

    Regards,

    Dave
    Last edited by Dave Evans; 02-04-2005 at 04:37 PM.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    26
    I still cant get this too work, no matter what it ALWAYS goes to the else statment even if (command == "Administrator"){

    I also tryed the secound one and went if (c_command == "Administrator"){


    and that will every time go to else


    if I open up a dos prompt and type %USERNAME% I get administrator, but I would like to compare this in C++, maybe you thought I wanted somthing different, thanks for the help

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Well post your latest code then. "It doesn't work" doesn't help since we've no idea what it is you've actually tried.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    26
    Well I have tryed this way

    Code:
    string command = "%USERNAME%";
    
    if(command == "Administrator")
    
    {
    
       cout << "administrator is logged on" << endl;
    
    }
    else
    
    {
      cout << "Someone else is logged on" << endl;
    }
    just like the secound answer, but each time it will go to the else statement even if command == "Administrator" is ture

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    So you're not calling getenv() as per previous suggestions then?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    26
    Thanks I got It figured out

    using getenv() Before I had gone
    Code:
    if (c_command == "Administrator" {
    command }
    else {
    command
    }
    but that didnt work so I uses getenv() and checked with strcmp

    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    using namespace std;
    int main()
    {
    
      string command = "%USERNAME%";
    
      char *c_command;
      c_command = getenv("USERNAME");
      
      system("CLS");
      
      if ( strcmp ( c_command, "Administrator" ) == 0 ) {
          
          cout << "Administrator is logged on!" << endl; }
          
          else {
              
              cout << "Someone else is logged on!" << endl;
          }    
     cin.get();
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  3. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 06:13 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 02:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21