Thread: Input in C++

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    Now I'm trying to write a save file, where part of the filename is a variable that you type in(your character's name), so each character generates its own unique save file. Here's what I have so far:
    Code:
    cout<<"Who are you? ";
             cin>>input;
             ofstream a_file (".sav");
             a_file<<"<Save game info for this character...>";
             a_file.close();
    I need a way to add the value of input before the ".sav" in the filename, is there a simple way to do this?

  2. #17
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Assuming input is a string object, it's trivial:
    Code:
    ofstream a_file (input + ".sav");
    My best code is written with the delete key.

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    Hrm, when trying that, I get this error:

    60 E:\Dev-Cpp\My Crap\New Adventure RPG\Adventure RPG.cpp no matching function for call to `std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'

  4. #19
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Heheh, silly me. I keep forgetting to do that:
    Code:
    ofstream a_file ( (input + ".sav").c_str() );
    A C-style string is expected, but after the concatenation all you have is a string object. So you need to call the c_str member function to get a C-style string.
    My best code is written with the delete key.

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    Thank you much! That works beautifully! Now, all I need to learn is how to make a @ dude that can move around, and I'm well on my way

    (I've decided to go after my roguelike dream again)

  6. #21
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    You gonna use the arrow keys for that? I'd recommend this FAQ link then

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    Yes, I have started a new thread in the games section for this, located here: http://cboard.cprogramming.com/showt...430#post591430

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem grabbing k/b input
    By falcon9 in forum C Programming
    Replies: 2
    Last Post: 10-28-2007, 11:47 AM
  2. continues input
    By Matty_Alan in forum C Programming
    Replies: 2
    Last Post: 06-22-2007, 10:04 PM
  3. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  4. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  5. Simple Console Input for Beginners
    By jlou in forum C++ Programming
    Replies: 0
    Last Post: 06-21-2005, 01:50 PM