I keep on having the error message stated in the title.
I am trying to move a string id and check with that ID is already present in an array of ID names.
if(!strcmp(id, identifier[z]))
Any ideas on how to solve this?
Printable View
I keep on having the error message stated in the title.
I am trying to move a string id and check with that ID is already present in an array of ID names.
if(!strcmp(id, identifier[z]))
Any ideas on how to solve this?
A conversion from char to const char should be valid. The error message is really about an invalid conversion from char to const char*, and that is probably because either id or identifier[z] is a char, i.e., it is not a string.
char identifier[maxLayer] = {" "};
id comes from a predefined file. The identifier[] declaration is given above.
Yes, therefore identifier[z] is a char. Why are you trying to compare a string with a single char? It sounds like you should compare id with identifier, not identifier[z].
I made that change and that error is now gone. However, I'm still being thrown one error. Not sure how to fix that one either. My code follows.
Code:#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;
int processData(char file[])
{
fstream fin;
int i, z;
char buffer[100];
int a, b, c, d, e, f, g, h, n;
char command[15], shapecode[13], id[13];
int layer;
const int maxLayer = 100;
char identifier[maxLayer] = {" "};
int integer[maxLayer] = {0};
int count = 0;
int identifier_counter = 0;
char character;
fin.open(file, ios::in);
if (fin.fail())
{
cout << "***Error: Exit Command Processed. Input file failed to open." << endl;
exit(-1); //Error -1 for failed to open
}
fin >> command;
while (!fin.eof()) {
// code here to determine command "switch"
if (!strcmp("Create", command))
{
// Suppose it is a "Create"
fin >> shapecode;
fin >> id;
fin >> layer;
if (integer[layer] != 0)
{
cout << "***Error: Shape created or moved to occupied layer.***";
}
else
{
for(z = 0; z < maxLayer; z++)
{
if(!strcmp(id, identifier))
cout << "***Error: Shape identifier already exists.***";
}
fin.getline(buffer,100);
n = sscanf(buffer, "%d %d %d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f, &g, &h, &i);
if (!strcmp("triangle", shapecode) && n!= 6)
cout << "***Error: Invalid vertix count for given shape code.***";
if (!strcmp("square", shapecode) && n!= 8)
cout << "***Error: Invalid vertix count for given shape code.***";
if (!strcmp("rectangle", shapecode) && n!= 8)
cout << "***Error: Invalid vertix count for given shape code.***";
if (!strcmp("trapezoid", shapecode) && n!= 8)
cout << "***Error: Invalid vertix count for given shape code.***";
else
cout << "***Error: Missing, or invalid shape code specified.***";
if (count >= 50)
{
cout << "***Error: Unable to add shape: shape structure is full.***";
exit(-2);
}
else
{
count = count + 1;
/*
case 's':
cout << "Please enter 4 coordinate pairs representing the vertices of your square\n";
cin >> x0 >> y0 >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
// Verify it forms a valid square
// Left edge x's must be same, Right edge x's must be same
// Bottom edge y's must be same, Top edge y's must be same
// Left and Top edge must be same length
// Edge length must be greater than zero
if (!(x0 == x2 && x1 == x3 && y0 == y1 && y2 == y3
&& (x3-x2) == (y2-y0) && x3-x2>0))
errCode |= 1;
if (!errCode) {
// Drawing code goes here...
}
break;
case 'r':
cout << "Please enter 4 coordinate pairs representing the vertices of your rectangle\n";
cin >> x0 >> y0 >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
// Verify it forms a valid square
// Left edge x's must be same, Right edge x's must be same
// Bottom edge y's must be same, Top edge y's must be same
// Width and height must be greater than zero
if (!(x0==x2 && x1==x3 && y0==y1 && y2==y3 && x3-x2>0 && y2-y0 > 0))
errCode |= 2;
if (!errCode) {
// Drawing code goes here...
}
break;
case 't':
cout << "Please enter 3 coordinate pairs representing the vertices of your rectangle\n";
cin >> x0 >> y0 >> x1 >> y1 >> x2 >> y2;
// Verify it forms a valid triangle
// either first, or last two y's must be same
// Diff of 1st and 3rd y's larger than zero
// if first 2 y's same, then third x matches 1st OR 2nd x
// AND diff of 1st/3rd y's same as 1st/2nd x's
// if second 2 y's same, then first x matches 2nd OR 3rd x
// AND diff of 1st/3rd y's same as 2nd/3rd x's
if (!((y0==y1 && y2-y0>0 && (x2==x0 || x2==x1) && (y2-y0 == x1-x0))
|| (y1==y2 && y2-y0>0 && (x0==x1 || x0==x2) && (y2-y0 == x2-x1))))
errCode |= 4;
if (!errCode) {
// Drawing code goes here...
}
break;
*/
}
fin >> command;
strcpy(id, identifier);
integer[layer] = 1;
}
}
else if (!strcmp("Translate", command))
{
fin >> id;
for(z = 0; z < maxLayer; z++)
{
if (!strcmp(id, identifier))
//Insert processes here
;
else
cout << "***Error: Shape with given identifier does not exist.***";
}
}
else if(!strcmp("Layer", command))
{
fin >> id;
fin >> layer;
if (integer[layer] == 0)
{
for(z = 0; z < maxLayer; z++)
{
if (!strcmp(id, identifier))
//Insert processes here
;
else
cout << "***Error: Shape with given identifier does not exist.***";
}
}
else if(!strcmp(id, identifier))
{
for(z = 0; z < maxLayer; z++)
{
if (!strcmp(id, identifier))
//Insert processes here
;
else
cout << "***Error: Shape with given identifier does not exist.***";
}
}
else
cout << "***Error: Shape created or moved to occupied layer.***";
}
else if(!strcmp("Display", command))
{
fin >> command;
}
else if(!strcmp("Fill", command))
{
fin >> id;
fin >> command;
strcpy(command, character);
for(z = 0; z < maxLayer; z++)
{
if (!strcmp(id, identifier))
if (!isascii(character))
//Insert Processes Here
;
else
cout << "***Error: Missing, or invalid fill character specified.***";
else
cout << "***Error: Shape with given identifier does not exist.***";
}
}
else if(!strcmp("Delete", command))
{
fin >> id;
for(z = 0; z < maxLayer; z++)
{
if (!strcmp(id, identifier))
{
integer[z] = 0;
count = count - 1;
}
else
cout << "***Error: Shape with given identifier does not exist.***";
}
}
else if(!strcmp("Exit", command))
{
fin.close();
cout << "***Error: Exit command processed.***";
exit(count);
}
else
cout << "***Error: Invalid Command.***";
fin.close();
}
cin.get();
cin.ignore();
return count;
}
I'm getting the following error.
'invalid conversion from 'char' to 'const char*' at the italicized location.
strcpy expects two strings. You have passed one string and one char.
The solution is easy: just do command[0] = character;
And if command has not been initialized previously, or if it's empty, add command[1] = '\0';
But this poses the question: why aren't you using std::string?
I finally got to the end of my assignment. My code is currently sitting as follows. Can you guys take a look at it and help me cut out the errors?
Any help would be great.
Code:#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std;
int countOfShapes;
struct vertex{
int x;
int y;
};
struct shape{
int layer;
char fillCharacter;
char shapeCode[20];
char identifier[20];
vertex coordinates[4];
};
int processData(char []);
int layerValidation(shape[], int, int);
void layerValidation(shape[], int, char[], int);
int shapeCodeValidation(char[]);
int identifierValidation(char[13], shape[50], int, int);
int vertexValidation(int[], int[], char[20], int);
void moveUp(shape[50], int, int);
void moveDown(shape[50], int, int);
void sortVertexSet(int[], int[], char[]);
void drawTheShapes(shape[50], int);
void drawSquare(struct vertex[], char);
void drawRectangle(struct vertex[], char);
void drawTrapezoid(struct vertex[], char);
void drawTriangle(struct vertex[], char);
void initService()
/*
This program is called from the main. It initialzes the values on any global arrays or countOfShapes.
Input: Static boolean value which verifies whether it is the first time this function is called. Also given access to any global variables.
Output: No direct output. Global variables and arrays are reset and initialized to 0.
Processing Steps:
1) If myFirstTimeCalled = true
a) Display what the function does
b) set myFirstTimeCalled = false
2) Initialize countOfShapes to 0
3) End Function and return to the calling function
*/
{
static bool myFirstTimeCalled = true;
if (myFirstTimeCalled)
{
cout << "You have choosen to initialize the arrays. This function will now auto-initialize the global variables.";
myFirstTimeCalled = false;
}
countOfShapes = 0;
}
void refreshBuffer()
{
}
char getBufferXY(int, int)
{
}
int processData(const char file[])
{
fstream fin;
char buffer[100];
char command[20], shapeCode[20], identifier[40];
int layer, x[3], y[3];
int countOfShapes = 0;
int hasAnError = 0;
int shapeOverload = 0;
int extraVertix = 0;
struct shape shapeArray[50];
fin.open(file, ios::in);
if (fin.fail())
countOfShapes = -1;
else
{
while(!fin.eof() && strcmp(command, "Exit"))
{
for(int i = 0; i < 4; i++) {
x[i] = -1;
y[i] = -1;
}
identifier[12] = 0;
hasAnError = 0;
fin >> command;
if(!strcmp(command, "Translate"))
{
fin >> identifier;
identifierValidation(identifier, shapeArray, countOfShapes, 2);
fin.getline(buffer, 100);
}
else if (!strcmp(command, "Layer"))
{
fin >> identifier;
fin >> layer;
fin.getline(buffer, '\n');
identifierValidation(identifier, shapeArray, countOfShapes, 2);
layerValidation(shapeArray, layer, identifier, countOfShapes);
}
else if (!strcmp(command, "Fill"))
{
int n = 0;
char fillCharacter = '0';
hasAnError = 0;
fin >> identifier;
fin.getline(buffer, 100);
n = sscanf(buffer, "%1s", &fillCharacter);
if (n < 1)
{
cout << "***Error: Missing, or invalid shape code specified.*** \n";
hasAnError = 1;
}
if (!hasAnError)
{
for (int i = 0; i < countOfShapes; i++)
{
if (shapeArray[i].identifier == identifier)
shapeArray[i].fillCharacter = fillCharacter;
}
}
}
else if (!strcmp(command, "Display"))
{
fin.getline(buffer, 100);
drawTheShapes(shapeArray, countOfShapes);
}
else if (!strcmp(command, "Delete"))
{
hasAnError = 0;
int indexOfDelete = 0;
fin >> identifier;
fin.getline(buffer, 100);
hasAnError = identifierValidation(identifier, shapeArray, countOfShapes, 2);
if (!hasAnError)
{
drawTriangle(shapeArray[i].coordinates,
if (shapeArray[i].identifier == identifier)
indexOfDelete = i;
moveDown(shapeArray, countOfShapes, indexOfDelete);
}
countOfShapes--;
}
else if (!strcmp(command, "Exit"))
{
cout << "***Error: Exit command processed.*** \n";
}
else if (!strcmp(command, "Create"))
{
int n = 0;
extraVertix = -1;
fin >> shapeCode;
fin >> identifier;
fin >> layer;
fin.getline(buffer, 100);
n = sscanf(buffer, "%d %d %d %d %d %d %d %d %d", &x[0], &y[0], &x[1], &y[1], &x[2], &y[2], &x[3], &y[3], &extraVertix);
sortVertexSet(x, y, shapeCode);
if (countOfShapes == 50)
{
cout << "***Error: Unable to add shape: shape structure is full.*** \n";
shapeOverload = 1;
}
else
{
hasAnError += identifierValidation(identifier, shapeArray, countOfShapes, 1);
hasAnError += layerValidation(shapeArray, layer, countOfShapes, 1);
hasAnError += shapeCodeValidation(shapeCode);
hasAnError += vertexValidation(x, y, shapeCode, n);
if (!hasAnError)
{
int indexOfInsert = 0;
for (int i = 0; i < countOfShapes; i++)
{
if (layer > shapeArray[i].layer)
indexOfInsert = i + 1;
}
moveUp(shapeArray, indexOfInsert, countOfShapes);
shapeArray[indexOfInsert].layer = layer;
strcpy(shapeArray[indexOfInsert].shapeCode, shapeCode);
strcpy(shapeArray[indexOfInsert].identifier, identifier);
for (int i = 0; i < 3; i++)
{
shapeArray[indexOfInsert].coordinates[i].x = x[i];
shapeArray[indexOfInsert].coordinates[y].y = y[i];
}
countOfShapes++;
}
}
}
else
{
cout << "***Error: Invalid Command.*** \n";
fin.getline(buffer, 100);
}
}
}
fin.close();
if (shapeOverload)
{
countOfShapes = -2;
}
return countOfShapes;
}
void moveDown(shape shapeArray[50], int countOfShapes, int indexOfDelete)
{
for (int i = indexOfDelete; i < countOfShapes; i++)
{
shapeArray[i].layer = shapeArray[i + 1].layer;
shapeArray[i].fillCharacter = shapeArray[i + 1].fillCharacter;
strcpy(shapeArray[i].shapeCode, shapeArray[i + 1].shapeCode);
strcpy(shapeArray[i].identifier, shapeArray[i + 1].identifier);
for (int j = 0; j < 4; j++)
{
shapeArray[i].coordinates[j].x = shapeArray[i + 1].coordinates[j].x;
shapeArray[i].coordinates[j].y = shapeArray[i + 1].coordinates[j].y;
}
}
}
void moveUp(shape shapeArray[50], int indexOfInsertion, int countOfShapes)
{
for (int i = countOfShapes; i > indexOfInsertion; i--)
{
shapeArray[i].layer = shapeArray[i - 1].layer;
shapeArray[i].fillCharacter = shapeArray[i - 1].fillCharacter;
strcpy(shapeArray[i].shapeCode, shapeArray[i - 1].shapeCode);
strcpy(shapeArray[i].identifier, shapeArray[i - 1].identifier);
for (int j = 0; j < 4; j++)
{
shapeArray[i].coordinates[j].x = shapeArray[i - 1].coordinates[j].x;
shapeArray[i].coordinates[j].y = shapeArray[i - 1].coordinates[j].y;
}
}
}
void sortVertexSet(int x[4], int y[4], char shapeCode[20])
{
int lowest = 0;
int indexOfSwitch = 0;
int xHolder = 0;
if (strcmp(shapeCode, "triangle"))
{
for (int i = 0; i < 4; i++)
{
cout << " " << x[i] << " " << y[i];
}
for (int i = 0; i < 4; i++)
{
lowest = y[i];
indexOfSwitch = i;
xHolder = 0;
for (int j = i + 1; j < 4; j++)
{
if (y[j] < lowest)
{
lowest = y[j];
indexOfSwitch = j;
}
else if (y[j] == lowest)
{
if (x[j] < x[indexOfSwitch])
{
lowest = y[j];
indexOfSwitch = j;
}
}
}
y[indexOfSwitch] = y[i];
y[i] = lowest;
xHolder = x[i];
x[i] = x[indexOfSwitch];
x[indexOfSwitch] = xHolder;
}
cout << endl;
for (int i = 0; i < 4; i++)
{
cout << " " << x[i] << " " << y[i];
}
cout << endl;
}
if (!strcmp(shapeCode, "triangle"))
{
for (int i = 0; i < 3; i++)
{
cout << " " << x[i] << " " << y[i];
}
for (int i = 0; i < 3; i++)
{
lowest = y[i];
indexOfSwitch = i;
xHolder = 0;
for (int j = i + 1; j < 3; j++)
{
if (y[j] < lowest)
{
lowest = y[j];
indexOfSwitch = j;
}
else if (y[j] == lowest)
{
if (x[j] < x[indexOfSwitch])
{
lowest = y[j];
indexOfSwitch = j;
}
}
}
y[indexOfSwitch] = y[i];
y[i] = lowest;
xHolder = x[i];
x[i] = x[indexOfSwitch];
x[indexOfSwitch] = xHolder;
}
cout << endl;
for (int i = 0; i < 3; i++)
{
cout << " " << x[i] << " " << y[i];
}
cout << endl;
}
}
int layerValidation(shape shapeArray[50], int layer, int countOfShapes)
{
int hasAnError = 0;
if (layer < 0 || layer > 100)
{
cout << "***Error: Invalid layer number specified.*** \n";
hasAnError = 1;
}
for (int i = 0; i < countOfShapes; i++)
{
if (layer == shapeArray[i].layer)
{
cout << "***Error: Shape created or moved to occupied layer.*** \n";
hasAnError = 1;
}
}
return hasAnError;
}
void layerValidation(shape shapeArray[50], int layer, char identifier[20], int countOfShapes)
{
if (layer < 0 || layer > 100)
cout << "***Error: Invalid layer number specified.*** \n";
for (int i = 0; i < countOfShapes; i++)
{
if (layer == shapeArray[i].layer)
{
if(strcmp(identifier, shapeArray[i].identifier))
cout << "***Error: Shape created or moved to occupied layer.*** \n";
}
}
}
int shapeCodeValidation(char shapeCode[20])
{
int hasAnError = 0;
if (strcmp(shapeCode, "triangle") && strcmp(shapeCode, "rectangle") && strcmp(shapeCode, "trapezoid") && strcmp(shapeCode, "square"))
{
cout << "***Error: Missing, or invalid shape code specified.*** \n";
hasAnError = 1;
}
return hasAnError;
}
int identifierValidation(char identifier[13]), shape shapeArray[50], int countOfShapes, int sender)
{
int hasAnError = 0;
int hasBeenUsed = 0;
if (identifier[12] > 0)
{
hasAnError = 1;
cout << "***Error: Missing, or invalid identifier specified.*** \n";
}
else
{
for (int i = 0; i < int(strlen(identifier)); i++)
{
if (!isalpha(identifier[i]))
{
hasAnError = 1;
cout << "***Error: Missing, or invalid identifier specified.*** \n";
}
}
}
for (int i = 0; i <countOfShapes; i++)
{
if (!strcmp(identifier, shapeArray[i].identifier))
hasBeenUsed = 1;
}
if (sender == 1 && hasBeenUsed)
{
cout << "***Error: Shape identifier already exists.***\n";
hasAnError = 1;
}
if (sender == 2 && !hasBeenUsed)
{
cout << "***Error: Shape with given identifier does not exist.***\n";
hasAnError = 1;
}
return hasAnError;
}
int vertexValidation(int x[3], int y[3], char shapeCode[20], int vertexCount)
{
int hasAnError = 0;
if ((!strcmp(shapeCode, "square") || !strcmp(shapeCode, "rectangle") || !strcmp(shapeCode, "trapezoid")) && vertexCount != 8)
{
cout << "***Error: Invalid vertex count for given shape code.***\n";
hasAnError = 1;
}
else if(!strcmp(shapeCode, "triangle") && (vertexCount !=6))
{
cout << "***Error: Invalid vertex count for given shape code.***\n";
hasAnError = 1;
}
if (!strcmp(shapeCode, "triangle"))
{
x[3] = 0;
y[3] = 0;
}
if (x[0] > 78 || y[0] > 24 || x[0] < 0 || y[0] < 0 || x[1] > 78 || y[1] > 24 || x[1] < 0 || y[1] < 0 || x[2] > 78 || y[2] > 24 || x[2] < 0 || y[2] < 0 || x[3] > 78 || y[3] > 24 || x[3] < 0 || y[3] < 0)
{
cout << "***Error: Missing, or specified coordinates fall outside of plot area.***\n";
hasAnError = 1;
}
if (!strcmp(shapeCode, "square"))
{
if (!(x[0] == x[2] && x[1] == x[3] && y[0] == y[2] & (x[3] - x[2]) == (y[2] - y[0]) && (x[3]-x[2]) > 0))
{
cout << "***Error: Invalid vertex set for the given shape code.***" << endl;
hasAnError = 1;
}
}
if (!strcmp(shapeCode, "rectangle"))
{
if (!(x[0]==x[2] && x[1] ==x[3] && y[0]==y[1] && y[2]==y[3] && x[3]-x[2]>0 && y[2]-y[0]>0))
{
cout << "***Error: Invalid vertex sset for the given shape code.***" << endl;
hasAnError = 1;
}
}
if (!strcmp(shapeCode, "trapezoid"))
{
if (0)
{
cout << "***Error: Invalid vertex set for the given shape code.***" << endl;
hasAnError = 1;
}
}
if (!strcmp(shapeCode, "triangle"))
{
if (!((y[0]==y[1] && y[2]-y[0]>0 && (x[2]==x[0] || x[2]==x[1]) && (y[2]-y[0] == x[1]-x[0])) || (y[1]==y[2] && y[2]-y[0]>0 && (x[0]==x[1] || x[0]==x[2]) && (y[2]-y[0] == x[2]-x[1]))))
{
cout << "***Error: Invalid vertex set for the given shape code.***" << endl;
hasAnError = 1;
}
}
return hasAnError;
}
void drawTheShapes(shape shapeArray[50], int countOfShapes)
{
for (int i = 0; i < countOfShapes; i++)
{
if (!strcmp(shapeArray[i].shapeCode, "square"))
drawSquare(shapeArray[i].coordinates, shapeArray[i].fillCharacter);
if (!strcmp(shapeArray[i].shapeCode, "rectangle"))
drawRectangle(shapeArray[i].coordinates, shapeArray[i].fillCharacter);
if (!strcmp(shapeArray[i].shapeCode, "trapezoid"))
drawTrapezoid(shapeArray[i].coordinates, shapeArray[i].fillCharacter);
if (!strcmp(shapeArray[i].shapeCode, "triangle"))
drawTriangle(shapeArray[i].coordinates, shapeArray[i].fillCharacter);
}
}
void drawSquare(vertex coordinates[4], char fillCharacter)
{
}
void drawRectangle(vertex coordinates[4], char fillCharacter)
{
}
void drawTrapezoid(vertex coordinates[4], char fillCharacter)
{
}
void drawTriangle(vertex coordinates[4], char fillCharacter)
{
}
Don't use end-of-file tests to control your loops. This is rarely done correctly. I've already explained this to one other person today and I'm tired so I'll spare you my little paragraph on why and just encourage you to do a little research on that.Code:while(!fin.eof() && strcmp(command, "Exit"))
I really need to write that paragraph/blurb to a file somewhere so I can just cut and paste.
Line 156:
"i" is undefined.Code:drawTriangle(shapeArray[i].coordinates,
Line 157:
Why are you trying to put an if-statement inside a function call?Code:if (shapeArray[i].identifier == identifier)
Line 187:
You are trying to call a function that accepts (sharep[50], int, int, int). But you have no such function. You only have:Code:hasAnError += layerValidation(shapeArray, layer, countOfShapes, 1);
layerValidation(shape[50], int, char[20], int)
layerValidation(shape[50], int, int)
Line 205:
"y" is an array, not an index!Code:shapeArray[indexOfInsert].coordinates[y].y = y[i];
Line 391:
See the out of place paranthesis?Code:int identifierValidation(char identifier[13]), shape shapeArray[50], int countOfShapes, int sender)
Line 455:
Did you mean &&?Code:if (!(x[0] == x[2] && x[1] == x[3] && y[0] == y[2] & (x[3] - x[2]) == (y[2] - y[0]) && (x[3]-x[2]) > 0))
Line 471:
Pointless?Code:if (0)
Unreadable piece of mess.Code:if (x[0] > 78 || y[0] > 24 || x[0] < 0 || y[0] < 0 || x[1] > 78 || y[1] > 24 || x[1] < 0 || y[1] < 0 || x[2] > 78 || y[2] > 24 || x[2] < 0 || y[2] < 0 || x[3] > 78 || y[3] > 24 || x[3] < 0 || y[3] < 0)
if (!((y[0]==y[1] && y[2]-y[0]>0 && (x[2]==x[0] || x[2]==x[1]) && (y[2]-y[0] == x[1]-x[0])) || (y[1]==y[2] && y[2]-y[0]>0 && (x[0]==x[1] || x[0]==x[2]) && (y[2]-y[0] == x[2]-x[1]))))