4 Replies - 290 Views - Last Post: 10 February 2014 - 04:28 PM Rate Topic: -----

#1 Sarao  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-February 14

I want to ask user three questions and validate the answer

Posted 10 February 2014 - 03:52 PM

OK here is the problem i C++, I want to as users for questions and have an answer back from them and validate if they have entered the correct answer weather its in uppercase or lowercase or in any way but has to match the correct answer of its not correct then gave the correct answer. So far here is my code. I am a new c++ learner.
// Libraries
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

#include <cctype>

// Programmer defined data types
// None

// Special compiler dependent definition
// None

// Global constants/variables
// None

// programmer defined functions
// Required for conversion to lowercase
class toLower {public: char operator()(char c) const {return tolower(c);}};

// Main program
int main()
{
    
    // Declaring Variables
    string q1;
    string q2;
    string q3;
  
    // Ask user question
    cout << "Who invented Mac?: ";
    getline(cin, q1);
    
    // Answer
    transform(q1.begin(), q1.end(), q1.begin(), toLower());
    if (q1 == "BIll Gates")
    {
        cout << "Correct,"<< q1 << endl;
    } else {
        cout << "Incorrect, it was " << q1 << endl;
    }
    
    
    // Ask user question
    cout << "What year was Mac invented?: ";
    getline(cin, q2);
    
    // Answer
    transform(q2.begin(), q2.end(), q2.begin(), toLower());
    if (q2 == "1984")
    {
        cout << "Correct." << endl;
    } else {
        cout << "Incorrect, Mac was invented " << q2 << endl;
    }
    
    
    // Ask user question
    cout << "Who is the new CEO if intel?: ";
    getline(cin, q3);
    
    // Answer
    transform(q3.begin(), q3.end(), q3.begin(), toLower());
    if ((q3 == "Brian Krzanich") || (q3 == "BrianKrzanich") || (q3 == "brian krzanich")
        || (q3 == "BRIAN KRZANICH") || (q3 == "BRIANKRZANICH") || ( q3 == "KZANICH") || (q3 == "krzanich"))
    {
        cout << "correct" << endl;
    } else {
        cout << "Incorrect, the new CEO of intel is "<< q3 << endl;
    }
    
    
    
}


This post has been edited by macosxnerd101: 10 February 2014 - 03:53 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: I want to ask user three questions and validate the answer

#2 Black_Epiphany  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 64
  • Joined: 02-February 14

Re: I want to ask user three questions and validate the answer

Posted 10 February 2014 - 04:01 PM

Make sure you make you indent and space your code so it's readable. For a program like this, I'd make a text file that has all the answers (all the acceptable answers and variations) for each question and read from that instead of writing a bunch of if statements.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,123
  • Joined: 05-May 12

Re: I want to ask user three questions and validate the answer

Posted 10 February 2014 - 04:04 PM

As an aside, I think Mac fans organize and break out the torches and pitchforks for what you consider as the correct answer of who invented the Mac.
Was This Post Helpful? 1
  • +
  • -

#4 Sarao  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-February 14

Re: I want to ask user three questions and validate the answer

Posted 10 February 2014 - 04:05 PM

View PostSkydiver, on 10 February 2014 - 04:04 PM, said:

As an aside, I think Mac fans organize and break out the torches and pitchforks for what you consider as the correct answer of who invented the Mac.

These are just temporary questions that I have used for a quick post
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: I want to ask user three questions and validate the answer

Posted 10 February 2014 - 04:28 PM

    // Ask user question
    cout << "Who invented Mac?: ";
    getline(cin, q1);
    
    // Answer
    transform(q1.begin(), q1.end(), q1.begin(), toLower());
    if (q1 == "BIll Gates")
    {
        cout << "Correct,"<< q1 << endl;
    } else {
        cout << "Incorrect, it was " << q1 << endl;
    }



Posted Image
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1