Integer displaying crazy numbers issue

This is a discussion on Integer displaying crazy numbers issue within the C++ Programming forums, part of the General Programming Boards category; O.K. First off I would like to explain that I am making a simple game. I check my syntax, and ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    48

    Unhappy Integer displaying crazy numbers issue

    O.K. First off I would like to explain that I am making a simple game. I check my syntax, and I check it often. I thought at first my problem only existed in my game code, but I then found out through testing that it is with all code. I am using Dev-C++ (Newest Version) and here is a sample code. Try it and you will see what I mean.
    Code:
    #include <iostream>
    using namespace std;
    int main();
    {int gold;
    gold == 45;
    cout<<"To display your current amount of gold hit the return key.";
    cin.get();
    cout<<"You currently have "<<gold<<" gold.";
    cin.get();}
    It displays 2 sometimes. At other times it displays like 21309874 or something like that. Somebody please help me!!

  2. #2
    Registered User
    Join Date
    Dec 2004
    Location
    Mercer, PA
    Posts
    25
    You are using the equality operator instead of the assignment...
    gold == 45 // wrong...
    gold = 45 // right

    The reason why it shoots strange numbers because it is trying to see if it equals 45 as in an if statement. When you declare any variable it is assigned an address and that address might contain garbage information that was used from a previous program.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Yeah, == evaluates something for example:
    Code:
    if(x==5)
         {
             Do something...
         }
    but just plain = assigns a value to a variable or character, etc.
    Code:
    int x=10;
    I used to do that alot

  4. #4
    Registered User
    Join Date
    Dec 2004
    Location
    Mercer, PA
    Posts
    25
    Its SOOOOO increadbly easy to make small syntax errors and logical errors like that. Even for intermediate and expert programmers - and you end up bashing your head off your keyboard trying to figure it out. Sometimes you need a fresh set of eyes because the longer to work at it the harder it becomes. Iv found too its easy just to walk away for a little bit then come back and take another peek.

    I have a funny story about a problem with Microsoft Visual C++ - I would have to post code and explain thought - let me know if your interested - haha. It had to do with my Energy encryption algorithm.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    48

    Talking Hm....

    I see...I am new to this, but I was once told by someone in the forums(dont remember who)to use == if you do not have a very strong wish not to. Oh well, I must have misunderstood them. As for the story, go ahead if you wish

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    48

    Question More help...

    OK...so lets say I get that figured out so it works correctly. When it displays gold, it displays the amount I meant for it to. Now lets say I want to make it so that gold can be spent on an item. I figured it would look something like
    Code:
     gold - 50 = gold
    So lets start from scratch. I want it so that the person drops 10 gold every time they enter l. I also want them to pick up 10 gold every time they enter 2. So I figure the code should resemble this:
    Code:
    #include<iostream>
    #include <string>
    using namespace std;
    int main();
    {int gold;
    string loseorgain;
    gold = 1000;
    start:
    cout<<"Enter '1' to gain 10 gold or '2' to lose 10 gold.";
    cin>> loseorgain;
    cin.ignore();
    if (loseorgain==1) {"<<gold<<" + 10 = gold}
    else if (loseorgain==2){"<<gold<<" - 10 = gold}
    else {}
    cout<<"You now have "<<gold<<" gold.";
    cin.get();
    goto start;
    Even though the code doesn't work, you should be able to get what I am asking. Thanks for the help earlier, and thanks to whoever will help me this time
    Last edited by Razorblade Kiss; 12-20-2004 at 10:17 PM. Reason: Syntax Errors in code

  7. #7
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Now lets say I want to make it so that gold can be spent on an item. I figured it would look something like
    Code:
    gold - 50 = gold
    No. The right side of the equation is always assigned to the left side, therefore your code should be

    Code:
    gold = gold - 50
    Or there's a shortcut version:

    Code:
    gold -= 50

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    48
    ok...pardon my stupidity, but how would that go in the code mentioned above?

  9. #9
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Code:
    if (loseorgain==1){
    
          gold+= 10;
    
    }else if (loseorgain==2){
    
          gold -= 10;
    }

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    48

    Errors all over the place

    OK, so I entered the code, hit compile & run. Errors, errors, errors...Where have I gone wrong? Why can't it be easy!
    Code:
    #include<iostream>
    #include <string>
    using namespace std;
    int main();
    {
    int gold, loseorgain;
    gold = 1000;
    start:
    cout<<"Enter 1 to gain gold, or 2 to lose gold.";
    cin>> loseorgain;
    cin.ignore();
    
    if (loseorgain==1){
    
               gold+= 10;}
    else if (loseorgain==2){
    
              gold-= 10;}
    Where is the freaking error?

  11. #11
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Where is the freaking error?
    You tell us. Post the error messages.

    I can see straight away that you don't even have matching }'s for each {.

    edit: You also specified int as the return type for main, yet you never return anything. Place "return 0;" at the end of your code, before you close the main function.

  12. #12
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Code:
    int main()  ; //no semicolon goes here
    That is your error. Remove it!!!!

  13. #13
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    (Remove just the semicolon)

  14. #14
    Registered User
    Join Date
    Dec 2004
    Posts
    48
    line 5:syntax error before {
    line 7:ISO C++ forbids declaration of 'gold' with no type
    line 8:syntax error before :
    line 10:syntax error before >>
    line 11: syntax error before .
    line 21: syntax error before <<
    line 22:syntax error before .

    Lots of errors...

  15. #15
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    BTW anyone else notice the program doesn't end? No escape clause as I like to say

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Crazy loss of focus issue (i think)
    By AtomRiot in forum C# Programming
    Replies: 2
    Last Post: 06-18-2007, 04:35 PM
  2. Replies: 6
    Last Post: 04-12-2002, 09:33 AM
  3. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 05:56 PM
  4. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

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