Oznium.com Store
Oznium Forum
The value of this forum is in the interaction with your fellow LED lighting enthusiasts.
Register today! - It is FREE and quick

Need some C Plus Help

Author
Message
Tim

Joined: Nov 16 2003
Posts: 10795
Location: Kalamazoo, MI


Are you sure you want to delete this post?
  
Post Wed Feb 14, 2007 8:09 pm

Ok, long story short class was canceled last week and of course I didn't use the extra week to figure out my project. I'm looking for some help creating this program that randomly displays a set of names, the names of the students in class, and should not display the person's name more than once. He wanted us to use an array to write this program. I will give you 100 Oznium points if you can help me out with this/do it for me. It's due tomorrow icon_cry.gif .
Aken

Joined: Feb 12 2003
Posts: 10885


Are you sure you want to delete this post?
  
Post Wed Feb 14, 2007 8:15 pm

I could do it in PHP icon_biggrin.gif
justin

Joined: Jan 15 2004
Posts: 5061
Location: Madison


Are you sure you want to delete this post?
  
Post Wed Feb 14, 2007 8:17 pm

how i would do it, may or may not be the right way.

array should contain values for a unique identifier, first name, last name, and a boolean "used" value.

im guessing the program shouldnt just know the number of names in the array, so it will have to check the array for the number of uid's (20 lets say). it will generate a random number, check if that name has been used, then output.
Tim

Joined: Nov 16 2003
Posts: 10795
Location: Kalamazoo, MI


Are you sure you want to delete this post?
  
Post Wed Feb 14, 2007 8:19 pm

Eric - That would do me no good icon_sad.gif

I can't figure this **** out. I know I have to assign each name to a number, 0 - 14 or w/e. I think there are 15 people in the class, I've got the list in the other room. I just don't know how to display it randomly in the console.
Aken

Joined: Feb 12 2003
Posts: 10885


Are you sure you want to delete this post?
  
Post Wed Feb 14, 2007 8:23 pm

justin

Joined: Jan 15 2004
Posts: 5061
Location: Madison


Are you sure you want to delete this post?
  
Post Wed Feb 14, 2007 8:27 pm

names are assigned a number.

10 your random number generator generates a number.
20 that random number just so happens to be assigned to mr. bilbo baggins.
30 mr baggins shold now have a "true" statement assigned to him, saying hes been used.
40 now print the name.
50 goto 10
Tim

Joined: Nov 16 2003
Posts: 10795
Location: Kalamazoo, MI


Are you sure you want to delete this post?
  
Post Wed Feb 14, 2007 8:41 pm

What?

Yeah, I don't belong in this class. I don't understand one bit.
Brandon

Joined: Jun 04 2003
Posts: 4189
Location: St. Louis, MO


Are you sure you want to delete this post?
  
Post Wed Feb 14, 2007 8:59 pm

I wanna help! I don't know the syntax, but I can google it and help you out if needed. This is the pseudo code I'd use

First create an array in the following form:
ArrayClassmates[0] = 'Brandon';
ArrayClassmates[1] = 'Aken';
ArrayClassmates[2] = 'Timmay';
ArrayClassmates[3] = 'Justin';
ArrayClassmates[4] = 'Philbish';

Then suffle the array. What this does it take an array and randomize the indexes (in our case 0-5). This way you don't have to destroy the array (known as popping), worry about duplicate indexes, or temporary variables:
shuffle_array($ArrayClassmates);

Then all we have to do is display them one at a time:
foreach ($ArrayClassmates as $name) {
print $name;
}
Brandon

Joined: Jun 04 2003
Posts: 4189
Location: St. Louis, MO


Are you sure you want to delete this post?
  
Post Wed Feb 14, 2007 9:04 pm

Hmm, it looks like C doesn't support arrays exactly, they use ints/chars to create an array:
Code:
char classmates[4] = {'Brandon', 'Aken', 'Tim', 'Justin', 'Philbish'}


And then going by the link Aken provided, your next step would be something very odd and not a real life line of code:
Code:
random_shuffle(classmates, classmates+4);


Then echo
Code:
cout<<classmates[0]<<classmates[1]<<classmates[2]<<classmates[3]<<endl;


What sucks is the example doesn't show you how to dynamically tell how big the array is. Maybe one of your books do?

