Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. weird, i dun get if()?

weird, i dun get if()?

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++questionlounge
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    can someone please help me with this piece of code. i was at work and bored so i started writing this. It should generate 9999 random numbers and count how many times it was 5 (according to me anyways). i'm really confused because i've programmed some heavy stuff already in MFC and i just dont get this. The problem is that v turns out to be 75536 every time i run it... #include #include #include #include #include #include int main(int argc, char *argv[]) { srand((unsigned)time(NULL)); int i; int v; for (i=0;i<=9999;i++) { int a = rand()%(9-0); cout << a; if(a=5) { v = v+1; } } cout << "\n"; cout << "\n"; cout << "The number of times 5 was generated:"; cout << v; cout << " / 9999\n"; getch(); return 0; } Thankya Kuni

    L M D 5 Replies Last reply
    0
    • L Lost User

      can someone please help me with this piece of code. i was at work and bored so i started writing this. It should generate 9999 random numbers and count how many times it was 5 (according to me anyways). i'm really confused because i've programmed some heavy stuff already in MFC and i just dont get this. The problem is that v turns out to be 75536 every time i run it... #include #include #include #include #include #include int main(int argc, char *argv[]) { srand((unsigned)time(NULL)); int i; int v; for (i=0;i<=9999;i++) { int a = rand()%(9-0); cout << a; if(a=5) { v = v+1; } } cout << "\n"; cout << "\n"; cout << "The number of times 5 was generated:"; cout << v; cout << " / 9999\n"; getch(); return 0; } Thankya Kuni

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      ok the includes are: stdio.h iostream.h conio.h stdlib.h time.h (yes there was one include too many :p) kuni

      1 Reply Last reply
      0
      • L Lost User

        can someone please help me with this piece of code. i was at work and bored so i started writing this. It should generate 9999 random numbers and count how many times it was 5 (according to me anyways). i'm really confused because i've programmed some heavy stuff already in MFC and i just dont get this. The problem is that v turns out to be 75536 every time i run it... #include #include #include #include #include #include int main(int argc, char *argv[]) { srand((unsigned)time(NULL)); int i; int v; for (i=0;i<=9999;i++) { int a = rand()%(9-0); cout << a; if(a=5) { v = v+1; } } cout << "\n"; cout << "\n"; cout << "The number of times 5 was generated:"; cout << v; cout << " / 9999\n"; getch(); return 0; } Thankya Kuni

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        ok the includes are: stdio.h iostream.h conio.h stdlib.h time.h (yes there was one include too many :p) kuni

        1 Reply Last reply
        0
        • L Lost User

          can someone please help me with this piece of code. i was at work and bored so i started writing this. It should generate 9999 random numbers and count how many times it was 5 (according to me anyways). i'm really confused because i've programmed some heavy stuff already in MFC and i just dont get this. The problem is that v turns out to be 75536 every time i run it... #include #include #include #include #include #include int main(int argc, char *argv[]) { srand((unsigned)time(NULL)); int i; int v; for (i=0;i<=9999;i++) { int a = rand()%(9-0); cout << a; if(a=5) { v = v+1; } } cout << "\n"; cout << "\n"; cout << "The number of times 5 was generated:"; cout << v; cout << " / 9999\n"; getch(); return 0; } Thankya Kuni

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #4

          if (a=5) Shouldn't that be if (a==5)? --Mike-- http://home.inreach.com/mdunn/ "Make sure that if you are using a blow torch that you don't set anything on fire."   -- Chris Maunder

          L 1 Reply Last reply
          0
          • M Michael Dunn

            if (a=5) Shouldn't that be if (a==5)? --Mike-- http://home.inreach.com/mdunn/ "Make sure that if you are using a blow torch that you don't set anything on fire."   -- Chris Maunder

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            no i tried that before, it gives something even more odd, v becomes negative.... kuni

            M 1 Reply Last reply
            0
            • L Lost User

              no i tried that before, it gives something even more odd, v becomes negative.... kuni

              M Offline
              M Offline
              Miroslav Rajcic
              wrote on last edited by
              #6

              Perhaps you should initialize v variable at the start of the program :) int v = 0; Miroslav Rajcic http://www.spacetide.com

              L 1 Reply Last reply
              0
              • M Miroslav Rajcic

                Perhaps you should initialize v variable at the start of the program :) int v = 0; Miroslav Rajcic http://www.spacetide.com

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                lol i just figured that out :) thx anyways :-D

                1 Reply Last reply
                0
                • L Lost User

                  can someone please help me with this piece of code. i was at work and bored so i started writing this. It should generate 9999 random numbers and count how many times it was 5 (according to me anyways). i'm really confused because i've programmed some heavy stuff already in MFC and i just dont get this. The problem is that v turns out to be 75536 every time i run it... #include #include #include #include #include #include int main(int argc, char *argv[]) { srand((unsigned)time(NULL)); int i; int v; for (i=0;i<=9999;i++) { int a = rand()%(9-0); cout << a; if(a=5) { v = v+1; } } cout << "\n"; cout << "\n"; cout << "The number of times 5 was generated:"; cout << v; cout << " / 9999\n"; getch(); return 0; } Thankya Kuni

                  D Offline
                  D Offline
                  Dean Goodman
                  wrote on last edited by
                  #8

                  Also, you never initialize v to be 0 -- probably explains why v is always 75536.

                  1 Reply Last reply
                  0
                  • L Lost User

                    can someone please help me with this piece of code. i was at work and bored so i started writing this. It should generate 9999 random numbers and count how many times it was 5 (according to me anyways). i'm really confused because i've programmed some heavy stuff already in MFC and i just dont get this. The problem is that v turns out to be 75536 every time i run it... #include #include #include #include #include #include int main(int argc, char *argv[]) { srand((unsigned)time(NULL)); int i; int v; for (i=0;i<=9999;i++) { int a = rand()%(9-0); cout << a; if(a=5) { v = v+1; } } cout << "\n"; cout << "\n"; cout << "The number of times 5 was generated:"; cout << v; cout << " / 9999\n"; getch(); return 0; } Thankya Kuni

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Try initializing v to 0.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups