Utter Newbie Needs Help =)

Hello everyone!

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?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include<iostream>

using namespace std;

int main()
{

bool goOn=true;
while(goOn)
{
srand(time(0));
int number=rand() % 30 + 1;
int guess;
int InsultN=rand() % 10 + 1;
string Insult;

// --------------------Insult Strings-------------
// One
if (InsultN = 1)
{
            Insult = ("ugly");
            }
// Two 
else if (InsultN = 2)
{
            Insult = ("fatass");
            }
// Three 
else if (InsultN = 3)
{
            Insult = ("loser");
            }
// Four 
else if (InsultN = 4)
{
            Insult = ("sissy");
            }
// Five 
else if (InsultN = 5)
{
            Insult = ("cunt");
            }
// Six 
else if (InsultN = 6)
{
            Insult = ("pieface");
            }
// Seven 
else if (InsultN = 7)
{
            Insult = ("filth");
            }
// Eight 
else if (InsultN = 8)
{
            Insult = ("whore");
            }
// Nine 
else if (InsultN = 9)
{
            Insult = ("pig");
            }
// Ten 
else if (InsultN = 10)
{
            Insult = ("butthead");
            }
//--------------------End of Insult Strings--------------------------    

char answer;
cout<<"Im thinking of a number between 1-30. Take a guess: ";
cin>>guess; 


// Guessing Programming
while(guess!=number)
{
if(guess>number)
{
                cout<<("Nope! That's too high ") << Insult << (", Guess again!: ");
                cin>>guess;
                int InsultN=rand()%10+1;;
           } 
if(guess<number)
{
                cout<<("Nope! That's too low ") << Insult << (", Guess again!: ");
                cin>>guess;
                int InsultN=rand()%10+1;;
           }
}

// Guessing Was Correct
if(guess==number)
{ 
                 cout<<"Hard to believe, but you acutally got it." << endl;
}
cout<<"Would you like to play again? Enter y or n: ";
cin>>answer;
if(answer!='y')
{
               goOn=false;
               cout<<"Well then get the fuck out of here!"<<endl;
} 
}
system("PAUSE");
return 0;
}
Please excuse my harsh language in the program - I want to surprise my friend when I send the program and give him a laugh. =)
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.
Last edited on
Hi Ssturges,

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.
Last edited on
Only call srand(...) once in your program. After that, you should call rand() every time you want a new random number. Move line 11 to line 7.
Last edited on
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)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

string getInsult(int randNum){
 InsultN = randNum;
if (InsultN = 1)
{
            Insult = "ugly";
            }
 
else if (InsultN = 2)
{
            Insult = "fatass";
            }

else if (InsultN = 3)
{
            Insult = "loser";
            }
//etc

return Insult;

}


Then write before you make an insult, call this function like:
Insult = getInsult(rand()%10+1);


P.S. another alternative to the long if statement, is you don't want to use an array is a switch statement.

let me know of that works out
Last edited on
closed account (3qX21hU5)
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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.
Last edited on

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.

Hey guys,

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.

Hi again,

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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include<iostream>

using namespace std;

int main()
{

bool goOn=true;
srand(time(0));
while(goOn)
{
int number=rand() % 30 + 1;
int guess;
int InsultN=rand() % 10 + 1;
string Insult;



char answer;
cout<<"Im thinking of a number between 1-30. Take a guess: ";
cin>>guess; 


// Guessing Programming
while(guess!=number)
{
                    // -----------Insult Strings-----------

switch (InsultN)
{
       case 1:
            Insult = ("ugly");
            break;
            
       case 2:
            Insult = ("fatass");
            break;
       
       case 3:     
            Insult = ("loser");
            break;
       
       case 4: 
            Insult = ("sissy");
            break;
            
       case 5:
            Insult = ("cunt");
            break;
            
       case 6: 
            Insult = ("pieface");
            break;
            
       case 7:
            Insult = ("filth");
            break;
            
       case 8:
            Insult = ("whore");
            break;
            
       case 9:
            Insult = ("pig");
            break;
            
       case 10:
            Insult = ("butthead");
            break;
            }
//--------------------------------------End of Insult Strings----------------------------  
if(guess>number)
{
                InsultN = rand() % 10 + 1;
                cout<<("Nope! That's too high ") << Insult << (", Guess again!: ");
                cin>>guess;
           } 
if(guess<number)
{
                InsultN = rand() % 10 + 1;
                cout<<("Nope! That's too low ") << Insult << (", Guess again!: ");
                cin>>guess;
           }
}

// Guessing Was Correct
if(guess==number)
{ 
                 cout<<"Hard to believe, but you actually got it." << endl;
}
cout<<"Would you like to play again? Enter y or n: ";
cin>>answer;
if(answer!='y')
{
               goOn=false;
               cout<<"Well then get the fuck out of here!"<<endl;
} 
}
system("PAUSE");
return 0;
}
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
Last edited on
closed account (3qX21hU5)
Shouldnt you get the random insult before the switch


Umm the whole point of the switch is to get a random insult....


and you may want another do/while for the play again


Huh? Why make another loop when it isn't needed he already is taking care of the playing again with his first while loop...

Last edited on
I meant the number for the insult I know what switches are and whoops didn't see first while indents make thing much easier to read
Topic archived. No new replies allowed.