EDIT: http://www.fredosaurus.com/notes-cpp/arrayptr/10arrays.html
There is no way to tell the length. This guy recommends to keep a variable that tells how big the array is. I would agree that's a good habit to get in to if there isn't a way to find the length on the fly.

EDIT 2: never use the quick posting when writing code icon_biggrin.gif


Last edited by Brandon on Wed Feb 14, 2007 9:08 pm, edited 3 times in total
Tim

Joined: Nov 16 2003
Posts: 10795
Location: Kalamazoo, MI


Are you sure you want to delete this post?
  
Post Wed Feb 14, 2007 9:05 pm

17 mother **** errors in my build. **** C

ooo...Brandon posted. Lemme redo my code...
Tim

Joined: Nov 16 2003
Posts: 10795
Location: Kalamazoo, MI


Are you sure you want to delete this post?
  
Post Wed Feb 14, 2007 9:10 pm

I took a look in my book and there's one paragraph on random_shuffle() Algorithms, no examples. What a **** joke.
Brandon

Joined: Jun 04 2003
Posts: 4189
Location: St. Louis, MO


Are you sure you want to delete this post?
  
Post Wed Feb 14, 2007 9:13 pm

Google to the rescue. Did my code work?
Tim

Joined: Nov 16 2003
Posts: 10795
Location: Kalamazoo, MI


Are you sure you want to delete this post?
  
Post Wed Feb 14, 2007 9:15 pm

I wonder why I can't copy and selected text in this thread...

Honestly, I don't know. I don't even know where to begin man. I'll I have is...

int main()

{


return o;
}

I don't even know where to insert your code. This is so over my head, nothing makes sense. This makes HTML look easy as pie.
Brandon

Joined: Jun 04 2003
Posts: 4189
Location: St. Louis, MO


Are you sure you want to delete this post?
  
Post Wed Feb 14, 2007 9:20 pm

I don't know how to compile C++ either. Can't you just copy and paste my code right after the open curly bracket?

Code:
int main()
{
   char classmates[4] = {'Brandon', 'Aken', 'Tim', 'Justin', 'Philbish'}
   
   random_shuffle(classmates, classmates+4);
   
   cout<<classmates[0]<<classmates[1]<<classmates[2]<<classmates[3]<<endl;

   return o;
}
Tim

Joined: Nov 16 2003
Posts: 10795
Location: Kalamazoo, MI


Are you sure you want to delete this post?
  
Post Wed Feb 14, 2007 9:21 pm

It says "Too many characters in constant" on line...

First error in a list of 9.

Code:
   char classmates[4] = {'Brandon', 'Aken', 'Tim', 'Justin', 'Philbish'}


Ahhh...I'm getting a headache.
Brandon

Joined: Jun 04 2003
Posts: 4189
Location: St. Louis, MO


Are you sure you want to delete this post?
  
Post Wed Feb 14, 2007 9:27 pm

what a funny error. It wouldn't have to do with the fact I left the semicolon off at the end would it?
Tim

Joined: Nov 16 2003
Posts: 10795
Location: Kalamazoo, MI


Are you sure you want to delete this post?
  
Post Wed Feb 14, 2007 9:30 pm

No, I tried it with with a semicolon and it still gave me that error. I did a search for that error on google and came up with, well of course 30 million results, the most I checked were all in a foreign language.

Code:
c:\documents and settings\tim\my documents\visual studio 2005\projects\names\names\names.cpp(11) : error C2015: too many characters in constant
c:\documents and settings\tim\my documents\visual studio 2005\projects\names\names\names.cpp(11) : warning C4305: 'initializing' : truncation from 'int' to 'char'
c:\documents and settings\tim\my documents\visual studio 2005\projects\names\names\names.cpp(11) : warning C4309: 'initializing' : truncation of constant value
c:\documents and settings\tim\my documents\visual studio 2005\projects\names\names\names.cpp(13) : error C3861: 'random_shuffle': identifier not found
c:\documents and settings\tim\my documents\visual studio 2005\projects\names\names\names.cpp(15) : error C2065: 'cout' : undeclared identifier
c:\documents and settings\tim\my documents\visual studio 2005\projects\names\names\names.cpp(15) : error C2065: 'endl' : undeclared identifier
c:\documents and settings\tim\my documents\visual studio 2005\projects\names\names\names.cpp(17) : error C2561: 'main' : function must return a value


I've never gotten errors about "cout' and "endl" before. I don't get what's going on...
Brandon

Joined: Jun 04 2003
Posts: 4189
Location: St. Louis, MO


Are you sure you want to delete this post?
  
Post Wed Feb 14, 2007 9:40 pm

Try breaking it up?
Code:
char classmates[4];

classmates[0] = 'Brandon';
classmates[1] =  'Aken';
classmates[2] = 'Tim';
classmates[3] = 'Justin';
classmates[4] = 'Philbish';


EDIT: Looks like it has to do w/ the single quotes. You can use the curly bracket method above and try double quotes: " ... " or try listing them individually which may be cleaner and easier to edit later.


Last edited by Brandon on Wed Feb 14, 2007 9:46 pm, edited 1 time in total
Tim

Joined: Nov 16 2003
Posts: 10795
Location: Kalamazoo, MI


Are you sure you want to delete this post?
  
Post Wed Feb 14, 2007 9:45 pm

Okay, I changed "Brandon" to "Tim" and it got rid of that error. Apparently it can only be three characters long..
Brandon

Joined: Jun 04 2003
Posts: 4189
Location: St. Louis, MO


Are you sure you want to delete this post?
  
Post Wed Feb 14, 2007 9:50 pm

try a string array instead of a char array?
http://www.thescripts.com/forum/thread160822.html
Code:
string array[] = {"This", "is", "a", "test", "everyone.", "stop"};
Tim

Joined: Nov 16 2003
Posts: 10795
Location: Kalamazoo, MI


Are you sure you want to delete this post?
  
Post Wed Feb 14, 2007 10:02 pm

Code:
string array classmates[2] = {'Tim', 'Aken'};


Giving my problems...
Brandon

Joined: Jun 04 2003
Posts: 4189
Location: St. Louis, MO


Are you sure you want to delete this post?
  
Post Wed Feb 14, 2007 10:07 pm

remove the word array and use double quotes:
Code:
string classmates[2] = {"Tim", "Aken"};


To explain this, "string" is the data type. In C++ you have to typecast and initiate all variable before using them.
"classmates" is the name of the variable
"[2]" signifies that this is an array with a maximum size of 2 (in C++ you can specify an array's site and not have it filled. You could for all practicality use [99])(and 2 actually means 3 because arrays start their indexes at 0 (eg. 0, 1, 2))
{"...", "..."} is a quick way to load the string array. Another way to do this I think is classmates[] = "Tim"; classmates[] = "Aken";
Tim

Joined: Nov 16 2003
Posts: 10795
Location: Kalamazoo, MI


Are you sure you want to delete this post?
  
Post Wed Feb 14, 2007 10:14 pm

Code:
'string' : undeclared identifier


It's not liking the word string...

Code:
#include "stdafx.h"
#include "algorithm"
#include "vector"
#include "iostream"

int main()
{
   string classmates[2] = {"Tim", "Phil"};
   
   random_shuffle(classmates, classmates 4);
   
   cout <<classmates[0]<<classmates[1]<<classmates[2]<<classmates[3]<<endl;

   return 0;
}
HoolaKinG

Joined: Apr 24 2003
Posts: 938


Are you sure you want to delete this post?
  
Post Wed Feb 14, 2007 10:16 pm

Here is what I would do...(assuming that you just want to stick everything in the main() and not bother with classes and such - not sure how advanced you are...if you want anything explained, let me know, much better to understand how/why things were done). Not sure why he would have you use arrays and not vectors, but oh well

Code:


#include <cstdlib>
#include <string>
#include <iostream>

int main(String[] args)
{
   int size = 100;
   String[size] studentNames = blahblahblah;
   String[size] studentNamesUsed = NULL;
   int used = 0;

   int x, randomIndex, y;
   int flag = 0;

   for (x = 0; x < size; x++)
   {
      while (flag == 0)
      {
         randomIndex = rand_int(0, size - 1);
         for (y = 0; y < used; y++)
         {
            if (studentNamesUsed[y] != studentNames[randomIndex])
               flag = 1;
         }
      }
      cout << "Student " << x + 1 << ": " << studentNames[randomIndex];
      studentNamesUsed[used] = studentNames[randomIndex];
      flag = 0;
      used++;
   }
   return 0;
}


This was done pretty quick, but I think it should work. It kind of is a force method - it isn't very elegantly done at all...if I had more time, or could use vectors, it would be a lot prettier. But, it should work.

[EDIT] I should read posts before replying - Tim's wouldn't work because #include <string> isn't in there - I don't know anything about the random_shuffle function though
Brandon

Joined: Jun 04 2003
Posts: 4189
Location: St. Louis, MO


Are you sure you want to delete this post?
  
Post Wed Feb 14, 2007 10:23 pm

ugh, I don't understand C at all. I'm sure it would help a lot of if I've ever took a class or opened a book, but at first glance it doesn't seem like the elegant language I know it is.
Tim

Joined: Nov 16 2003
Posts: 10795
Location: Kalamazoo, MI


Are you sure you want to delete this post?
  
Post Wed Feb 14, 2007 10:24 pm

Yeah, I added..

Code:
#include <string>


to what I had before and I still get 9 errors. I can't even think straight.
HoolaKinG

Joined: Apr 24 2003
Posts: 938


Are you sure you want to delete this post?
  
Post Thu Feb 15, 2007 6:11 am

It can be elegant - that solution I posted was just a brute force type of thing. If I took my time and used vectors, it would be about half that size and a lot nicer looking.
Dougan

Joined: Jul 18 2004
Posts: 140
Location: Steven's Point, WI


Are you sure you want to delete this post?
  
Post Thu Feb 15, 2007 11:02 am

Tim wrote:


I've never gotten errors about "cout' and "endl" before. I don't get what's going on...


I'm probably too late anyway, but...

I've never used visual studio for C/C++, but I know with most compilers that can be solved by putting "using namespace std;" at the beginning of your program, after your includes and such.

It's kind of a no-no in the programming world, but, if you're writing a small program and are learning, it will make everything a lot easier for you.

"std" is a namespace. cout and endl are part of the std namespace. C++ (and most good OO languages) use namespaces, or something of the like. The idea is that eventually two libraries will have the same identifier, so you can specify which one you are using. Ideally, you should be putting "std::cout" every time, but, since you aren't writing a complicated program, and it's only using one namespace, you can just put "using namespace std" and it'll handle it for you.

Good luck, hope you got everything figured out.

Brandon, aren't/weren't you a Computer Science major? They didn't teach you C/C++?
Tim

Joined: Nov 16 2003
Posts: 10795
Location: Kalamazoo, MI


Are you sure you want to delete this post?
  
Post Thu Feb 15, 2007 11:06 am

^ I bet that's what my problem was, I remember using that line of code in the other two programs I made. I'll check it out in a few as soon as I boot up the laptop. Went into class today, two classes today same teacher, and the teacher said class is canceled tonight. So basically I have another week to work on the homework. I lucked out so much there. Gave me another assignment though, has to do with vectors. I don't know what the hell a vector is but except me to ask it in a few days once I finish this array assignment, lol.
HoolaKinG

Joined: Apr 24 2003
Posts: 938


Are you sure you want to delete this post?
  
Post Thu Feb 15, 2007 2:55 pm

Vectors will make your life a lot easier, you can add and remove things from them unlike arrays where you have to make the whole thing over again.
Dougan

Joined: Jul 18 2004
Posts: 140
Location: Steven's Point, WI


Are you sure you want to delete this post?
  
Post Thu Feb 15, 2007 4:14 pm

Agreed, vectors are a useful part of C++. I'm assuming you're using STL vectors. You should try and get a head start on it, STL and templates are a little tricky to get the hang of and you'll probably run into some errors that are hard to debug.

Edit: Vectors in C? Haha.


Last edited by Dougan on Thu Feb 15, 2007 5:45 pm, edited 1 time in total
tjmonk15

Joined: Feb 12 2007
Posts: 8


Are you sure you want to delete this post?
  
Post Thu Feb 15, 2007 5:02 pm

eh, if you still haven't figured it out, i can help, limme boot up MSVS and write something real quick. hang on...
tjmonk15

Joined: Feb 12 2007
Posts: 8


Are you sure you want to delete this post?
  
Post Thu Feb 15, 2007 5:25 pm

here you go:
Code:


// Ignore this line unless you are using MSVS 2005 or your main function has a TCHAR somewhere in it
#include "TCHAR.h"

#include <iostream>
#include <time.h>

#define NUM_NAMES      10         // Change to how ever many you need

// I use MSVS 2005, so your main function might be different
int _tmain(int argc, _TCHAR* argv[])
{
   // Create our array of names
   char* classlist[NUM_NAMES];
   classlist[0] = "Brendom\0";      //You need the '\0' it tells the computer where the end
                           //of the string is, this is called a null-terminated string.
   classlist[1] = "Tim\0";
   classlist[2] = "Bob\0";
   classlist[3] = "Cecil\0";
   classlist[4] = "Brad\0";
   classlist[5] = "Sean\0";
   classlist[6] = "Jimbo\0";
   classlist[7] = "Joe\0";
   classlist[8] = "Sally\0";
   classlist[9] = "Mary\0";

   // Seed the random-number generator with current time so that
   // the numbers will be different every time we run.
   srand( (unsigned)time( NULL ) );

   // Shuffle the names
   for( int i=0; i<10; i++ )      //We are going to shuffle the list ten times
   {
      for( int j=0; j<NUM_NAMES; j++ )
      {
         // Generate the random number. rand() give a number between 0
         // and RAND_MAX. use this formula to scale it down to the
         // range we need.
         int index = (((double) rand() /
                         (double) RAND_MAX) * NUM_NAMES);

         // Swap the random name with the current name.
         char* temp = classlist[index];
         classlist[index] = classlist[j];
         classlist[j] = temp;
      }
   }

   // Print out the names
   for( int i=0; i<NUM_NAMES; i++ )
      std::cout << classlist[i] << std::endl;

   // These two lines just make the program pause and wait for you so you can read the
   // list. Enter a number or letter and press enter to end the program.
   char c;
   std::cin >> c;

   return 0;
}


works for me, just change the names there and add anymore you need following the same format. And don't forget to change NUM_NAMES at the top!

Hope this helps icon_smile.gif
-Monk
HoolaKinG

Joined: Apr 24 2003
Posts: 938


Are you sure you want to delete this post?
  
Post Thu Feb 15, 2007 5:49 pm

^ A much nicer solution than mine was.
Brandon

Joined: Jun 04 2003
Posts: 4189
Location: St. Louis, MO


Are you sure you want to delete this post?
  
Post Thu Feb 15, 2007 6:13 pm

hahah, wow. C sucks! j/p :D

And I was a Comp Sci major, and I learned Java in my classes. Now I use strictly PHP -- learning OOP w/ PHP 5 now
Tim

Joined: Nov 16 2003
Posts: 10795
Location: Kalamazoo, MI


Are you sure you want to delete this post?
  
Post Thu Feb 15, 2007 6:29 pm

Wow man, works perfectly. Just have to make a "Would you like to reorder the list?" text output and a loop and I'm good. You have no idea how happy this makes me. It's like, a giant burden off my back haha.

Is there anyway I can exclude "std::" before cout and endl? Can I declare it up top or anything?

This is what I currently have..

Code:
// Ignore this line unless you are using MSVS 2005 or your main function has a TCHAR somewhere in it
#include "TCHAR.h"

#include <iostream>
#include <time.h>

#define NUM_NAMES      10



int _tmain(int argc, _TCHAR* argv[])


   
{
   char answer;

   // Create our array of names
   char* classlist[NUM_NAMES];
   classlist[0] = "Brendom\0";      //You need the '\0' it tells the computer where the end
                                    //of the string is, this is called a null-terminated string.
   classlist[1] = "Tim\0";
   classlist[2] = "Bob\0";
   classlist[3] = "Cecil\0";
   classlist[4] = "Brad\0";
   classlist[5] = "Sean\0";
   classlist[6] = "Jimbo\0";
   classlist[7] = "Joe\0";
   classlist[8] = "Sally\0";
   classlist[9] = "Mary\0";

   // Seed the random-number generator with current time so that
   // the numbers will be different every time we run.
   srand( (unsigned)time( NULL ) );

   // Shuffle the names
   for( int i=0; i<10; i++ )      //We are going to shuffle the list ten times
   {
      for( int j=0; j<NUM_NAMES; j++ )
      {
         // Generate the random number. rand() give a number between 0
         // and RAND_MAX. use this formula to scale it down to the
         // range we need.
         int index = (((double) rand() /
                         (double) RAND_MAX) * NUM_NAMES);

         // Swap the random name with the current name.
         char* temp = classlist[index];
         classlist[index] = classlist[j];
         classlist[j] = temp;
      }
   }

   // Print out the names
   for( int i=0; i<NUM_NAMES; i++ )
      std::cout << classlist[i] << std::endl;

   // These two lines just make the program pause and wait for you so you can read the
   // list. Enter a number or letter and press enter to end the program.
   
   std::cout << "Would you like to reorder the list? Y/N";
   std::cin >> answer;

   if (answer == "y")
   {
      std::cout << "You rolled a";
   }

   if (answer == "n")
   {
      std::cout << "Thank you for Playing" ;
   }

   return 0;
}


I'm getting 4 errors...

Code:
------ Build started: Project: names, Configuration: Debug Win32 ------
Compiling...
names.cpp
c:\documents and settings\tim\my documents\visual studio 2005\projects\names\names\names.cpp(45) : warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data
c:\documents and settings\tim\my documents\visual studio 2005\projects\names\names\names.cpp(64) : error C2446: '==' : no conversion from 'const char *' to 'int'
        There is no context in which this conversion is possible
c:\documents and settings\tim\my documents\visual studio 2005\projects\names\names\names.cpp(64) : error C2040: '==' : 'int' differs in levels of indirection from 'const char [2]'
c:\documents and settings\tim\my documents\visual studio 2005\projects\names\names\names.cpp(69) : error C2446: '==' : no conversion from 'const char *' to 'int'
        There is no context in which this conversion is possible
c:\documents and settings\tim\my documents\visual studio 2005\projects\names\names\names.cpp(69) : error C2040: '==' : 'int' differs in levels of indirection from 'const char [2]'
Build log was saved at "file://c:\Documents and Settings\Tim\My Documents\Visual Studio 2005\Projects\names\names\Debug\BuildLog.htm"
names - 4 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


I'm confused. I pulled that If part from another project thinking it should work and it's not liking those =='s.


Last edited by Tim on Thu Feb 15, 2007 8:16 pm, edited 1 time in total
Silentdawn

Joined: Apr 12 2005
Posts: 1481
Location: Hockessin, Delaware


Are you sure you want to delete this post?
  
Post Thu Feb 15, 2007 8:16 pm

another thing you could do to randomize something is using #include <math.h> I believe. then you can use "rand". Just a suggestion...thats what I did last year in c plus plus
Tim

Joined: Nov 16 2003
Posts: 10795
Location: Kalamazoo, MI


Are you sure you want to delete this post?
  
Post Thu Feb 15, 2007 8:23 pm

Oh, thanks man. I used the #include <math.h> in a previous program that added some dice totals. Wasn't quite sure how to adapt it to this instance though.
Tim

Joined: Nov 16 2003
Posts: 10795
Location: Kalamazoo, MI


Are you sure you want to delete this post?
  
Post Thu Feb 15, 2007 9:17 pm

Got it

Code:
// Ignore this line unless you are using MSVS 2005 or your main function has a TCHAR somewhere in it
#include "TCHAR.h"

#include <iostream>
#include <time.h>

#define NUM_NAMES      10
using std::cout;
using std::endl;



int _tmain(int argc, _TCHAR* argv[])


   
{

   char answer;
   do
   
   {
   // Create our array of names
   char* classlist[NUM_NAMES];
   classlist[0] = "Brendom\0";      //You need the '\0' it tells the computer where the end
                                    //of the string is, this is called a null-terminated string.
   classlist[1] = "Tim\0";
   classlist[2] = "Bob\0";
   classlist[3] = "Cecil\0";
   classlist[4] = "Brad\0";
   classlist[5] = "Sean\0";
   classlist[6] = "Jimbo\0";
   classlist[7] = "Joe\0";
   classlist[8] = "Sally\0";
   classlist[9] = "Mary\0";

   // Seed the random-number generator with current time so that
   // the numbers will be different every time we run.
   srand( (unsigned)time( NULL ) );

   // Shuffle the names
   for( int i=0; i<10; i   )      //We are going to shuffle the list ten times
   {
      for( int j=0; j<NUM_NAMES; j   )
      {
         // Generate the random number. rand() give a number between 0
         // and RAND_MAX. use this formula to scale it down to the
         // range we need.
         int index = (((double) rand() /
                         (double) RAND_MAX) * NUM_NAMES);

         // Swap the random name with the current name.
         char* temp = classlist[index];
         classlist[index] = classlist[j];
         classlist[j] = temp;
      }
   }

   // Print out the names
   for( int i=0; i<NUM_NAMES; i   )
      std::cout << classlist[i] << std::endl;

   // These two lines just make the program pause and wait for you so you can read the
   // list. Enter a number or letter and press enter to end the program.
   
   std::cout << "\nWould you like to reorder the list? Y/N ";
   std::cin >> answer;

   }
   
   while (answer == 'y');

   std::cout << "OK, Bye";
   

   return 0;
}
Brandon

Joined: Jun 04 2003
Posts: 4189
Location: St. Louis, MO


Are you sure you want to delete this post?
  
Post Fri Feb 16, 2007 5:36 pm

I HOPE you removed some of those comments before turning it in! lol.
Tim

Joined: Nov 16 2003
Posts: 10795
Location: Kalamazoo, MI


Are you sure you want to delete this post?
  
Post Sun Feb 18, 2007 6:47 pm

Didn't turn it in yet, it's due on Thursday but yes I will remove the comments lol.
Tim

Joined: Nov 16 2003
Posts: 10795
Location: Kalamazoo, MI


Are you sure you want to delete this post?
  
Post Wed Feb 21, 2007 9:15 pm

Okay, I've got another question. I've got the basic program down, just an error I'm unfamiliar with.

Program is supposed to list the names, then prompt the user if they're ready for battle. Then it's supposed to randomly select a name and display that they died, then display a new list without that person in it. Prompts the user again if they want to battle until there is only one name left, declared the winner.

The error...

Code:
c:\documents and settings\tim\my documents\visual studio 2005\projects\deathtrg\deathtrg\deathtrg.cpp(47) : error C2181: illegal else without matching if


Code:
// DeathTRG.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <ctime>
#include <algorithm>
using namespace std;
char answer;


int _tmain(int argc, _TCHAR* argv[])
{

vector<string> names;
names.push_back("Tim");
names.push_back("Bob");
names.push_back("Bill");

vector<string>::iterator myInterator;
vector<string>::const_iterator iter;

cout << "\nNames:\n";
for (iter = names.begin(); iter != names.end();   iter)
cout << *iter << endl;

cout << "\nRandom Names:\n";
srand(time(0));
random_shuffle(names.begin(), names.end());
for (iter = names.begin(); iter != names.end();   iter)
cout << *iter << endl;



   cout << "\nReady for Battle? (y/n)";
   cin >> answer;

if (answer == 'y')

cout << "Bill has died";
   names.erase((names.begin()   2));
   cout << "\nPlayers Left:\n";
   for (iter = names.begin(); iter != names.end();   iter)
      cout << *iter << endl;
else


cout << "\nBye";

   return 0;
}

Dougan

Joined: Jul 18 2004
Posts: 140
Location: Steven's Point, WI


Are you sure you want to delete this post?
  
Post Wed Feb 21, 2007 9:25 pm

if (answer == 'y')

cout << "Bill has died";
names.erase((names.begin() 2));
cout << "\nPlayers Left:\n";
for (iter = names.begin(); iter != names.end(); iter)
cout << *iter << endl;
else


cout << "\nBye";

-------------------
should be:

if (answer == 'y'){

cout << "Bill has died";
names.erase((names.begin() 2));
cout << "\nPlayers Left:\n";
for (iter = names.begin(); iter != names.end(); iter)
cout << *iter << endl;
}else


cout << "\nBye";


The error is exactly what your error message says it was.
Tim

Joined: Nov 16 2003
Posts: 10795
Location: Kalamazoo, MI


Are you sure you want to delete this post?
  
Post Wed Feb 21, 2007 9:31 pm

Oh, okay. I forgot the { }'s, thanks. Now I just need to figure out how to randomly select a name instead of me declaring a name to "die" and be taken off the list.

I'm pretty proud of myself, I wrote the whole thing myself so far and I'm almost done. Just need to get the random thing down and put everything together. I'm...learning. icon_eek.gif
The time now is Sun Jul 15, 2018 9:38 pm
Page 1 of 1