lost!!! I took a homework assignment that worked written in just main and for practice I wanted to make them classes with pointers and have one derived class of base. Although the while loop, for the most part worked before it was turned into a class, it now does not work. I tried using If statement but I am haveing problems constructing. Now I'm totally at a lose as what to do.


error C2664: 'strcmp' : cannot convert parameter 1 from 'char *[20]' to 'const char *
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.

sample.obj - 2 error(s), 0 warning(s)

*************************************************************

#include <iostream.h>	// This is used for the cout's and cin's
#include <fstream.h>	// This is used to read from and write to files
#include <stdlib.h>		// This gives us the exit(1) command
#include <string.h>	
	

class file1
{

protected:
	char *filename[12];
	char *name[20];


public:
	file1()
	{ 	cout << "Enter the filename: ";
		cin >> *filename;
		cout << endl; 
	}

	void file();
	//void display();
};


void file1::file()
{



// Creating an instance of outbound data
	ofstream outfile;

	outfile.open(*filename, ios::app);

	if (outfile.fail())
	{
		cout <<"The file failed to open" <<endl;
		exit(1);
	}


	cout << "Enter names to the file called: "<< *filename <<endl;
	cout << "Enter end to stop entering names." << endl;

	
	cout << endl;


//loop to enter names and keep count
//section having problems with!
while(strcmp(name, "end"))
{
	

	int count = 0;
	cout << "Enter names: " << endl;
	cin >>  *name;}

	
	//send data to outfile
	outfile << *name;

	outfile << "\n";

	count++;
	cout << endl;

	
}

	// Closing the outfile to free up memory
	outfile.close();

return ;
}
class filederived : file1
{

public:
	char derived();

}

filederived::derived()
{


	int count = 0;
// Creating an instance of inbound data
	ifstream infile;

	// Opening the infile "filename" for reading
	infile.open(*filename);

	//to view amount of names entered
	cout << "You have entered " << count << " names " << "in file " << *filename << "\n\n";

	while(count>1)
	{
	// Reading the contents of "filename" 
	infile >> *name;

	//to view names entered into the file
	cout <<"The name you have entered is: " << *name << endl;
	count--;

	return ;
	}

	
	infile.close();
	return ;
}
	
int main()
{
	
	file1 a;
	filederived b;

	a.file();
	

	b.derived();

	return 0;
}

:rolleyes:

Dani AI

Generated

Brief diagnosis: the error comes from the way the name/filename members are declared and then used. In ’s code the members are arrays of pointers (not character buffers), and many places dereference those arrays (for example *filename or *name) instead of passing a proper null‑terminated C string to C functions like strcmp. That mismatch causes the C2664 message. Several other problems are visible as well: wrong return types for methods, missing public inheritance, count declared in the wrong scope, misplaced braces/returns, and use of obsolete headers (<iostream.h>).

Practical fixes (concise checklist):

  • Prefer std::string for filename and input lines; it avoids manual allocation and strcmp issues (as suggested).
  • If C‑style strings are required, declare a single buffer (char name[256];) or allocate one pointer with new[] and ensure an ending '\0'. Don’t use char *name[20] unless an array of 20 pointers is intended.
  • Read full lines with std::getline (operator>> stops at whitespace). Use if (name == "end") to stop; strcmp returns 0 when strings match, so while (strcmp(...) != 0) would be the equivalent.
  • Put int count = 0; outside the loop and increment it inside. Close files inside the function (or rely on RAII).

Minimal example of a safe input loop (modern style):

#include <iostream>
#include <fstream>
#include <string>

void writeNames() {
  std::string filename;
  std::getline(std::cin, filename);
  std::ofstream outfile(filename.c_str(), std::ios::app);
  if (!outfile) return;

  std::string name;
  int count = 0;
  while (std::getline(std::cin, name)) {
    if (name == "end") break;
    outfile << name << '\n';
    ++count;
  }
}

Class notes: declare class filederived : public file1 and make method signatures consistent (e.g., void derived() not char derived()), define filederived::derived() with a return type, and keep file operations and counters inside the correct scope. Use modern headers (<iostream>, <fstream>, <string>) and enable compiler warnings to catch many of these problems early.

Recommended Answers

All 5 Replies

strcmp parameters are invalid like the compiler says! I would convert to using the std::string class found in <string> as it is much easier to use and has most of its operators overloaded so you can do things like:

using namespace std;

string str1 = "This is a ";
str1 += "message";
cout << str1 << "\n"; // prints "This is a message"

Look at the lines:

protected:
char *filename[12];
char *name[20];

I assume U just need two variables for holding two strings, not a tables of 12 (20) pointers to char. If so, try to remove * before filename and name and fix other errors by yourself (there are several of them :) )

If you must use char * (c-style) strings then define them as

char *string1;
char *string2;

string1 = (char*) new char[length + 1]; // allocate memory for a string of a given length. the + 1 is for the \0 character

delete [] string1; // deletes allocated memory

the strings should be ok to pass to c string functions

I M FACING BELOW PROBLEM WHEN PROGRAMMING IN CLASS IN C++. PLZ SORT OUT THIS
WARNING IS:
-- FUNCTION SHOULD BE EXPANDED INLINE
class one
{
public:
int a,b,c;
void show()
{
for(i=0;i<=5;i++)
{
cout<<"sam";
}
}
};

void main()
{
one obj;
obj.show();
}

program is executing but compiler is showing warning i.e.

FUNCTION CONTAINING FOR ARE NOT EXPANDED INLINE.
IF ANY ONE HAVE IDEA ABOUT SAME THEN PLZ SORT OUT MY PROBLEM

I M FACING BELOW PROBLEM WHEN PROGRAMMING IN CLASS IN C++. PLZ SORT OUT THIS
WARNING IS:
program is executing but compiler is showing warning i.e.

FUNCTION CONTAINING FOR ARE NOT EXPANDED INLINE.
IF ANY ONE HAVE IDEA ABOUT SAME THEN PLZ SORT OUT MY PROBLEM
class one
{
public:
int a,b,c;
void show()
{
for(i=0;i<=5;i++)
{
cout<<"sam";
}
}
};

void main()
{
one obj;
obj.show();
}

program is executing but compiler is showing warning i.e.

FUNCTION CONTAINING FOR ARE NOT EXPANDED INLINE.
IF ANY ONE HAVE IDEA ABOUT SAME THEN PLZ SORT OUT MY PROBLEM

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.