By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,225 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

Hello World weirdness

P: n/a
Hi gurus! I'm a C++ newbie and I've heard that a good
first program is one that prints "Hello World!"

I tried typing in a program I found in a book, but
for some reason it wouldn't compile. So I just changed
things at random until it compiled and almost worked.
That is, it prints out two words, but they're not
"Hello World!" I don't know how to fix this, and am
afraid to change anthing else lest my compiler just
start yelling at me again!

Here's my program. Can anyone please help?

#include <cstdio>
template<int N,int D>
void p(char c)
{
p<0,97>(N%26+D);
p<N/26,97>(c);
}
template<>
void p<0,97>(char c)
{
std::fputc(c, stdout);
}
int main()
{
p<5179226,65>(32);
p<8428737,65>(33);
return 0;
}

-----------------------------------------------
"Life is too important to be taken seriously."
--Oscar Wilde
-----------------------------------------------
Jul 19 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
> Hi gurus! I'm a C++ newbie and I've heard that a good
first program is one that prints "Hello World!"

I tried typing in a program I found in a book,


In wich book did you find THAT program, I'd really like to read that.
Jul 19 '05 #2

P: n/a
"Niklas Borson" <ni*****@microsoft.com> wrote...
Hi gurus! I'm a C++ newbie and I've heard that a good
first program is one that prints "Hello World!"

I tried typing in a program I found in a book, but
for some reason it wouldn't compile. So I just changed
things at random until it compiled and almost worked.
That is, it prints out two words, but they're not
"Hello World!" I don't know how to fix this, and am
afraid to change anthing else lest my compiler just
start yelling at me again!

Here's my program. Can anyone please help?

#include <cstdio>
template<int N,int D>
void p(char c)
{
p<0,97>(N%26+D);
p<N/26,97>(c);
}
template<>
void p<0,97>(char c)
{
std::fputc(c, stdout);
}
int main()
{
p<5179226,65>(32);
p<8428737,65>(33);
return 0;
}


You need to change the values (not at random, though):

p<((((14*26)+11)*26+11)*26+4)*26,72>(32);
p<((((3*26)+11)*26+17)*26+14)*26,87>(33);

or

p<6598540,72>(32);
p<1576120,87>(33);

Should do it. BTW, Visual C++ v6 cannot produce correct code
for this program. Perhaps the next one will be able to.

Victor
Jul 19 '05 #3

P: n/a
> So I just changed things at
random until it compiled


On my system it say !slooF liprA, but
I am still backtracking the code.

-X
Jul 19 '05 #4

P: n/a

"Victor Bazarov" <v.********@attAbi.com> wrote in message
news:vi************@corp.supernews.com...
"Niklas Borson" <ni*****@microsoft.com> wrote...
Hi gurus! I'm a C++ newbie and I've heard that a good
first program is one that prints "Hello World!"

I tried typing in a program I found in a book, but
for some reason it wouldn't compile. So I just changed
things at random until it compiled and almost worked.
That is, it prints out two words, but they're not
"Hello World!" I don't know how to fix this, and am
afraid to change anthing else lest my compiler just
start yelling at me again!

Here's my program. Can anyone please help?

#include <cstdio>
template<int N,int D>
void p(char c)
{
p<0,97>(N%26+D);
p<N/26,97>(c);
}
template<>
void p<0,97>(char c)
{
std::fputc(c, stdout);
}
int main()
{
p<5179226,65>(32);
p<8428737,65>(33);
return 0;
}
You need to change the values (not at random, though):

p<((((14*26)+11)*26+11)*26+4)*26,72>(32);
p<((((3*26)+11)*26+17)*26+14)*26,87>(33);

or

p<6598540,72>(32);
p<1576120,87>(33);


Excuse my ignorance, but what is the meaning of numbers as template
arguments?

Aaron

Should do it. BTW, Visual C++ v6 cannot produce correct code
for this program. Perhaps the next one will be able to.

Victor

Jul 19 '05 #5

P: n/a
"Aaron Anodide" <an*****@hotmail.com> wrote...

"Victor Bazarov" <v.********@attAbi.com> wrote in message
news:vi************@corp.supernews.com...
"Niklas Borson" <ni*****@microsoft.com> wrote...
Hi gurus! I'm a C++ newbie and I've heard that a good
first program is one that prints "Hello World!"

