Thread: Accepting Word/Sentence Answers

  1. #1
    Registered User
    Join Date
    Aug 2006

    Accepting Word/Sentence Answers

    Say I give out a question, what is the code to accept a long or even one word answer.


    the program asks the question "What is the capital of the US?" How can you accept the answer.

    Sorry this sounds dumb but I have only done number answers.

  2. #2
    Registered User
    Join Date
    Aug 2006
    You'd either use a character array, or a string.
    character arrays go something like this:

    char    capital[20]; //The brackets mean it's an array, the 20 means 
                                 //that you can have up to 20 values in your array
    cin >> capital;
    strings are like this:
    #include <string>  //This is at the top of your program(before main)
    //in main do this:
    string capital;  //This makes a string variable and calls it captial.
    cin >> capital; //This will get one word answers, but if there's a space it'll end there.
    getline(cin, capital); //This gets a whole line for the capital string.
    take a look here
    Last edited by System_159; 09-08-2006 at 02:58 PM.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Common questions are in the FAQ.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Yahoo Answers
    By zacs7 in forum A Brief History of
    Replies: 7
    Last Post: 07-01-2008, 03:03 AM
  2. analysis and answers to ACM ICPC?
    By George2 in forum C Programming
    Replies: 0
    Last Post: 08-03-2006, 04:37 AM
  3. answers for Ivor Horton's Beginning C++?
    By 7stud in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2005, 12:13 AM
  4. Answers for exercise in K & R
    By kermit in forum C Programming
    Replies: 7
    Last Post: 01-25-2003, 08:38 PM
  5. need answers! (Only have 10 minutes)
    By minime6696 in forum Windows Programming
    Replies: 4
    Last Post: 09-01-2001, 06:45 PM