Need Help with programming getting an error dont know what to do

I am trying to get this program to work what it is supposed to do is
PRINTING CONTENTS OF ARRAY and adding x to every other element
A x C x E x G x I x K x M x O x Q x S x U x W x Y x

• Write the code that will display only the even or odd numbered elements within the array. The output should appear as follows:

PRINTING CONTENTS OF ARRAY USING THE MOD Option
=====================================================
Even Numbered Element = 0 Contents of Element within Array is = A
Even Numbered Element = 2 Contents of Element within Array is = C
Even Numbered Element = 4 Contents of Element within Array is = E
Even Numbered Element = 6 Contents of Element within Array is = G
Even Numbered Element = 8 Contents of Element within Array is = I
Even Numbered Element = 10 Contents of Element within Array is = K
Even Numbered Element = 12 Contents of Element within Array is = M
Even Numbered Element = 14 Contents of Element within Array is = O
Even Numbered Element = 16 Contents of Element within Array is = Q
Even Numbered Element = 18 Contents of Element within Array is = S
Even Numbered Element = 20 Contents of Element within Array is = U
Even Numbered Element = 22 Contents of Element within Array is = W
Even Numbered Element = 24 Contents of Element within Array is = Y



I have everything else written but having problems with
void problem3()
{
char letters[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
bool odd = false;

cout << "PRINTING CONTENTS OF ARRAY and adding x to every other element" << endl;

for (int i = 0; i < sizeof(letters); ++i)
{
if ( odd ) {
letters = 'x';
}

odd = !odd;

cout << letters << " ";
}

cout << endl;
}

the error i am getting is cannot convert from 'char' to 'char [26]'
which is underlining the letters = 'x'; and i dont know what to do if i could get some help that would be great

Here is my full programming code


#include <iostream>
using std::cin;
using std::cout;
using std::endl;

void problem1()
{
char letters[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

cout << "PRINTING CONTENTS OF ARRAY" << endl;
cout << "==================================" << endl;

for (int i = 0; i < sizeof(letters); ++i)
{
cout << letters << " ";
}

cout << endl;
}

void problem2()
{
char letters[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

cout << "This is the title to your Program related to the alphabet." << endl;
cout << endl;
cout << "Select the number that coincides with the alphabet." << endl;
cout << "For example, the number 7 should display the letter G." << endl;
cout << endl;
cout << "Enter a number between 1 and 26: ";

int number;
cin >> number;

cout << endl;
cout << "The number you selected: " << number << endl;

if ((number > 0) && (number <= sizeof(letters)))
{
cout << "The letter related to this number: " << letters[number-1] << endl;
}
else
{
cout << "Sorry, the number must be between 1 and 26." << endl;
}
}

void problem3()
{
char letters[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
bool odd = false;

cout << "PRINTING CONTENTS OF ARRAY and adding x to every other element" << endl;

for (int i = 0; i < sizeof(letters); ++i)
{
if ( odd ) {
letters = 'x';
}

odd = !odd;

cout << letters << " ";
}

cout << endl;
}

void problem4(bool even)
{
char letters[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
char* prefixes[] = { "Even", "Odd" };

cout << "PRINTING CONTENTS OF ARRAY USING THE MOD Option" << endl;
cout << "=====================================================" << endl;

int start;

if (even)
{
start = 0;
}
else
{
start = 1;
}

for (int i = start; i < sizeof(letters); i += 2)
{
cout << prefixes[start] << " Numbered Element = " << i;
cout << " Contents of Element within Array is = " << letters << endl;
}
}

int main()
{
problem1();
cout << endl;
problem2();
cout << endl;
problem3();
cout << endl;
problem4(true);
cout << endl;
return 0;
}


You're attempting to assign a character to an array of characters, the problem is solved by accessing the index of the array, in your case letters[i]

Edit: Also, odd is an int, any integer thats not 0 will be true, try making use of the modulus operator ( % )
Last edited on
Topic archived. No new replies allowed.