code giving weird symbol

Pages: 12
I don't know why but my code gives off a weird smiley face when it says what your character is

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
# include <iostream>

# include <string>

using namespace std;

int main()
{
	
string name;

string species;

int str = 2;

int hp = 10;

int def = 1;

int alignment = 0;

string alignmentt;

int intl = 1;

int maxmp = 0;

int mpregen = 0;

int mp = 0;

string weapon;

int randnum;

string proffesion;

cout << "in a world where games with graphics ruled a 13 year old programmer dreamed of text based games being great again, so he created this:" << endl;
system("pause");
system("CLS");

cout << "enter your name" << endl;

cin >> name;

system("CLS");

cout << "hello " << name << ", lets make you a character" << endl;

system ("pause");

system ("CLS");

randnum = rand () % 5 + 1;

if(randnum == 1) species = "orc" && str++ && alignment--;

if(randnum == 2) species = "gnome" && intl++ && alignment++;

if(randnum == 3) species = "elf" && maxmp++ && alignment++;

if(randnum == 4) species = "human" && hp++;

if(randnum == 5) species = "goblin" && def++ && alignment--;

randnum = rand () % 5 + 1;

if(randnum == 1) proffesion = "barbarian" && str++ && alignment--;

if(randnum == 2) proffesion = "wizard" && intl++ && maxmp++ && mpregen++ && alignment++;

if(randnum == 3) proffesion = "healer" && hp++ && alignment++;

if(randnum == 4) proffesion = "paladin" && str++ && alignment++;

if(randnum == 5) proffesion = "rouge" && def++ && alignment--;

if(alignment > 0) alignmentt = "good";

if(alignment < 0) alignmentt = "evil";

if(alignment == 0) alignmentt = "nuetral";

cout << "done!!, you are a " << alignmentt << " " << species << " " << proffesion << " with " << str << " strength " << intl << " intelligence " << hp << " health and " << def << " defense" << endl;

system("pause");

system("CLS");

}
Hello,

I don't exactly understand what you want to do, but I'm guessing you want to make a console application that shows a random description of a character.

I'm not following what your program really does, but, if the console is showing a smiley face, most probably you have an integer holding the value 1 and then you treat it as a character instead of an integer, so when you do that, the character whose ascii code is 1 is shown, which is the smiley. I noticed this once when I tried to convert an integer to a char to see what happens :P

Other than that, I'd like to say: Don't use using namespace std; even if it appears in most documentations. If you want to do something like a header and you use using namespace std; you may have problems with your program (especially if someone else uses your library), and it's not that hard to learn to not use it. Libraries like iostream and string.h have everything in the namespace std (like cout and cin, or endl), so you have to access it adding std:: on its left. It may look weird to you, but it's how it works. Don't use using namespace std;

Another thing you really shouldn't do is to use system(). I don't know why we all do it when we start, but it's a bad practice for many reasons, including security vulnerabilities, compatibility problems (will only work under windows), and more stuff. You have to learn to use C++ without system(). Just think about it, isn't using batch in your C++ code... how would you call it...
Instead of system("pause"); you can #include <conio.h> and then use getch();

Another practice you should do is to use tabs (or spaces) inside each block between {}, otherwise when you write a long code you won't easily read where every thing is, if you do this, you'll be able to see your program flow easier. Like this:

1
2
3
4
5
6
7
8
9
10
int main()
{
     //...
     if (i == 2)
     {
          a = 1;
     }
     //...
     return 0;
}


There is more stuff to point out, but it's stuff you will learn in the tutorial - such as using switch() - so I won't explain it here, you will eventually learn it yourself. I'm telling you this because it doesn't come in the documentation, and these are errors that happen usually to beginners and someone has to point them :)
no i'm not outputting an integer where it is happening at
please someone help!!!!!!!
Reposting OP's code with more sensible spacing:
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
# include <iostream>
# include <string>
using namespace std;

