Skip to main content
gamedev.net
🔒 Locked

Help... please!

Started by dmf_king Jan 19, 2004 at 9:47 AM 8 replies 500+ views
Original Post
dmf_king
dmf_king
Hi again, what''s wrong with this code? Thanx in advance! #include <iostream> int main () { int i; cout << "Type a number: "; cin >> i; if ((i%2)!=0) cout << "Number is odd.\n"; else cout << "Number is even.\n"; }
Nested Block
Nested Block
You forgot th .h after iostream, and your int main needs to return 0, so here''s your new source coode:

#include <iostream.h>

int main ()
{
int i;

cout << "Type a number: ";
cin >> i;

if ((i%2)!=0) cout << "Number is odd.\n";
else cout << "Number is even.\n";
return 0;
}
See ya.

"Your mother was a hamster and your father smelled of elderberries."
"Your mother was a hamster and your father smelled of elderberries."
Jingo
Jingo
You have forgot to qualify the std namespace in some way.


#include <iostream>

using namespace std;

int main ()
{
int i;

cout << "Type a number: ";
cin >> i;

if ((i%2)!=0) cout << "Number is odd.\n";
else cout << "Number is even.\n";
}


or


#include <iostream>

int main ()
{
int i;

std::cout << "Type a number: ";
std::cin >> i;

if ((i%2)!=0) std::cout << "Number is odd.\n";
else std::cout << "Number is even.\n";
}


Hope that helps
Jingo
Jingo
quote:
Original post by Nested Block
You forgot th .h after iostream, and your int main needs to return 0, so here''s your new source coode:




iostream.h is not a valid C++ header file :/. Needs to be iostream (without a .h).
Qw3r7yU10p!
Qw3r7yU10p!
What's the difference between <xxx> and <xxx.h> headers?

to the Original Poster, you'll get far better answers if instead of just posting code and asking "what's wrong with this?" you post a series of the reasons why you think it's wrong and why you don't understand how to fix it.

e.g.
post the error messages the compiler gives you.
Say that you've looked it up and don't understand the terms

or
post the error messages the compiler gives you.
Say that you've looked it up and understand what its saying but don't seem to be able to fix it.

or
Say that it's not giving the results you'd expect. Say what they are and what you expected. Say why they shouldn't be what you're getting

etc.

[edited by - petewood on January 19, 2004 11:28:02 AM]
Jingo
Jingo
quote:
Original post by petewood
you''ll get far better answers if instead of just posting code and asking "what''s wrong with this?"


Are you saying there is something wrong with my answers ?

Qw3r7yU10p!
Qw3r7yU10p!
quote:
Original post by Jingo
quote:
Original post by petewood
you''ll get far better answers if instead of just posting code and asking "what''s wrong with this?"


Are you saying there is something wrong with my answers ?




gosh... sorry about that... that was a real freudian slip.
uller
uller
iostream.h and iostream are both correct, however iostream.h is a relic only around for backward compatibility, like goto statements and the like, and as such is not advisable.

[edited by - Uller on January 21, 2004 7:34:01 AM]
"Great spirits have always encountered voilent opposition from mediocre minds."
Qw3r7yU10p!
Qw3r7yU10p!
quote:
Original post by uller
iostream.h and iostream are both correct

in what way is iostream.h correct?

quote:
however iostream.h is a relic only around for backward compatibility

that is true in as much as it is for compatibility with code written before standardisation. The iostream.h library and iostream library are very different. The iostream library works with iterators and the STL.

quote:
like goto statements and the like

goto is part of the standard


[edited by - petewood on January 21, 2004 8:02:37 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.