ascii "hello world"

Hello. I am currently working with C++ and was instructed by my teacher to convert my "hello world" code to use ascii decimal values instead of human characters for printing hello world to the screen.
would this work?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main()
{
	char ascii;
	int numeric;
	cout << "72"
	cout << "69"
	cout << "108"
	cout << "108"
	cout << "79"
	cout << "32"
	cout << "119"
	cout << "79"
	cout << "82"
	cout << "76"
	cout << "68"
	return 0;
}
Did you try to compile it?

Does it compile?

Does it run?

Does it produce the desired output?

it's not compiling in Visual Studio.
it says it cannot find myprogram.exe
but it says it successfuly built it, but cannot find the myprogram.exe in the debug folder. Why?

real message:


Unable to start program 'C:\users\Max\documents\Visual Studio 2012\Projects\ascii hello world\Debug\ascii hello world.exe'.

The system cannot find the file specified.
Last edited on
Well since there are quite a few errors it shouldn't compile.

Did you create a project or are you just trying to compile your file without a project?

I created a project and the .cpp is within the project. The main point though, is weither or not I was correct with my code. Could you please point me in the right direction?
@maxrax

1: You need to put a semi-colon after each cout statement
2: You are only trying to print numbers, not the ascii value of them
3: Try putting cout << char(the ascii number); instead of cout << "the ascii number"
thanks dude!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

int main()
{
	char ascii;
	int numeric;
	cout << char(72);
	cout << char(69);
	cout << char(108);
	cout << char(108);
	cout << char(79);
	cout << char(32);
	cout << char(119);
	cout << char(79);
	cout << char(82);
	cout << char(76);
	cout << char(68);
		return 0;
}

would this work? VS is still not compiling.
@maxrax

If you haven't turned off the need for adding the precompiled header, #include "'stdafx.h" , then no. Try adding it as the first line after any comments, and it should compile okay. Mine did..

Oh, and the ascii for capital 'W' is 87, not 119, and capital 'L' is 76
Last edited on
again thank you sir!
wait... still not working! godammit...
but would this code work?
if it does... than that's all I really need.
@maxrax

Here is the code I was able to compile, and run, using Visual Studio 2010 C++ Express. The only real change I did, was to comment out the char ascii and int numeric, since you weren't really using them in this program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Hello World.cpp : main project file.

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	//char ascii; // For this program, these two lines were not needed
	//int numeric;
	cout << char(72);
	cout << char(69);
	cout << char(76);
	cout << char(76);
	cout << char(79);
	cout << char(32);
	cout << char(87);
	cout << char(79);
	cout << char(82);
	cout << char(76);
	cout << char(68);
	cout << endl; // Just to add a new line after printing the two words
	return 0;
}


Oh yeah, I also noticed I accidentally added an apostrophe in #include "'stdafx.h" , that shouldn't have been there. Sorry about that.
Last edited on
k then. thanks!
You're welcome, @maxrax.
Topic archived. No new replies allowed.