Hello, I am having a small problem because I have to have to have it so the user can enter a number and that number is stored in a variable and that variable becomes the name of the file to be created with the extension .asw. It is going to be a custom encrypted file. Just need to know what is wrong because I am getting an error right now.

cout << "Enter the Item ID: \n";
 string ItemID;
 cin >> ItemID;
 ofstream ItemFile;
 ItemFile.open(ItemID.c_str()".asw");
 ItemFile.close();

The error is error: expected `)' before string constant|

You formed the string incorrectly ItemFile.open((string)(ItemID+".asw").c_str());

The result of operator+ for std::string is always going to be a new std::string of the same type, so your cast is redundant. This is cleaner:

ItemFile.open((ItemID + ".asw").c_str());
Comments
Zing!

ok still getting errors on both suggestions

Ancient Dragon Error
error: no matching function for call to `std::basic_ofstream<char, std::char_traits<char> >::open(std::string)'|

Narue Error

error: invalid operands of types `void ()()' and `const char[5]' to binary `operator+'|

I think I mistyped one of my variables I changed because it seems to be working now, thanks. Ill post if I have any problems

now I have only 1 error left and its from this part of the code

ofstream file;
file.open((ItemIDd + ".asw").c_str());

it says that 'file' was not declared in the scope. Any help appreciated.

no its not the problem is the word 'file' i just added a d to ItemID because I needed to change the word quick.

error: `file' was not declared in this scope|

#include <iostream>
#include <fstream>
using namespace std;
void ItemID();
void ItemID1();
string ItemIDd;


int main()
{
    ItemID();
}
void ItemID()
{
    cout << "Enter the Item ID: \n";
    cin >> ItemIDd;
    ofstream file;
    file.open((ItemIDd + ".asw").c_str());
    file.close();
    ItemID1();
}

  void ItemID1()
  {
    cout << "Enter 1st Digit of the Item ID: \n";
    string ItemID1;
    cin >> ItemID1;

    if (ItemID1 == "1")
        ofstream file;
        file.open((ItemIDd + ".asw").c_str());
        "*&^@" >> file;
        file.close();
}

I got a lot of errors on that code. example: line 16: ItemIDd was not declared.

line 26: you can't name a variable with the same name as a function. Name the variable on line 26 something else.

line 32: what the hell is that????

lines 29-33: you need to surround it with { and } if you want all those statements inside the if clause. Put a { after line 29 and a } after line 33.

im still getting that 1 error, line 32 is an encryption btw and i added the include <string>. There is somethign wierd going on because the only thing wrong it says is that file is not declared when it does seem to be declared when i typed
ofstream file;

I am using the GNU GCC compiler btw

>> line 32 is an encryption btw
Doesn't matter because its still wrong.

You have that last remaining error because you didn't include the { and } correctly. See below.

I get a clean compile with this (vc++ 2008 express). I commented out that bad line near the bottom of the program.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void ItemID();
void ItemID1();
string ItemIDd;


int main()
{
    ItemID();
}
void ItemID()
{
    cout << "Enter the Item ID: \n";
    cin >> ItemIDd;
    ofstream file;
    file.open((ItemIDd + ".asw").c_str());
    file.close();
    ItemID1();
}

  void ItemID1()
  {
    cout << "Enter 1st Digit of the Item ID: \n";
    string ItemID1;
    cin >> ItemID1;

    if (ItemID1 == "1")
    {
        ofstream file;
        file.open((ItemIDd + ".asw").c_str());
     //   "*&^@" >> file;
        file.close();
    }
}

ok but what is the syntax to take something such as a string "4l@3" and put it into the file that I just opened if the line you commented out is wrong

it still not working I am getting an error

error: no match for 'operator>>' in 'file >> "*&^@"'|

yeah I just figured it out on my own but thanks for all the help it makes sense now

This article has been dead for over six months. Start a new discussion instead.