Help, please.

This is a discussion on Help, please. within the C++ Programming forums, part of the General Programming Boards category; Here's my code, it's kindof self-explanatory. I am getting errors and I believe its how I am trying to make ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    241

    Help, please.

    Here's my code, it's kindof self-explanatory. I am getting errors and I believe its how I am trying to make the command a string, I am not that good when it comes to strings.
    Code:
    #include <iostream>
    #include <windows.h>
    #include <stdio.h>
    #include <string.h>
    #define msg "C:\\windows\\system32\\msg.exe";
    
    using namespace std;
    
    int main()
    {
        char Message[90];
        int Who;
        char Where[90];
        string command[0];
    
        cout<<"Please enter your message:";
        cin>>Message; 
        cout<<"Please enter who:";
        cin>>Who;
        cout<<"Please enter where:";
        cin>>Where;
        if (Who==0)
        {
            command=(msg Where Message);
            system(command);
            return 1;
        }
        else
        {
            command=(msg Who Message);
            system(command);
            return 1;
        }
        cin.get();
        return 1;
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    string command[0];
    That declares a 'string' type. string types require that you include the header file <string>. With string types you don't declare them as arrays, you declare them like this:
    Code:
    string name;
    name = "Bobby";
    string types are much easier to use than cstyle strings(null terminated char arrays). This is how you add strings:
    Code:
    string first = "Bobby";
    string last = "Smith";
    
    string fullname = first + " " + last;
    cout<<fullname<<endl;
    
    char a[] = "hello ";
    string greeting = a + fullname;
    cout<<greeting<<endl;
    Note how you don't have to call functions like strcpy() or strcat() and worry about whether the string properly ends in a '\0' or not--like you have to do with c-style strings.
    Last edited by 7stud; 12-05-2005 at 06:55 AM.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Code:
    command=(msg Where Message);
    ->
    Code:
    strcpy(command, Where);
    strcat(command, Message);
    Or, if you change the variables involved to strings:
    Code:
    command = Where + Message;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Thank you

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    This is what I have
    Code:
    #include <iostream>
    #include <windows.h>
    #include <cstring>
    #define msg "C:\\windows\\system32\\msg.exe";
    
    using namespace std;
    
    int main()
    {
        string Message;
        int Who;
        string Where;
        string command;
    
        cout<<"Please enter your message:";
        cin>>Message; 
        cout<<"Please enter who:";
        cin>>Who;
        cout<<"Please enter where:";
        cin>>Where;
        if (Who==0)
        {
            command=Where+Message;
            system("msg ", command);
            return 1;
        }
        else
        {
            command=Who+Message;
            system("msg ", command);
            return 1;
        }
        cin.get();
        return 1;
    }
    The thing is, now it's opening up stdlib.h and saying theres "too many arguments" this sucks, any help?

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Yeah.

    Code:
    system("msg ", command); // system only takes one argument
    If you wanted to say message and the command string, then you'd have to concatenate the command string to the end of message and put that in a string.

    Also why are you returning a non-zero value in main?

    ...and a little note. Do you know what's a bad idea? Letting your user choose what goes in the system command.
    Last edited by SlyMaelstrom; 12-05-2005 at 05:40 PM.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Yeah, I know it's a bad idea, but only I'm using it so yeah... anyways how do I include spaces in strings
    EDIT: oh yeah, I always mix up the one and zero values for return :-p

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    What do you mean include spaces? Do you mean read in to include spaces or do you mean just adding spaces between concatinations.

    To read in with spaces, use getline()
    Code:
    getline(cin, string1, '\n');  \\ getline(*file, string, const char*)
    To just add a space between your concatination
    Code:
    string foo = "Hello";
    string foo2 = "World!";
    foo = foo + " " + foo2;
    Sent from my iPadŽ

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Ok, so I was able to make my code shorter. Now it's this
    Code:
    #include <iostream>
    #include <windows.h>
    int main()
    {
        std::string Message;
        std::string Recipiant;
        std::string command;
        std::cout<<"Please enter your message:";
        std::cin>>Message; 
        std::cout<<"Please enter the recipiant:";
        std::cin>>Recipiant;
        command="msg.exe"+" "+Recipiant+" "+Message;
        system(command);
        std::cin.get();
        return 0;
    }
    But I am getting errors because of the +
    Last edited by bikr692002; 12-05-2005 at 08:06 PM.

  10. #10
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    That is because you are concatinate two string literals, which is not neccessary here. Note that you do not need "windows.h". You do need <string> when you work with strings though.

    system takes a const char* as input, so you need to call the string function that returns such a pointer.

    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::string Message;
        std::string Recipient;
        std::string Command;
    
        std::cout << "Please enter your message:";
        std::cin >> Message; 
    
        std::cout << "Please enter the recipient:";
        std::cin >> Recipient;
    
        Command = "msg.exe " + Recipient + " " + Message;
    
        system( Command.c_str() );
    
        std::cin.get();
    
        return 0;
    }
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  11. #11
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Quote Originally Posted by nvoigt
    That is because you are concatinate two string literals, which is not neccessary here. Note that you do not need "windows.h". You do need <string> when you work with strings though.

    system takes a const char* as input, so you need to call the string function that returns such a pointer.

    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::string Message;
        std::string Recipient;
        std::string Command;
    
        std::cout << "Please enter your message:";
        std::cin >> Message; 
    
        std::cout << "Please enter the recipient:";
        std::cin >> Recipient;
    
        Command = "msg.exe " + Recipient + " " + Message;
    
        system( Command.c_str() );
    
        std::cin.get();
    
        return 0;
    }
    wouldnt I need windows.h for system()?

  12. #12
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    I don't know about <windows.h>, but system() is in <cstdlib>.
    There is a difference between tedious and difficult.

  13. #13
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    It's also worth mentioning that <cstdlib> is declared in <iostream>.
    Last edited by SlyMaelstrom; 12-06-2005 at 09:30 AM.
    Sent from my iPadŽ

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> It's also worth mentioning that <cstdlib> is declared in <iostream>.
    Not necessarily. Just because your compiler does it doesn't mean everybody's does. Specifically include each header that you need to include, don't rely on compiler specific header relationships.

  15. #15
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Yeah, that's a good point. Though I thought iostream was dependant on including cstdlib.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

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