How can I make random answers to user inputs in C++?

This is a discussion on How can I make random answers to user inputs in C++? within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm rather new at C++ and I was wondering if you guys could help me out with something I'm ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    2

    Question How can I make random answers to user inputs in C++?

    Hello, I'm rather new at C++ and I was wondering if you guys could help me out with something I'm trying to do. What I want to do is make it so when I type something like "apple" the cmd comes back and picks a random word that I supply it to say.

    Also how could I make it so I can keep typing words in after I get one answer, I want to be able to just keep telling it words without the CMD going "Press any key to continue" and exiting out. Thanks for any help! This is the only code I have so far:

    Code:
    // Random Words
    #include <iostream>
    #include <cmath>
    void myfun (int);
    
    
    int main ()
    {
    using namespace std;
    cout << endl;
    cout << endl;
    cout << "You: ";
    int userinput;
    cin >> userinput;
    myfun(userinput);
    system("pause");
    return 0;
    }
    
    void myfun(int x)
    {
         using namespace std;
         cout << endl;
         cout << "Your Computer: apple" << endl;
         cout << endl;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you aren't planning to type integers, then you shouldn't read in to an integer variable.

    As to the computer's response, you'll have to decide where those words should come from. Should it be related to the input somehow? Should it just choose from a random list of words? Should it always be apple?

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    2
    No its should choose from random words, I just dont know how to make it do that. And I have the integer variable because its the only way I could get it to come back with something. I get a error if I take it out.
    Last edited by Catfish0221; 01-31-2011 at 07:31 PM.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Quote Originally Posted by Catfish0221 View Post
    No its should choose from random words, I just dont know how to make it do that. And I have the integer variable because its the only way I could get it to come back with something. I get a error if I take it out.
    There's no library built into C++ that just "has a list of random words." You either need to store the words somewhere, and access them from there, or read them in from a file or something when the program starts up.

    Quote Originally Posted by Catfish0221 View Post
    Hello, I'm rather new at C++ and I was wondering if you guys could help me out with something I'm trying to do. What I want to do is make it so when I type something like "apple" the cmd comes back and picks a random word that I supply it to say.

    Also how could I make it so I can keep typing words in after I get one answer, I want to be able to just keep telling it words without the CMD going "Press any key to continue" and exiting out. Thanks for any help! This is the only code I have so far:

    Code:
    // Random Words
    #include <iostream>
    #include <cmath>
    #include <string>
    void myfun (int);
    
    using namespace std;
    
    int main ()
    {
       cout << endl;
       cout << endl;
       string userinput;
       do
       {
          cout << "You: ";
          cin >> userinput;
          myfun(userinput);
       }
       while (userinput != "exit");
    
       return 0;
    }
    
    void myfun(string x)
    {
         cout << endl;
         cout << "Your Computer: apple" << endl;
         cout << endl;
    }
    There, I marked some obvious changes to get you going in the right direction.

    A) The "using namespace std" directive, just needs to be done once, under the includes, before the function. It'll do that for the rest of that file until another "using" is declared.

    B) To get it to keep asking, use a loop. Look up "loop constructs" - there's 3 basic types, while, do-while, and for. Do-while loops lend themselves well to input situations like you have.

    C) It's erroring when you use a string because you didn't include the string library. In C++, the only types you get are basics, like short, long, int, char, See (look up) "C++ primitives" for more information on those. Everything else is a struct, object, or typedef. Structs are just contiguous containers, classes is the basis of C++'s OOP, and typedefs just call something something else (for ease of syntax).

    Unless you define it yourself, any objects will come in as being linked, typically these will come in the form of include statements, ie.
    #include <string>

    Includes the C++ std lib for the string class.

    D) There's no built-in libraries that have just "lists" of words, you'll either have to access them via a system API (ie. OS X has a dictionary built in, I'm sure its accessible by just including the right library), or store it somewhere and read it in at run-time.

    E) For a list of what IS available to you "out of the box" - while it's not part of C++, the STL comes with just about every C++ implementation. You can find information on that at gotAPI/HTML - Instant search in HTML and other developer documentation or cplusplus.com - The C++ Resources Network - or you can just google "STL" (standard template library).

    Hope that helps get you started. You can PM me if you have any questions, or respond here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Average of user inputs (in a loop)
    By SilentPirate007 in forum C Programming
    Replies: 13
    Last Post: 03-08-2010, 05:46 PM
  2. Style Points
    By jason_m in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 06:15 AM
  3. program is checking for ent when user inputs out?
    By Blizzarddog in forum C++ Programming
    Replies: 1
    Last Post: 04-07-2003, 01:16 PM
  4. Replies: 3
    Last Post: 07-24-2002, 08:46 AM
  5. Replies: 1
    Last Post: 03-12-2002, 05:31 AM

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