I'm totally new to C++, my only experience with programming being 13 years ago in highschool when I wrote some Pascal and QBasic stuff.
I only started learning last night (so about 3 hours under my belt so far), and a friend challenged me to learn "rand" to get me going. He suggested I make a magic 8-ball sort of program, but since I was already working on a guessing game (I learned from a previous forum poster here) I thought I'd modify that to include what I'd learn from an 8-Ball game.
Problem is... last insult, "butthead" is always what comes up. I got the guessing game part working just fine. My trouble is with making my program extremely rude! Haha. =)
So three things:
1) Why isn't Insult working? It's not the rand, I know that - if I sent InsultN to any number it still comes up as "butthead". Must be something about my if statements.
2) How do I streamline said insult statements into less lines? Worth doing?
3) How's my commenting? I think it's efficient. Better way?
Hey, so I haven't rand your code yet, and assuming everything works:
in your if statements you are using the assignment operator "=" and not the equals operator "=="
see if that makes a difference.
and if it does, as you probably already know, you will only get a "new" insult at the start of the program, and what ever the first is, will be used, because you are only randomizing the insultN in the beginning.(not sure if it suppose to be like that or not tho)
and as for using less lines of code, There are a few ways to go about it:
1.Type them in a text file, and then in the beginning of the program pull each one out and store it in an array, then randomly call an insult from that array.
This way you are able to update the amount of insults you use by simply adding them to this text file.
2. Using an Array as in the option above but without the text file. (not as efficient tho)
3....um I'm 200% there are a ton of other ways to this without the the if-statements, I just can't think of it any :-/
and as for the comments, keep on parts of code for explanations, but on line 18,23 etc stuff like "//one" wouldn't be seen as useful information, and it takes up more space.
Thanks for the advice! That sounds like that's it for sure - I forgot about the difference between "=" and "==". Therefore InsultN becomes 10.
I also now realize that "int InsultN=rand()%10+1;;" after the guess response won't work to re-randomize the insult. Or at least I don't think so. It should just be "InsultN=srand(time(0));", right? That should re-randomize InsultN?
I'll have to see when I get home and try it. I'll also have to try an array, as I haven't gotten to that yet.
So, well I see nothing wrong with: insultN = rand()%10 +1; //returns a value from 1-10
And as for: srand(time(0));
All this line of code really does is give rand() different numbers, each time you call rand()
so I wouldn't make changes to that aspect of your code.
plus I don't think : InsultN=srand(time(0));
would give you the results you want/or even work
What I would do is take the string of insults, and store them into a function, have this function return a insult(string) that will be used in the program, but I would have this function take an integer value (a random one from 1-10) and it will return the corresponding insult(string).
so something like: (this is just example code)
And as for:
srand(time(0));
All this line of code really does is give rand() different numbers, each time you call rand()
so I wouldn't make changes to that aspect of your code.
Thats not what it is doing at all ;p
What I would do is take the string of insults, and store them into a function, have this function return a insult(string) that will be used in the program, but I would have this function take an integer value (a random one from 1-10) and it will return the corresponding insult(string).
so something like: (this is just example code)
That is a bit complicated for this stage of the game I believe. Best not overwhelm the problem with complicated answers.
Anyways what booradley is saying is you need to move your srand(time(0)); outside of your while loop so it doesn't get called every time your loop executes.
I also now realize that "int InsultN=rand()%10+1;;" after the guess response won't work to re-randomize the insult. Or at least I don't think so. It should just be "InsultN=srand(time(0));", right? That should re-randomize InsultN?
Actually you had it right the first time. When you want a new random number in your variable InsultN you would call it like InsultN = rand() % 10 + 1;. Just think of srand() as initializing your random number generator you only need to call it once. After that you can call rand() to get a new random number.
Also another thing to think on once you got your original program working like you want it to. If you want to make it look a bit more clean you can trade out all them if else conditions for a switch statement. Which would look something like this
switch (InsultN)
{
case 1:
// What you want to do if the number is 1
break; // You need this break statement
case 2:
// What you want to do if the number is 2
break;
case 3:
// What you want to do if the number is 3
break;
default:
// This is special it will execute this case if InsultN holds a number
// that you haven't specified in your switch statement. You can think of this
// like a else condition, it will run if the other conditions aren't true.
break;
}
Just a suggestion, best of luck with your program and learning. Let us know if you have any more problems and we would be glad to help out.
And as for:
srand(time(0));
All this line of code really does is give rand() different numbers, each time you call rand()
so I wouldn't make changes to that aspect of your code.
Thats not what it is doing at all ;p
I'm no expert by any means, but I'm pretty sure srand()is just setting the seed for the rand,
and when you set the seed to time(0) (current time in seconds) it will change the seed value which will allow rand() to give a different number each time it is called. Because without srand(time()), you would get the same seed value (default seed value is like 1 or something I think), or if you used srand(someValue), it would use the same value each time rand() was called.
Like I said I'm not expert, but I'm always open for explanations ^_^
Zereo wrote:
That is a bit complicated for this stage of the game I believe. Best not overwhelm the problem with complicated answers.
I also agree, I was just responding to: Videshi wrote:
2) How do I streamline said insult statements into less lines? Worth doing?
but again, I agree using a switch statement would be a good alternative as well.
Thanks for so much for the advice, I really appreciate it! I haven't tried "switch" yet, but I'll definitely have a go at that, it seems like a more efficient (in my way of thinking) method of doing what I want, especially with the use of "default". Much better than a series of "if" statements when "else" isn't really going to be involved. Less brackets in my code, too, which in this early stage I find myself counting to make sure I've done them correctly. I'm assuming that's one of those things that'll become automatic over time.
I'm using the C++ Primer (v.6), by the way, as my book, and Dev C++ as my IDE.
Okay! I got it working!. The real problem was my misunderstanding of "while". I needed to have my insults within that while, because that's what's basically constantly looping throughout my program.
Shouldnt you get the random insult before the switch and you may want another do/while for the play again oh I see its set default to rand then each time it gets previous random insult