Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Can anyone tell me what I did wrong here?


  • Please log in to reply

#1
Whipsmack

Whipsmack

    New Member

  • Member
  • Pip
  • 9 posts
Just trying to learn C++ on my own. Only problem with that is I have no one I can go to when I have little questions. I'm trying to figure out why this isn't working:

#include <iostream.h>
int main ()
{
int i;
int j;

i = 5000;

cout << "Please enter a number";
cin >> j;

if j > i;

cout << "Your number is greater than 5000";

if j == i;

cout << "Your number is 5000";

else

cout << "Your number is lesser than 5000";

return 0;
}

For any other complete newbies out there, I left the brackets out it should look like this

if (j > i) {
cout <<" BLAH BLAH" << endl;
}

Edited by Whipsmack, 15 June 2005 - 03:49 PM.

  • 0

Advertisements


#2
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 875 posts
do you have a c++ book? the first thing you want to learn is how to use the index of your book. read up on 'if' statements.

another thing to look at - the messages from the compiler. pick out the 1st error. then look at the line number called out by the compiler. if you are lucky(which is often) the compiler will actually identify the first line with a problem.
  • 0

#3
Whipsmack

Whipsmack

    New Member

  • Topic Starter
  • Member
  • Pip
  • 9 posts
Ah, thanks for the tips there. No I don't have a book yet. Do you have a suggestion? I did order Beginning C++ for gaming (hasn't shipped yet), I hear its a good one. But do you have another recommendation for more of a referance guide?

Thanks oh and i'll refrain from the ultra newbie stupid questions from now on :tazz:

I suppose I could take a few classes too, that would probably help more more than learning on my own. I don't know if my brain will be able to handle all this stuff though i'm not the sharpest tool in the shed lol
  • 0

#4
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 875 posts
I don't have a recommendation on a C++ book. If Deital & Deital has a C++ book, you might check it out(they have an excellent java text).

check out the tutorials listed in this topic

taking a class is a good idea, too. if you have a choice, you might consider taking some java classes. java is similar to C++, without the C++ headaches.

good luck
  • 0

#5
m2zt

m2zt

    Member

  • Member
  • PipPip
  • 75 posts
a good book to get for c++ is c++ programming in easy steps

its not very big and has alot of examples
  • 0

#6
MaverickSidewinder

MaverickSidewinder

    Member

  • Member
  • PipPipPip
  • 257 posts
I don't know if this helps or not, but when you do:

cout << "blablablablabla";
Shouldn't you do it like this?
cout << "blablablablabla" << endl;

;)

I'm not really sure since im a beginner also...but i tried to compile a program once without having put << endl; and it gave me a compile error.

Anyway good luck!

:tazz:

- Maverick
  • 0

#7
UV_Power

UV_Power

    Member

  • Member
  • PipPipPip
  • 391 posts
Dont feel too bad, Whipsmack. I have taken four C++ classes and I still make that same error now and again.

The important thing to remember is to not ask for help too soon. Several times I have gotten stuck and I try to fix it using the "stare" method (which all programmers are familiar with. Looks kinda like this: ;) ). After about an hour (or sometimes more :tazz:) I just got up and went for a walk. 15 minutes later I sat back in my chair and I saw the problem within seconds.

A helpful tip when programming (and I wish someone told me this when I first started) is to frequently take breaks. You will save mountains of time in the long run.

Good luck out there ;)
  • 0

#8
UV_Power

UV_Power

    Member

  • Member
  • PipPipPip
  • 391 posts
Also, in response to MaverickSidewinder...

This:
cout << "blablablablabla";

will not give you a compiler error. Adding an "<< endl;" to the end is for readability only. My guess is the reason you got a compiler error is that you put this:
cout << "blablablablabla"

when it should say this:
cout << "blablablablabla";


Have fun out there :tazz:
  • 0

#9
Whipsmack

Whipsmack

    New Member

  • Topic Starter
  • Member
  • Pip
  • 9 posts
I have figured out all the brackets now for if else statements. They are my favorite ;)

Now i'm trying to make a dice roller and just found out that the rand() is far from being random :tazz:
  • 0

#10
mpfeif101

mpfeif101

    Member 1K

  • Retired Staff
  • 1,411 posts
Yeah, you have to seed it with time (which isnt actually random), or something actually random (like radioactive decay or something).
  • 0

Advertisements


#11
Whipsmack

Whipsmack

    New Member

  • Topic Starter
  • Member
  • Pip
  • 9 posts
radioactive decay holy crap lol...I know that you can use things like the duration between your last left click of your mouse or how much harddrive space you have for the seed. Or heck even millaseconds would work for me. Just want to make sure the seed is different one out of say 10,000 times. I suppose I should make another thread about that?

Having a hard time finding good info on truely random stuff, only pseudo randomness, but I guess thats how it is with computers. But what about lottery and online casinos surely that have some code that is truely random, prolly insanely hard to code eh?
  • 0

#12
UV_Power

UV_Power

    Member

  • Member
  • PipPipPip
  • 391 posts
If you use this include:
#include <ctime>

and you put this at the beginning of main (or anywhere in main before rand()):
srand(time(0));

then the randomizer will not reset to the same starting point when you terminate the program. It should give you different results every time you run it.

I don't know if this addresses your problem or not, but I hope it helps :tazz:
  • 0

#13
chickenman

chickenman

    Member

  • Member
  • PipPip
  • 37 posts
The program below is the working version of it :tazz:

#include <iostream>
using namespace std;

int main ()
{
int i;
int j=0;

i = 5000;

cout <<"Please enter a number ";
cin >> j;
cin.ignore();

if ( j > i ) {

cout << "Your number is greater than 5000\n";
}

if  ( j == i ) {

cout << "Your number is 5000\n";
}

if (j < i ) {
cout <<"Your number is lesser than 5000\n"; }

cin.get();
return 0;
}


  • 0

#14
rockwall

rockwall

    New Member

  • Member
  • Pip
  • 5 posts
fyi, even slot machines are not random, and though the makers try to make them unpredictable, ppl occationally find paterns. it is physically impossible 4 a computer 2 b random b/c there is no factor that would change on its own. if 2 identical computers run 2 pieces of code with identical input, the output will b the same. perfect randomness in a game isn't important anyways tho.
  • 0

#15
UV_Power

UV_Power

    Member

  • Member
  • PipPipPip
  • 391 posts

it is physically impossible 4 a computer 2 b random


Please see my previous post...

When I use it in a program, it seems to be as random as can be. Even though it may not be PERFECT random, it's pretty close. Please prove me wrong if I am so I can stop spreading lies... :tazz:
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP