Thread: String incorrect for output command target

  1. #1
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105

    String incorrect for output command target

    Ok, I'm back, and I have a new question. What am I doing wrong in this code?

    Code:
    int save()
    {
        char * filename;
        char * fileext = ".rpg";
        cout<<"Save game?\n";
        cin>>choice;
        if (choice=="yes" || choice=="y" || choice=="Y" || choice=="Yes" || choice=="yES" || choice=="YES" || choice=="YeS" || choice=="yEs" || choice=="YEs" || choice=="yeS")
        {
                          cout<<"Save filename?\n";
                          cin>>filename;
                          filename=filename+fileext;
                          std::ofstream output(filename);
                          output <<startpoint<<' '<<yourname<<' '<<hp<<' '<<weapon<<' '<<attack<<' '<<maxhp<<' '<<gold<<' '<<frags<<' '<<dmg<<' '<<block<<' '<<bow<<' '<<handgrenade<<' '<<broom<<' '<<rope<<' '<<grapple<<' '<<stairboots<<' '<<goldplate<<' '<<goldgoblet<<' '<<goldbowl<<' '<<goldflatware<<' '<<rockbuster<<' '<<bluessword<<' '<<smsand<<' '<<lgsand<<' '<<bacon<<' '<<rbeef<<' '<<bbqpork<<' '<<bbqbeef<<' '<<chicken<<' '<<steak<<' '<<apple<<' '<<pear<<' '<<plum<<' '<<pineapple<<' '<<peach<<' '<<papaya<<' '<<cheese<<' '<<candy<<' '<<beaten<<' '<<broomequip<<' '<<rockbusterequip<<' '<<bluesswordequip<<' '<<bowgive<<' '<<taken<<' '<<computer<<' '<<motherboard<<' '<<floppydrive<<' '<<harddisk<<' '<<compucase<<' '<<os<<' '<<rps<<' '<<calculate<<std::endl;
                          output.close();
                          cout<<"Game Saved.\n";
        }
        else
        {
        }
        system("pause");
    }
    It gives me an error about using the wrong data type. What am I doing wrong and how can I fix it?

  2. #2
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    Which line is giving you that error

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    If you want your input to be treated in a case-insensitive way, then do it. Checking to see if the input is equal to "yeS" is dumb. Nobody will ever type that. Seriously.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    Wait, i forgot that that was a modified version. For that code, the problematic line is:
    Code:
    filename=filename+fileext;
    The real form of the code is this, and the erroneous line is bold:

    Code:
    int save()
    {
        string filename;
        cout<<"Save game?\n";
        cin>>choice;
        if (choice=="yes" || choice=="y" || choice=="Y" || choice=="Yes" || choice=="yES" || choice=="YES" || choice=="YeS" || choice=="yEs" || choice=="YEs" || choice=="yeS")
        {
                          cout<<"Save filename?\n";
                          cin>>filename;
                          filename=filename+".rpg";
                          std::ofstream output(filename);
                          output <<startpoint<<' '<<yourname<<' '<<hp<<' '<<weapon<<' '<<attack<<' '<<maxhp<<' '<<gold<<' '<<frags<<' '<<dmg<<' '<<block<<' '<<bow<<' '<<handgrenade<<' '<<broom<<' '<<rope<<' '<<grapple<<' '<<stairboots<<' '<<goldplate<<' '<<goldgoblet<<' '<<goldbowl<<' '<<goldflatware<<' '<<rockbuster<<' '<<bluessword<<' '<<smsand<<' '<<lgsand<<' '<<bacon<<' '<<rbeef<<' '<<bbqpork<<' '<<bbqbeef<<' '<<chicken<<' '<<steak<<' '<<apple<<' '<<pear<<' '<<plum<<' '<<pineapple<<' '<<peach<<' '<<papaya<<' '<<cheese<<' '<<candy<<' '<<beaten<<' '<<broomequip<<' '<<rockbusterequip<<' '<<bluesswordequip<<' '<<bowgive<<' '<<taken<<' '<<computer<<' '<<motherboard<<' '<<floppydrive<<' '<<harddisk<<' '<<compucase<<' '<<os<<' '<<rps<<' '<<calculate<<std::endl;
                          output.close();
                          cout<<"Game Saved.\n";
        }
        else
        {
        }
        system("pause");
    }

  5. #5
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    ofstream's constructor can take a char * (a C string), not a C++ string

    Code:
    std::ofstream output(filename.c_str);

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Kernel Sanders View Post
    Code:
    std::ofstream output(filename.c_str);
    Or more correctly:
    Code:
    std::ofstream output(filename.c_str());
    I would ask the "Save ?" this way:
    Code:
    do {
        cout<<"Save game?\n";
        cin>>choice;
    } while (toupper(choice[0]) != 'Y' && toupper(choice[0]) != 'N');
    if (toupper(choice[0] == 'Y')
       ... do saving.
    Now that will of course accept "Yeah", "Yo" or "yn" as "Yes", but I think that's acceptable. If you do not want to do that, then you probably should write a proper parser that takes a list of valid "commands" for a particular situation, and only returns when you get a valid one.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    That helps, so I'll upgrade my games to do that. But how do I convert the string to a char? Or, in the first code I posted, append the extension to the filename?

  8. #8
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    Append with the += operator and convert it to a C string char * with the c_str() function

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Quote Originally Posted by brewbuck View Post
    As soon as you've formed an opinion, you're already wrong
    Holy crap, how did my wife get a Sr. SWE job???
    Last edited by rags_to_riches; 08-19-2008 at 03:43 PM. Reason: Fixed quote

  10. #10
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    Off topic, man...

    What is the syntax for the c_str() function?

  11. #11
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    Quote Originally Posted by DarkAlex View Post
    Off topic, man...

    What is the syntax for the c_str() function?
    Code:
    string s("whatever");
    char *cs = s.c_str();
    Google is your friend

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Code:
    char *cs = s.c_str();
    should of course be:
    Code:
    const char *cs = s.c_str();
    although you usually just pass it directly to the fstream function (or whatever legacy code requires the C style string).

  13. #13
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    Quote Originally Posted by Daved View Post
    Code:
    char *cs = s.c_str();
    should of course be:
    Code:
    const char *cs = s.c_str();
    although you usually just pass it directly to the fstream function (or whatever legacy code requires the C style string).
    Off topic - can you recommend a good reference for C++'s const keyword? As a C programmer learning C++ const is one of my stumbling blocks

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Maybe this: http://www.parashift.com/c++-faq-lit...rrectness.html

    Otherwise, just the recommended books.

    You might also try searching gotw and/or comp.lang.c++.moderated, although it might be difficult given that const can be used in many scenarios without actually explaining it.

  15. #15
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    It keeps giving me errors...

    Code:
    int save()
    {
        string filenameo;
        char * filename;
        do
        {
           cout<<"Save game?\n";
           cin>>choice;
        } while (toupper(choice[0]) != 'Y' && toupper(choice[0]) != 'N');
        if (toupper(choice[0] == 'Y'))
        {
                          cout<<"Save filename?\n";
                          cin>>filenameo;
                          filename=filenameo.c_str();
                          filename=filename+="rpg";
                          std::ofstream output(filename);
                          output <<startpoint<<' '<<yourname<<' '<<hp<<' '<<weapon<<' '<<attack<<' '<<maxhp<<' '<<gold<<' '<<frags<<' '<<dmg<<' '<<block<<' '<<bow<<' '<<handgrenade<<' '<<broom<<' '<<rope<<' '<<grapple<<' '<<stairboots<<' '<<goldplate<<' '<<goldgoblet<<' '<<goldbowl<<' '<<goldflatware<<' '<<rockbuster<<' '<<bluessword<<' '<<smsand<<' '<<lgsand<<' '<<bacon<<' '<<rbeef<<' '<<bbqpork<<' '<<bbqbeef<<' '<<chicken<<' '<<steak<<' '<<apple<<' '<<pear<<' '<<plum<<' '<<pineapple<<' '<<peach<<' '<<papaya<<' '<<cheese<<' '<<candy<<' '<<beaten<<' '<<broomequip<<' '<<rockbusterequip<<' '<<bluesswordequip<<' '<<bowgive<<' '<<taken<<' '<<computer<<' '<<motherboard<<' '<<floppydrive<<' '<<harddisk<<' '<<compucase<<' '<<os<<' '<<rps<<' '<<calculate<<std::endl;
                          output.close();
                          cout<<"Game Saved.\n";
        }
        else
        {
        }
        system("pause");
    }
    Erroneous lines are bold, and errors are:

    In function `int save()':
    508 invalid conversion from `const char*' to `char*'
    509 invalid operands of types `char*' and `const char[4]' to binary `operator+'
    509 in evaluation of `operator+=(char*, const char[4])'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM