Jump to content

Recommended Posts

Posted

I decided this afternoon to look around on the internet and learn/teach myself C++. It has been a few hours and one thing has me stumped. I have written the source code for a program that gets you to try to guess a number between 1 and 10- you get it right and it congratulates you.... you get it wrong it says you are wrong. That part works. Below is the program source I have created.

 

#include <iostream>
using namespace std;

int main()
{
int X;

cout<<"Guess what number I am thinking of between 1 and 10: ";
cin>> X;
cin.ignore();
if (X == 7) {cout<<"Very good you must be psychic.\n";}
if (X != 7) {cout<<"Sorry you guessed wrong.\n";}
cin.get ();
}

 

Now my problem is how would I get to give a different message if they entered a number that is not 1-10? I have tried several things but so far they all ignore my attempts for a new message if something 11 or higher is entered.

 

 

Also what is iostream and why is it needed for this program to function and will all my programs need iostream? If that one isn't too difficult to answer.

Posted

I haven't done C++, but looking at your syntax and logic can you not just add another if statement below the last?

 

if (X>10 || x<1) {cout<<"That number is not between 1 and 10.\n";}

 

And nice work trying to pick up something new. :)

Posted

What Milk said. Or you could add the "else" part of the if statement but what Milk said is easier. It would look like this (I'm assuming its similar to Java in this aspect, idk too much C++ but i would bet the if else statements are the same).

 

if (X == 7) {cout<<"Very good you must be psychic.\n";}

else {cout<<"Sorry you guessed wrong.\n";}

if (X>10 || X<1) {cout<<"That number is not between 1 and 10.\n";}

Posted

Thanks for the attempt Milk that is one of the things I thought too but it only half way works if placed before the other if statements. It will display both "That number is not between 1 and 10." and "Sorry you guessed wrong." if you guess higher than 10.

 

Taking shots in the dark I made it work though I haven't the slightest idea why this works only when using "else if" statements. This is what I came up with.

 

#include <iostream>
using namespace std;

int main()
{
int X;

cout<<"Guess what number I am thinking of between 1 and 10: ";
cin>> X;
cin.ignore();
if (X>10 || X<1) {cout<<"Read the directions moron.... clearly not psychic.\n";}
else if (X == 7) {cout<<"Very good you must be psychic.\n";}
else if (X != 7) {cout<<"Sorry you guessed wrong.\n";}
cin.get();
}

Strangely enough if (X>10 || X<1) also works for letters as well..... seems like letters would have been covered by else if (X != 7).... oh well guess I will eventually figure this out.

 

PS Hobbit I tried your method and it yields the same problem as Milk's but I appreciate the attempt.

  • Administrators
Posted

Also what is iostream and why is it needed for this program to function and will all my programs need iostream? If that one isn't too difficult to answer.

 

@Wiki

Posted

Ahhh of course it would display both. A number greater than 10 or less than 1 is still not equal to 7, so both ifs were true and both outputs were displayed.

 

Else ifs were the way to go, nicely done.

 

Edit:

 

Looking at why the else works:

 

Else ifs are only tested if the first if fails. When you put in 11, program outputs "Read directions" and doesn't check elseifs.

 

When you put in 4, first if fails and so else ifs are tested. I'm sure you can find a deeper explanation somewhere if you need it. I think that's it, hope I'm not wrong on that.

 

This was nice distraction, back to study.

Posted

I'm actually taking a C++ class right now lol, Java is way better I think though for the built in functions. Ask Duckie he know Java pretty well.

Posted

cool to see you thinking about coding, anti...

 

programming languages are just a tool to accomplish tasks and create cool features.. they all have their own purposes... but its way harder to learn C++ after knowing Java.. so starting with C++ is actually a good strategy if you're going to learn a few... (Java > C++) && (C# > C++) && (C++ > C)

 

Most important thing when you get into programming languages is that you get yourself GOOD tools to help you with crappy syntax and stupid errors which compilers and run time errors can provide oh so easily. It's like reading a forum post and then out of nowh SEGMENTATION FAULT DS)DFG*S)DF*HSDF)H*&S)*#$)(*%#!!$#@LRSFGKS{DFG-08809281092384&()*&.

 

One other thing, Anti... you said that the program is supposed to make you guess a random number from 1 to 10... but it looks like there is no random number... it is hard coded to 7... =]

 

you can use this api to create a random number.

http://www.cplusplus.com/reference/clibrary/cstdlib/rand/

 

int randomInt = rand();

 

Familiar with modular arithmetic?

1230 mod 10 + 1 = 1

611 mod 10 + 1 = 2

49 mod 10 + 1 = 10

 

See the pattern?

rand() mod 10 + 1 = (random number through 1-10)

 

Then you'll actually have to guess a number =P

Posted

cool to see you thinking about coding, anti...

 

programming languages are just a tool to accomplish tasks and create cool features.. they all have their own purposes... but its way harder to learn C++ after knowing Java.. so starting with C++ is actually a good strategy if you're going to learn a few... (Java > C++) && (C# > C++) && (C++ > C)

 

Most important thing when you get into programming languages is that you get yourself GOOD tools to help you with crappy syntax and stupid errors which compilers and run time errors can provide oh so easily. It's like reading a forum post and then out of nowh SEGMENTATION FAULT DS)DFG*S)DF*HSDF)H*&S)*#$)(*%#!!$#@LRSFGKS{DFG-08809281092384&()*&.

 

One other thing, Anti... you said that the program is supposed to make you guess a random number from 1 to 10... but it looks like there is no random number... it is hard coded to 7... =]

 

you can use this api to create a random number.

http://www.cplusplus.com/reference/clibrary/cstdlib/rand/

 

int randomInt = rand();

 

Familiar with modular arithmetic?

1230 mod 10 + 1 = 1

611 mod 10 + 1 = 2

49 mod 10 + 1 = 10

 

See the pattern?

rand() mod 10 + 1 = (random number through 1-10)

 

Then you'll actually have to guess a number =P

 

Thanks Duckie but I never said random number (my skill wasn't high enough to attempt that at the time), but now thanks to you it is. ;)

Posted

Hmm in the last C++ compiler I was working on I believe it was g++ (linux) rand() was not an accepted function because it was not counted as pure C++ in the compiler

 

I was making a dice roller program and the error I kept getting was after I did find the randomize function it needed a seed number and boundaries to operate, aka randomize(somenumber,lowerboundary,upperboundary) and it would use the number you put in as a jumping off point for the randomization.

 

I put in some random junk, and tested it, unfortunately for me since I used a static variable for the seed it always cued up the same string of numbers.

 

so I tired grabbing the date and time from the computer and running that as the random seed number, it seemed to do the trick, till I tried rolling multiple dice, as they all rolled at the same time, the seed remained the same.

 

I thought of putting the seed number is as the date and time++1 or adding 1 every time the function was ran, well thinking of that since this was just as small part of a d20 based mud I was working on, as many times as the dice were to be rolled it would not take to horribly long to exceed the maximum number size, even putting it in as a double long. ( not sure if that is still around, it has been a while )

 

At last I got a final solution with grabbing the date and time and cpu ticks , putting them together as one integer and using that as the seed number, worked like a charm.

 

I really hope they have rand() back or some version of it on g++ next time I dig back into it, will make it so much easier..

 

Ended up making the dice roller class operable and created static variables of the class named D6 for 6 sided dice, etc, all the common dice up to D100, worked quite well as I could use this class as an integer anywhere I needed a dice roll.

 

if any of you have played a table top rpg such as D&D there are quite a lot of them all I had to do was cout<<D10 to reflect the roll of a 10 sided dice on the screen, or in rolling attacks using simple addition

 

example

Player attack bonus is 10

Weapon has attack bonus of 2

Using power attack for a negative bonus of -5

so to calculate the roll and display the results was

monsters ac = 18

 

int hitroll = (10+2-5+D20);

if ((hitroll == ac) || (hitroll > ac))

cout<<"you hit the monster for %i damage",damage;

else cout<<"Missed";

 

might have messed up on the syntax, I am rusty as hell, lol

 

I love programming, you never know what weird problem you will run into next =D

Posted

cool to see you thinking about coding, anti...

 

programming languages are just a tool to accomplish tasks and create cool features.. they all have their own purposes... but its way harder to learn C++ after knowing Java.. so starting with C++ is actually a good strategy if you're going to learn a few... (Java > C++) && (C# > C++) && (C++ > C)

 

Ohh now you say that -.- And I just finished Java.

Posted

Hmm in the last C++ compiler I was working on I believe it was g++ (linux) rand() was not an accepted function because it was not counted as pure C++ in the compiler

 

 

Another reason why Java is better... lol... you can actually use all the Java api... Java is Java... C++ is C.

Posted

I my opinion C++ is similar to C, as far as syntax is concerned.

 

After that, relearn everything, my last C++ program had no functions and made no function calls.

 

That's it.. I am gonna get back into it, damn I should not have read this post, it got me the bug again.. I wanna create something now.

 

P.S. I do want to learn Java, I don't know it, so I can say nothing as far as it being better/worse than C++.. at this time I am heavily biased for C++

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.