one of several

This is a discussion on one of several within the C++ Programming forums, part of the General Programming Boards category; after the user inputs a char how can a make i randomly return one of several responses? Does entering more ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    465

    one of several

    after the user inputs a char how can a make i randomly return one of several responses? Does entering more than one char work the same way as one?
    My computer is awesome.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    You are going to have to carefully think about the question you are attempting to ask, and then word it in a manner so that we can understand it.

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    After wrapping my brain around your corkscrew of a question, I think I sort of understand what you're getting at....

    To get more than one character as input, use cin.get

    example:
    Code:
    char name[512];
    cin.get(name,511,'\n');
    cin.ignore(512,'\n');
    As for randomly doing something depending on the input.....look in to the rand() function, and to make sure it's different every time look in to the srand function.

    You'll also need to use if statements. I hope you know how to use them.

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Your question is posed in such a way that leads me to believe that english may not be your first language. I will now attempt to respond to your inquery based on my own assumptions.

    From what I understand, you want a user to input a character, and then return a random response.

    Here is some example code for what it's worth:

    Code:
    #include<iostream>
    #include<cstdlib>
    #include<ctime>
    using namespace std;
    
    int main()
    {
         char input;
    
         cout "Enter a character ";
         cin >> input;     //this will only accept a single char 
         cin.ignore(200,"\n");  //ignore basically everying else in the input buffer
    
         srand((unsigned)time(NULL));
    
         switch(rand()%4)  //generate a random number from 0 to 3
         {
    
              case 0:  cout << "I am saying something";
                            break;
    
              case 1:  cout << "This is a random saying";
                            break;
    
              case 2:  cout << "I am saying something that is random";
                            break;
    
              case 3:  cout << "Random stuff";
                            break;
    
         }
    
    return 0;
    }


    To answer the second part of your question:
    Does entering more than one char work the same way as one?
    The use of cin.ignore( ) can be used to disregard any extraneous input that exceeds the first valid char (in this case, for up to 200 characters and/or the enter key)
    Last edited by The Brain; 01-13-2005 at 03:53 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Popular pages Recent additions subscribe to a feed

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