Thread: Another more complex problem,

  1. #1
    Interested Newbie
    Join Date
    Sep 2004
    Location
    Sweden
    Posts
    51

    Another more complex problem,

    I've made(with tonz of help from you guys) a program that prints random numbers.

    Now it is so that I want to somehow change these numbers!

    So when I for example get a 1 I want to change that into for example the word bear, so I can cout it out like bear if the program returned 1.

    (My program is limited to only using the 1-9 numbers)

    I hope you understand me, I'm not to good in english

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Where's the code?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Interested Newbie
    Join Date
    Sep 2004
    Location
    Sweden
    Posts
    51
    Code:
    #include <conio.h>
    #include <cstdlib>
    #include <iostream>
    #include <stdio.h>
    #include <time.h>
    
    using namespace std;
    
    int number_generator()
    {
    	return rand()%10+1;	
    }
    
    int main()
    {
    	srand(time(NULL));
    	
    	do
    	{
    	//Val av attack
    	int input;
    	cout << "Vad vill du attackera med?" << endl;
    	cout << "1. Sax" << endl;
    	cout << "2. Sten" << endl;
    	cout << "3. Påse" << endl;
    	cin >> input;
    
    	}
    		while( yorn == 'y');
    
    	getch();
    	return 0;
    }
    As you see its not completed. But you do get what I want?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    char *creatures[] = {
    	"Sax",
    	"Sten",
    	"Påse",
    	// some more
    };
    cout << creatures[number_generator()] << endl;
    cout <<
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Interested Newbie
    Join Date
    Sep 2004
    Location
    Sweden
    Posts
    51
    Alright I didn't get much of that but I'll try!

  6. #6
    Interested Newbie
    Join Date
    Sep 2004
    Location
    Sweden
    Posts
    51
    What I really want is something like this:

    A function returns a variable with a number. I want , preferably in a function, to make that number into a word like "sax". So it wont print the number, it should print the word!

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Salem used an array. So, characters[0] is Sax, characters[1] is Sten, and characters[2] is Påse.

    Then, you use characters[random].

    Note that because each string is already an array, you have an "array-of-arrays".

    These are not array variables:
    X1
    X2
    X3

    These are array variables:
    X[1]
    X[2]
    X[3]
    Now, we can have X[n], where n is another variable. If n is random, we will randomly select one of the variables in the array.

    Another way to do this is with if-statements, or switch.

    [EDIT] - Salem's solution should work if you get the syntax right... You should be able to display the "word".
    Last edited by DougDbug; 09-29-2004 at 01:05 PM.

  8. #8
    Interested Newbie
    Join Date
    Sep 2004
    Location
    Sweden
    Posts
    51
    I see . I'll do some testing tomorrow then I'll return here to report my succes/failure!

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    You could also use a simple switch statement. For example:
    Code:
    switch (number)
    {
        case 1:
            cout << "Sax";
            break;
        case 2:
            cout << "Sten";
            break;
        case 3:
            cout << "Påse";
            break;
        default:
            cout << "ERROR";
            break;
    }
    You could also put that in a function and return the string instead of using cout to display it. The array idea is a little cleaner, but if you don't know arrays, it might be easier to just use a switch or even if/else statements.

  10. #10
    Interested Newbie
    Join Date
    Sep 2004
    Location
    Sweden
    Posts
    51
    Thanks a lot !! =D =D Going to try this later on today after class!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. problem with complex numbers
    By dsc in forum C Programming
    Replies: 2
    Last Post: 12-12-2006, 01:18 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM