Hi,
The excercise I'm trying solving it today is printing the numbers from (32 to 256) and the ASCII values in table, I completed the code but the console appear one value only.. :(
The excercise:

ASCII table

Make a program that writes a table of all characters with values from 32 to 255.

 

Example:
value 	character
62 	>
63 	?
64 	@
65 	A
66 	B
and so on...

 

Hint: if you output a variable of type char you will get the corresponging character on the screen. If you output a variable of type int you will get the value as a number on the screen.

my code:

#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;

void main()
{	int i=32;
	for(i=32; i<=256; i++);
	cout << setw(0) << "value" << setw(14) << "character" << endl;
	cout << setw(0) << i << setw(10) << static_cast<char>(i) << endl;
	getch();
}

my output:

Recommended Answers

All 7 Replies

Hi,

my code:

#include <iostream>
#include <iomanip>
#include <conio.h>  //leave this out, it's nonstandard
using namespace std;

void main()
{	int i=32;
	for(i=32; i<=256; i++);    //something extra on this line
	cout << setw(0) << "value" << setw(14) << "character" << endl;
	cout << setw(0) << i << setw(10) << static_cast<char>(i) << endl;
	getch(); //change this to cin.get()  it's much more portable
}

my output:

You need to shift your for loop around (first remove something that doesn't belong) as you only want to print out the numbers that many times -- the way you have it once you remove the extra something from that line you would print the header that many times. Take another crack at it.

why I can't print all numbers and all ASCII values

Dude, you've got to listen to what people are trying to tell you and not just repeat your question. If there's something you don't understand, say so.

Look at the structure of your for loop. Compare it to other for loops you've seen. How many have had a ; directly after the for statement?

you are right, I'm listing than trying ..
you mean for loop brackets, I put it but no change :(

#include <iostream>
#include <iomanip>
using namespace std;

void main()
{	int i;
	for(i=32; i<=256; i++);
	{
	cout << setw(0) << "value" << setw(14) << "character" << endl;
	cout << setw(0) << i << setw(10) << static_cast<char>(i) << endl;
	}
	cin.get();
}

Ok, good that's a bit closer. I told you about line 7. Putting the semicolon after the for() will cause the loop to be skipped. Get rid of it.

Also, I didn't mention it before but main should always return int. void main() is not standard and should be avoided.

After you fix the semicolon, one more problem is going to crop up. See if you can figure out how to rearrange things to prevent this.

Thanks 'jonsca' :)
your way in explanation is good, you learned me with indirect way, you let me correct mistake by myself.
I moved the line:

cout << setw(0) << "value" << setw(14) << "character" << endl;

out the for loop, to print it once:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{	int i;
	cout << setw(0) << "value" << setw(14) << "character" << endl;
	for(i=32; i<=256; i++)
	{
	cout << setw(0) << i << setw(10) << static_cast<char>(i) << endl;
	}
	return 0;
}

but when i changed 'void' with 'int' I got the same result..! sorry I have two questions:
1) what is the difference between 'void main()' and' int main()' ?
2) if I become professional in c++ in the future, is it enough and I will not need java and c#?

but when i changed 'void' with 'int' I got the same result..! sorry I have two questions:
1) what is the difference between 'void main()' and' int main()' ?
2) if I become professional in c++ in the future, is it enough and I will not need java and c#?

Great! I'm glad you got it working.

For the answer to #1, see http://www.eskimo.com/~scs/readings/voidmain.960823.html. When the operating system (or other code) that's calling your program expects to get an integer value back from your program and you don't give it one, it can cause problems.
At times these return values are codes to let the calling program know whether your code has exited successfully (which is represented by return 0; )or if another error has occurred.
If you declare main as an int, even if you don't put a return 0 (or other return value at the end) it will implicitly return 0 to the OS anyway.
You may not have experienced any change or error in switching from void to int, but you've prevented the possibility of things getting fouled up when running the code under different conditions.
I'm probably leaving off some bits and pieces of the details here but if you google void main versus int main lots of stuff comes up.

As for #2 I'm not really sure, but I am sure that the requirements for programmers vary greatly from workplace to workplace and region to region. I will tell you I have been actively immersed in learning all of those languages at one time or another and the "meat" of them is very similar so if you know one well, picking up another is more about finding the nuances between the languages and mastering those changes.

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.