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|

Recommended Answers

All 21 Replies

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

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());
commented: Zing! +1

ok thanks guys

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+'|

You must have mistyped something -- Narue's version works ok, mine doesn't. So post code so we can see what you did

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.

>>ItemIDd
That's misspelled.

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|

works fine for me, so it must be something else you've done. try posting all the code.

#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.

you forgot to include <string> header file.

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

you had it backwards file >> "4l@3";

commented: Lot's of patience and grand advice! +1

ok thanks

it still not working I am getting an error

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

Oops! sorry but it should be << not >> file << "*&^@";

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.