What can i do to solve it?

Recommended Answers

All 10 Replies

Fix the code?

i don't know what this error means

It would appear that you are passing a null pointer to some function. Check for null before passing the value to the function.

And it's difficult for us to observe from the 0 lines of code you posted. ;)

It would appear that you are passing a null pointer to some function. Check for null before passing the value to the function.

And it's difficult for us to observe from the 0 lines of code you posted. ;)

LOL, here is it, but it's confusing i think.
Don`t mind the text between cout, i`m brazilian.

#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdio.h>
using namespace std;
int main() 
{
int a;
string *n;
char *pc;
ofstream *p;
cout << "Quantos arquivos de texto vc quer?";
cin >> a;
p = new ofstream[a];
n = new string[a];

for (int i=0;i!=a-1;i++)
{

cout << "Escreva o titulo do " << i+1  << " arquivo:";
getline(cin,n[i]);
pc = new char[n[i].size()];
strcpy(pc,n[i].c_str());
p[i].open(pc);  // when i take it out i get no error, i dont know why...

}

cout << "Feito" << endl;

return 0;
}

Are you passing an empty string to .open()?

Are you passing an empty string to .open()?

No, because the pointer pc is holding the char array.
First i made that program that just ask for a title for a file and then what will be in it, but it was not taking the "spaces", so i tried to use a string, but i have to convert it to char array before passing it in .open.
The program works when this line pc = new char[n.size()]; is outside the for loop and it's bad because i cant get getline information on the loop, it has to be before that line to give the size of the array.

It also works when i delete this line: p.open(pc); i cant see the problem with it.

No, because the pointer pc is holding the char array.

Did you verify this be, say printing out the text of the file to be opened? Like,

cout << "pc = \"" << pc << "\"\n";

Ya see, when you input a number it leaves the newline in the stream. This will pretty much ensure that the filename is empty.

First i made that program that just ask for a title for a file and then what will be in it, but it was not taking the "spaces", so i tried to use a string, but i have to convert it to char array before passing it in .open.

You don't have to. In fact, you have the other option right there in your strcpy call.

But an empty string, a size() of zero, going to new -- I wouldn't expect great things. Fix your user input problems upstream.

You don't have to. In fact, you have the other option right there in your strcpy call.

I tried to do it but when i put the string in the first line of getline function i just get this error:
error C2664: 'strcpy' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char *'

i want to know if it is a problem to use new inside a loop...

What I was saying was something akin to...

int main() 
{
   string input;
   cout << "Quantos arquivos de texto vc quer?";
   int a;
   if ( getline(cin, input) )
   {
      istringstream iss(input);
      if ( iss >> a )
      {
         ofstream *p = new ofstream[a];
         for ( int i = 0; i < a; ++i )
         {
            cout << "Escreva o titulo do " << i+1  << " arquivo:";
            getline(cin,input);
            p[i].open(input.c_str());
         }
         cout << "Feito" << endl;
      }
   }
   return 0;
}

what i was saying was something akin to...

thank you forever

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.