is the following c++ code for printing the ASCII value of a number correct?

#include<iostream.h>
#include<conio.h>
 void main()
{ int a=10;
 char ch[10];
ch=a;
cout<<ch;
 
getch();
}

Recommended Answers

All 2 Replies

Wow what compiler are you using - #include <iostream.h>

No, the code is not correct at all.

It's not supposed to be <iostream.h> , it's just supposed to be <iostream> .

You shouldn't be using <conio.h> at all, because that just allows you to use old DOS-isms that can be done in a more portable way.

You're not supposed to use void main() , you are supposed to use int main() . Furthermore, you are supposed to have return 0; at the end of your program, not the ancient and deprecated getch(); .

The line ch=a; will immediately cause a compiler error because ch is a static array, and that line of code is an attempt to change its location (which is illegal). You don't even need an array in this case anyway. Actually, you don't even need the variable a .

I'm pretty sure you need to have using namespace std; before the main() if you want to be able to use cout .

If you want to print the a character based on the ASCII value with cout , the following is sufficient:

#include <iostream>

using namespace std;

int main()
{
    char ct = 10;    //Replace the number 10 with whatever the ASCII value is.
    cout << ct;

    return 0;
}
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.