ascii "hello world"

Apr 4, 2013 at 3:14am
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;
}
Apr 4, 2013 at 3:15am
Did you try to compile it?

Does it compile?

Does it run?

Does it produce the desired output?

Apr 4, 2013 at 3:20am
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 Apr 4, 2013 at 3:23am
Apr 4, 2013 at 3:22am
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?

Apr 4, 2013 at 3:25am
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?
Apr 4, 2013 at 3:40am
@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"
Apr 4, 2013 at 3:46am
thanks dude!
Apr 4, 2013 at 3:49am
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.
Apr 4, 2013 at 4:00am
@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 Apr 4, 2013 at 4:02am
Apr 4, 2013 at 4:04am
again thank you sir!
Apr 4, 2013 at 4:06am
wait... still not working! godammit...
Apr 4, 2013 at 4:08am
but would this code work?
if it does... than that's all I really need.
Apr 4, 2013 at 4:24am
@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 Apr 4, 2013 at 4:26am
Apr 4, 2013 at 4:29am
k then. thanks!
Apr 4, 2013 at 4:53am
You're welcome, @maxrax.
Topic archived. No new replies allowed.