Help naming files

This is a discussion on Help naming files within the C++ Programming forums, part of the General Programming Boards category; Hello All, I currently am writing a program that creates recipe text files. I have a folder on my desktop ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    8

    Help naming files

    Hello All,

    I currently am writing a program that creates recipe text files. I have a folder on my desktop to hold the files. My program asks the the user to name the recipe using the std::getline function. I want to use that variable to name the file at a certain path. Here is my code:

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
        std::string name, category, group, style, prep, ingred, instruct, notes;
        char answer;
    
        //Recipe information questions:
    
        std::cout<<"Recipe name?\n\n";
        std::getline ( std::cin, name );
        ofstream recipe("C:\\Users\\Tony\\Desktop\\magic cookbook\\",name);            //names the recipe file
    
        std::cout<<"\n\nCategory?\n\n";
        std::getline ( std::cin, category );
        recipe << "Recipe Category: ";
        recipe << category;
    
        std::cout<<"\n\nFood Group?\n\n";
        std::getline ( std::cin, group );
    
        std::cout<<"\n\nFood Style?\n\n";
        std::getline ( std::cin, style );
    
        std::cout<<"\n\nPrep Time?\n\n";
        std::getline ( std::cin, prep);
    
        std::cout<<"\n\nRecipe Ingredients?\n\n";
        std::getline ( std::cin, ingred );
    
        std::cout<<"\n\nCooking Instructions?\n\n";
        std::getline ( std::cin, instruct );
    
        std::cout<<"\n\nServing Notes?\n\n";
        std::getline ( std::cin, notes );
    
        //outputing the recipe
    
        cout<<"\n\nView Recipe? y/n?\n\n";
        cin>> answer;
    
        if (answer == 'y')
            {
                std::cout<<"\n\n" << name <<"\n\n";
                std::cout<<"Category: "<< category <<"\n\n";
                std::cout<<"Food Group: "<< group <<"\n\n";
                std::cout<<"Food Style: "<< style <<"\n\n";
                std::cout<<"Prep Time: "<< prep <<"\n\n";
                std::cout<<"Ingredients: "<< ingred <<"\n\n";
                std::cout<<"Cooking Instructions: "<< instruct <<"\n\n";
                std::cout<<"Serving Notes: "<< notes <<"\n\n";
            }
    
    }

    Here are my errors:
    C:\Users\Tony\Desktop\magic cookbook\magic2\main.cpp|16|error: no matching function for call to `std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(const char[38], std::string&)'|


    I'm modifying the last program that I made just to display the input that the user gave.
    Eventually I want to program to gather all the input and save it as a file.

    How do I use that path for the file and add the variable name at the end?

    Thanks,
    Tony

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why not std::ofstream just like everything else?

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    8
    Quote Originally Posted by tabstop View Post
    Why not std::ofstream just like everything else?
    Didn't know you can do that. I'm pretty much a programing novice. I'll try it and report back thanks!

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Actually now that I look at it, you've got using namespace so you're fine.

    Why are you trying to give two things to the constructor? The constructor doesn't want two things.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    8
    Quote Originally Posted by tabstop View Post
    Actually now that I look at it, you've got using namespace so you're fine.

    Why are you trying to give two things to the constructor? The constructor doesn't want two things.
    I want the program to save the file to the C:\Users\Tony\Desktop\magic cookbook\ directory with what ever name the want to name it. EX: chicken.txt.

    I just want to make sure they are placed in the right folder.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Concatenate the path and the filename. Then use the c_str method, because ofstream's constructor takes a C-style string.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-11-2009, 07:45 AM
  2. Header files and multiple definitions
    By sjweinberg in forum C++ Programming
    Replies: 16
    Last Post: 07-17-2009, 06:59 PM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 05:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM

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