I wanted to post this because I was not really sure what issue I was having with a simple assignment statement. I am doing a homework assignment that asks me to write structs and functions in a simple program to draw out shapes of ASCII characters. Right now I am just trying to test the functions I have written, and I am trying to assign a value to the symbol element of a Circle struct just to test out the DrawShape function I wrote. When I try to assign it a * char, I get an error message saying "error: invalid conversion from 'const char*' to 'char'". I will put the whole code in, though it is very long and unfinished. Any help with this would be appreciated. The problem I am getting is right in the beginning of the main at "circle1.char = '*' "
#include <iostream>
#include <math.h>
#include <cstdlib>
using namespace std;
const int NUMBER_OF_ROWS = 26;
const int NUMBER_OF_COLUMNS = 81;
char drawSpace[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
struct Point{
int x;
int y;
};
struct Circle{
Point center;
int radius;
char symbol;
bool buffer[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
};
bool setCircleRadius(Circle &b, int r);
bool setCircleCenter(Circle &b, int x, int y);
bool moveCircle(Circle &b, int x, int y);
void drawCircle (Circle b);
void lineChars(Line a);
void circleChars(Circle b);
void drawShapes();
int main() {
Circle circle1;
circle1.radius = 5;
circle1.symbol = "*";
circle1.center.x = 40;
circle1.center.y = 10;
drawCircle(circle1);
return 0;
}
You should be using single quotes for characters. Double quotes means you're using a (potentially single-character) string literal, which is represented as a const char *
(pointer to constant character).
Correct syntax: circle1.symbol = '*';
The problem is here:
circle1.symbol = "*";
circle1.symbol
is defined as char
, but you assign a string to it (an array of chars). What you need to do is
circle1.symbol = '*';
Your definition of Circle
says that symbol
is a char
, yet you try to assign it a string literal of type char[2]
:
circle1.symbol = "*";
Instead, you should be assigning it a char
:
circle1.symbol = '*';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With