Problem with char

Jul 7, 2011 at 10:24pm
I'm trying to copy out this program, which illustrates a use of char type.

#include <iostream>

using namespace std;

int main()
{
char letter;

for(letter= "A"; letter <= "Z"; letter++)
cout<< letter;
return 0;
}


My compiler gives the error- Invalid conversion from "const char*" to "char", and the error- ISO C++ forbids comparison between pointer and integer.

Can somebody explain these errors?

Jul 7, 2011 at 10:45pm
closed account (zb0S216C)
Your error is referring to the assignment, and the condition within your for loop. When assigning, or comparing a character to a char type, you must specify the character within single quotes, like this: char Char = 'a'.

Wazzak
Last edited on Jul 7, 2011 at 10:45pm
Jul 10, 2011 at 9:10pm
Im having trouble with this: (char),(name) it says "name" is undefined??

#include <iostream>
using namespace std;

int main()
{
//program code

//declared then initialized
float nums[3];
nums[0] = 1.5; nums[1] = 2.27; nums[2] = 3.25

//declared and initialized
char name[5] = {'m','i','k','e','\0'};
int coords[2][3] = {{1,2,3},{4,5,6}};

//insert statements
cout << "nums[0]:" << nums[0] << endl;
cout << "nums[1]:" << nums[1] << endl;
cout << "nums[2]:" << nums[2] << endl;
cout << "name[0]:" << name[0] << endl;
cout << "Text string:" << name << endl;
cout << "coords[0][2]:" << coords[0][2] << endl;
cout << "coords[1][2]:" << coords[1][2] << endl;

getchar();
return 0;
}

this should be right, but it doesn't work. what am i doing wrong?
Last edited on Jul 10, 2011 at 9:17pm
Jul 10, 2011 at 9:28pm
1
2
3
//declared then initialized
float nums[3];
nums[0] = 1.5; nums[1] = 2.27; nums[2] = 3.25


This might not be your problem but I plugged it into my compiler and it told me you were missing a ';' after the 3.25

cout << "Text string:" << name << endl;

You don't have a variable by titled 'name'

You do have an array of char dubbed 'name'. Perhaps try including the index? i.e. name[0] or name[1] etc...?

I don't really know, just learning and reading.
Jul 10, 2011 at 9:32pm
Wow! your absolutely right. thank you for your reply. My compiler told me i was missing that (;), but didnt say where. I thought i had everything in place, i was wrong. thanks again, you're super.
Topic archived. No new replies allowed.