I tried typing in a program I found in a book, but
for some reason it wouldn't compile. So I just changed
things at random until it compiled and almost worked.
That is, it prints out two words, but they're not
"Hello World!" I don't know how to fix this, and am
afraid to change anthing else lest my compiler just
start yelling at me again!

Here's my program. Can anyone please help?

#include <cstdio>
template<int N,int D>
void p(char c)
{
p<0,97>(N%26+D);
p<N/26,97>(c);
}
template<>
void p<0,97>(char c)
{
std::fputc(c, stdout);
}
int main()
{
p<5179226,65>(32);
p<8428737,65>(33);
return 0;
}


You need to change the values (not at random, though):

p<((((14*26)+11)*26+11)*26+4)*26,72>(32);
p<((((3*26)+11)*26+17)*26+14)*26,87>(33);

or

p<6598540,72>(32);
p<1576120,87>(33);


Excuse my ignorance, but what is the meaning of numbers as template
arguments?


Erm... I don't know what to tell you, to be honest. Those are non-type
template arguments. The meaning... Well, whatever the author of the
program made them mean. I guess I am not sure what you mean when asking
that question.

template<int a> void stars()
{
cout << string(a, '*');
}

template<> void stars<0>() // specialisation
{
cout << '0';
}

template<int a> void stars_recursive()
{
stars_recursive<a-1>(); cout << '*';
}

template<> void stars_recursive<1>()
{
cout << '*';
}

int main()
{
stars<10>(); // creates one instantiation
stars_recursive<10>(); // creates 10 instantiations
}

I am not sure what else to tell you, my guess is you need to get
a book on templates. Vandevoorde and Josuttis wrote a good one.

Victor
Jul 19 '05 #6

P: n/a
Tobias Langner <to************@t-online.de> wrote in message news:<bg*************@news.t-online.com>...
Hi gurus! I'm a C++ newbie and I've heard that a good
first program is one that prints "Hello World!"

I tried typing in a program I found in a book,


In wich book did you find THAT program, I'd really like to read that.


C++ hello world for haskell gurus?
Jul 19 '05 #7

P: n/a
"Agent Mulder" <mb*******************@home.nl> wrote in message news:<bg**********@news4.tilbu1.nb.home.nl>...
So I just changed things at
random until it compiled


On my system it say !slooF liprA, but
I am still backtracking the code.


That's very odd. This program shouldn't depend on any
implementation-defined behavior such as byte order.
Jul 19 '05 #8

P: n/a
"Alex Hirner" <hi****@yahoo.com> wrote in message news:<bg**********@newsreader1.netway.at>...
Niklas Borson <ni*****@microsoft.com> schrieb in im Newsbeitrag:
bc**************************@posting.google.com...

for some reason it wouldn't compile. So I just changed
things at random until it compiled and almost worked.


Then continue changing things randomly until it works.

(That is what i would call a generic/reusable algorithm. It acutally
should make it into the STL.)


Yes, the "infinite number of monkeys" algorithm.
I understand there is a group working on this:
http://www.newtechusa.com/ppi/pressroom.asp
;-)
Jul 19 '05 #9

P: n/a
pi***@correo.nu (Bobo) wrote in message news:<74*************************@posting.google.c om>...
ni*****@microsoft.com (Niklas Borson) wrote in message news:<bc**************************@posting.google. com>...
"Agent Mulder" <mb*******************@home.nl> wrote in message news:<bg**********@news4.tilbu1.nb.home.nl>...
> So I just changed things at
> random until it compiled

On my system it say !slooF liprA, but
I am still backtracking the code.


That's very odd. This program shouldn't depend on any
implementation-defined behavior such as byte order.


It does. It only works if the system uses an ASCII compatible character table.


You're right of course, as I realized a short time after I
posted. Nevertheless, I don't think the backwards output
Agent Mulder reported could be explained by this.

The posted program sends the following sequence of character
codes to standard output:

65 112 114 105 108 32 70 111 111 108 115 33

In ASCII, this is "April Fools!"

Now try to devise a character encoding in which the above
sequence produces the output "!slooF lirpA". You may be able
to do so, but it will be a pretty odd duck, such as I doubt
you'd find in the wild. Bear in mind that at least some
character codes must be escaped (otherwise 108 and 111 would
each map to two different characters), yet the total number
of characters equals the total number of code points.
Jul 19 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.