int main()
{
    string name;
    string species;
    int str = 2;
    int hp = 10;
    int def = 1;
    int alignment = 0;
    string alignmentt;
    int intl = 1;
    int maxmp = 0;
    int mpregen = 0;
    int mp = 0;
    string weapon;
    int randnum;
    string proffesion;

    cout << "in a world where games with graphics ruled a 13 year old programmer dreamed of text based games being great again, so he created this:" << endl;
    system("pause");
    system("CLS");

    cout << "enter your name" << endl;
    cin >> name;
    system("CLS");

    cout << "hello " << name << ", lets make you a character" << endl;
    system ("pause");
    system ("CLS");

    randnum = rand () % 5 + 1;
    if(randnum == 1) species = "orc" && str++ && alignment--;
    if(randnum == 2) species = "gnome" && intl++ && alignment++;
    if(randnum == 3) species = "elf" && maxmp++ && alignment++;
    if(randnum == 4) species = "human" && hp++;
    if(randnum == 5) species = "goblin" && def++ && alignment--;

    randnum = rand () % 5 + 1;
    if(randnum == 1) proffesion = "barbarian" && str++ && alignment--;
    if(randnum == 2) proffesion = "wizard" && intl++ && maxmp++ && mpregen++ && alignment++;
    if(randnum == 3) proffesion = "healer" && hp++ && alignment++;
    if(randnum == 4) proffesion = "paladin" && str++ && alignment++;
    if(randnum == 5) proffesion = "rouge" && def++ && alignment--;

    if(alignment > 0) alignmentt = "good";
    if(alignment < 0) alignmentt = "evil";
    if(alignment == 0) alignmentt = "nuetral";

    cout << "done!!, you are a " << alignmentt << " " << species << " " << proffesion << " with " << str << " strength " << intl << " intelligence " << hp << " health and " << def << " defense" << endl;

    system("pause");
    system("CLS");
}
closed account (9wqjE3v7)
Hi,

Also, that is not the correct way of using '&&', this is the bitwise operator (known as *AND) and should only be used when comparing two expressions.

What should be correct is:

1
2
3
4
5
if (randnum == 1) {
    proffesion = "barbarian";
    str++; 
    alignment--;
}

Last edited on
I don't believe you're using the '&&' operator properly. It doesn't mean "Do each of the things I've &&'ed together" (although that may be a side effect as long as each statement is interpreted the same as a boolean 'true').
Maybe instead of this:
1
2
3
4
5
6
    randnum = rand () % 5 + 1;
    if(randnum == 1) species = "orc" && str++ && alignment--;
    if(randnum == 2) species = "gnome" && intl++ && alignment++;
    if(randnum == 3) species = "elf" && maxmp++ && alignment++;
    if(randnum == 4) species = "human" && hp++;
    if(randnum == 5) species = "goblin" && def++ && alignment--;

you mean this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    randnum = rand () % 5 + 1;
    if(randnum == 1)
    {
        species = "orc";
        str++;
        alignment--;
    }
    if(randnum == 2)
    {
        species = "gnome";
        intl++;
        alignment++;
    }
    // etc... 


