Hello World weirdness

N

Niklas Borson

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;
}
 
T

Tobias Langner

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.
 
V

Victor Bazarov

Niklas Borson said:
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
 
A

Aaron Anodide

Victor Bazarov said:
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
 
V

Victor Bazarov

Aaron Anodide said:
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
 
N

Niklas Borson

Agent Mulder said:
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.
 
N

Niklas Borson

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,351
Latest member
LolaD32479

Latest Threads

Top