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. Help with program

Help with program

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestionlounge
6 Posts 5 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.
  • D Offline
    D Offline
    DaveE9th
    wrote on last edited by
    #1

    I'm having trouble getting the following program to compile. I'm trying to write a program that will generate a random number (say our of 150 numbers for example). What am I doing wrong? My error message is 2601. I'm defining something wrong it appears. #include <stdlib.h> #include <iostream.h> void main() { int nrand (int n) { int x = RAND_MAX / n; int r; do r = rand() / x; while (r >= n); cout< Thanks, Dave :-D _"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson_

    M T B 3 Replies Last reply
    0
    • D DaveE9th

      I'm having trouble getting the following program to compile. I'm trying to write a program that will generate a random number (say our of 150 numbers for example). What am I doing wrong? My error message is 2601. I'm defining something wrong it appears. #include <stdlib.h> #include <iostream.h> void main() { int nrand (int n) { int x = RAND_MAX / n; int r; do r = rand() / x; while (r >= n); cout< Thanks, Dave :-D _"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson_

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      can you reformat the listing so it is easier to read ? (use the > < buttons below ) also, use srand to regenerate the random number generator (look in msnd for a full example)


      Maximilien Lincourt "Never underestimate the bandwidth of a station wagon filled with backup tapes." ("Computer Networks" by Andrew S Tannenbaum )

      1 Reply Last reply
      0
      • D DaveE9th

        I'm having trouble getting the following program to compile. I'm trying to write a program that will generate a random number (say our of 150 numbers for example). What am I doing wrong? My error message is 2601. I'm defining something wrong it appears. #include <stdlib.h> #include <iostream.h> void main() { int nrand (int n) { int x = RAND_MAX / n; int r; do r = rand() / x; while (r >= n); cout< Thanks, Dave :-D _"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson_

        T Offline
        T Offline
        Terry ONolley
        wrote on last edited by
        #3

        Some of the most immediate problems are your #includes aren't correct. Also (unless you have a custom stdlib.h) there is a typo - your first include should be stdlib, not cstdlib. the #includes should look like #include <stdlib.h> (actually, you probably had it correct, but because they look like HTML tags to the server processor, they got stripped). Also namespace is a C++ specific feature but in C++ you can't have locally defined functions (your int nrand(int n). You'll need to break out your nrand and call it from your main. Create a NRand class and header file and save them in their own source files. #include the header in your main source file. Then you'll need to call the class with an integer. I didn't even look at the logic contained in your loop. Try to get a good compile and then check back in!



        1 Reply Last reply
        0
        • D DaveE9th

          I'm having trouble getting the following program to compile. I'm trying to write a program that will generate a random number (say our of 150 numbers for example). What am I doing wrong? My error message is 2601. I'm defining something wrong it appears. #include <stdlib.h> #include <iostream.h> void main() { int nrand (int n) { int x = RAND_MAX / n; int r; do r = rand() / x; while (r >= n); cout< Thanks, Dave :-D _"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson_

          B Offline
          B Offline
          bugtesting
          wrote on last edited by
          #4

          Look at my modification:

          #include
          #include

          using namespace std;
          int nrand (int n)

          {

          int x = RAND_MAX / n;
          int r;

          do r = rand() / x;
          while (r >= n);
          cout<

          Allan

          J 1 Reply Last reply
          0
          • B bugtesting

            Look at my modification:

            #include
            #include

            using namespace std;
            int nrand (int n)

            {

            int x = RAND_MAX / n;
            int r;

            do r = rand() / x;
            while (r >= n);
            cout<

            Allan

            J Offline
            J Offline
            jhwurmbach
            wrote on last edited by
            #5

            Hint: if you have a space before and after the < > they get not deleted by the stupid HTML-interpreter at CP. I did find a few things in your program. First: In your main(), you need to seed the random-generator, or you will get the same list of random numbers over and over. Use srand() for that. You can find an example how to do it in the MSDN. Second: You should do the cout in your main, after the call to your nrand-function. Separates the logic in natural parts: nrand() gets you a number, but does not say what to do with it, and main() uses the number to do whatever must be done, but does not bother how to generate it. Third: Your main sould be of return type int, and you do return 0; if all went well, otherwise something different (right now cant see why you would want to return anything other than 0.


            Who is 'General Failure'? And why is he reading my harddisk?!?

            D 1 Reply Last reply
            0
            • J jhwurmbach

              Hint: if you have a space before and after the < > they get not deleted by the stupid HTML-interpreter at CP. I did find a few things in your program. First: In your main(), you need to seed the random-generator, or you will get the same list of random numbers over and over. Use srand() for that. You can find an example how to do it in the MSDN. Second: You should do the cout in your main, after the call to your nrand-function. Separates the logic in natural parts: nrand() gets you a number, but does not say what to do with it, and main() uses the number to do whatever must be done, but does not bother how to generate it. Third: Your main sould be of return type int, and you do return 0; if all went well, otherwise something different (right now cant see why you would want to return anything other than 0.


              Who is 'General Failure'? And why is he reading my harddisk?!?

              D Offline
              D Offline
              DaveE9th
              wrote on last edited by
              #6

              Thanks all, I will work with your suggestions. Dave :-D

              "The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson

              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