If this better reflects your intent, look up the 'switch' statement. (http://www.cplusplus.com/doc/tutorial/control/ towards the bottom)
Consider this code:
1
2
3
4
    int str = 2;
    int alignment = 0;
    
    cout << ("orc" && str++ && alignment--);

Output:
0

Now look at this:
1
2
3
    string species;

    species = "orc" && str++ && alignment--;

what will the resulting value of species be?

Edit: Well, I can't even get that second example to compile.

I'm guessing it might have been the intention to do this:
1
2
3
4
5
6
    if (randnum == 1)
    {
        species = "orc";
        str++;
        alignment--;
    }
Last edited on
should only be used when comparing two expressions.

Screw conventions. As long as the code does what it's author intends for it to do, then it is "correct"

1
2
3
4
5
6
    randnum = rand () % 5 + 1;
    if(randnum == 1) species = "orc" & str++ & alignment--;
    if(randnum == 2) species = "gnome" & intl++ & alignment++;
    if(randnum == 3) species = "elf" & maxmp++ & alignment++;
    if(randnum == 4) species = "human" & hp++;
    if(randnum == 5) species = "goblin" & def++ & alignment--;

FIXED! you just need to replace all your && signs with &.

Because, for instance...
function1() && function2()
function2() will not run if function1() happens to return 0 or false.
If you use function1() & function2(), then the function2() will run regardless of function1() being false.

you can also use "|"
Last edited on
closed account (9wqjE3v7)
^I know, I used that term in the wrong way. When I mentioned 'correct' I meant the way the operator is, in general intended to be used, which is to compare two expressions with the logical operator *AND, not COMPLEMENT/NOT (excuse my error).
Last edited on
&& is the logical and operator, used for combining two boolean conditions. The result is false unless both of the individual conditions are true, when the result is true.
closed account (9wqjE3v7)
If you use function1() & function2(), then the function2() will run regardless of function1() being false.


No, ( function1() & function2() ) is a bitwise AND operation. Lets assume the return values of both func1 are 4.

Since the result will only evaluate to true if both bits are '1':

0 0 1 0
0 0 1 0
______
0 0 1 0

The result would also evaluate to 4.
No, ( function1() & function2() ) is a bitwise AND operation. Lets assume the return values of both func1 are 4.

wtf u talking about? I am correct.
Proof:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int function1()
{
	cout<<"This is Function 1"<<endl;
	return 0;
}
int function2()
{
	cout<<"This is Function 2"<<endl;
	return 0;
}

int main()
{
	function1() & function2();
	cout<<endl;
	function1() && function2();
}


output:
1
2
3
4
This is Function 1
This is Function 2

This is function 1

...
Since the result will only evaluate to true if both bits are '1':

wrong, c/c++ sees every integer that is not 0 as true.
Last edited on
He is explaining what the '&' operator is. It is a bitwise operator for and you need to read this before being so rude. http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/


Anyways to cut the case short:

function 1 returns 0
function 2 returns 0

0 & 0 == 0
0 && 0 == 0


just like any operator

you can use them and it will evaluate even if you do not assign that result to something.

so function1() & function2() will do both functions then use the returns and set it equal to 0

so if we did

int a = function1() & function2();
a == 0;

if you use the && operator that is saying if a and b are true ( not 0 ) then return 1

so when you try and do function1() && function2()
the function1() returns 0 so it is false so it doesn't even bother with function2()

again if you did a = function1() && function2() then a == 0


Now lets say they both return 4 instead

function1() & function2() == 4
function1() && function2() == 1

lets look at 10 for function1 and 2 for function2

function1() & function2() == 2
function1() && function2() == 1

*fixed typo.

*also you shouldn't call people out like that.


Last edited on
Using operator & in this situation is confusing. Why do an operation that has a side effect, but not do anything to capture the side effect? It makes it appear to outside readers of the code that there's potentially some mistake in the code. If the statements read better on separate lines, just put them on separate lines. Even using the comma operator reads more sensibly than using bitwise-AND.
He is explaining what the '&' operator is. It is a bitwise operator for and you need to read this before being so rude. http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/.

I never said it wasn't, so I assumed he was disagreeing with what i previously posted, the sentence that he quoted. Seems logical enough...

also you shouldn't call people out like that.

oh the irony!

Using operator & in this situation is confusing. Why do an operation that has a side effect, but not do anything to capture the side effect? It makes it appear to outside readers of the code that there's potentially some mistake in the code. If the statements read better on separate lines, just put them on separate lines. Even using the comma operator reads more sensibly than using bitwise-AND.

Its mostly just personal preference. I find it helpful to keep an open mind when reading code.

It seems silly how a lot of people are making statements that are similar to... "Your coding style is different from mine, therefore it must be wrong! MY CODING STYLE IS PERFECT SO EVERYONE SHOULD COPY ME!"

Who knows, you might learn something new that you didn't know you could do in C++ before... (in this instance, using & in a way that is unfamiliar/unconventional).
Last edited on
@threeright:
This isn't Perl. In Perl you can get away with using whatever underhanded trick you'd like to shorten programs and express complicated functionality in smaller spaces.

However, this is C++. There's a time and place for breaking conventions in this language, and breaking them requires a very keen sense of judgment. Helping someone who is just starting to program with their code is neither the time nor place.

Please avoid being so rude in the future.

-Albatross

This isn't Perl. In Perl you can get away with using whatever underhanded trick you'd like to shorten programs and express complicated functionality in smaller spaces.

However, this is C++. There's a time and place for breaking conventions in this language, and breaking them requires a very keen sense of judgment. Helping someone who is just starting to program with their code is neither the time nor place.

Why not? I don't see why a code that runs and does exactly what the author intends for the code to do should be considered wrong.

Right = "code works the way it is intended"
Wrong = "code doesn't work the way it is intended"

What is this "Keen sense of judgement" supposed to mean anyways? This statement has practically no meaning. Too vague, generalized term. Not specific enough.

Please avoid being so rude in the future.

Oh the irony
Last edited on
I don't see how a code that runs and does exactly what the author intends for the code to do is wrong.

Because code isn't always written merely for yourself. If you're writing code only for yourself, then do whatever you please, and typically, Perl programs are written only for the programmer (hence my statement).

However, in the case where anyone else will have to read or use your code (professor, peer, boss, open-source community, whoever) and you want them to think anything decent of it, then you need to make an effort to follow conventions.

"Please avoid being so rude in the future." Oh the irony

I wasn't being condescending or rude. The only one being rude here is you.

-Albatross
Last edited on
What everyone is rying to say threeright is that the OP was not trying to do what you are saying. He was trying to put multiple statements (block) inside of an if statement.
Pages: 12