-3

When answered with the first question, if you put in 1962, for example, it comes up with "Close but its 1969" instead of "Wrong, it 1969".

In fact, putting anything, including the 1969, which is the right answer, still outputs "Close but its 1969".

#include <iostream>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <unistd.h>
        
using namespace std;

void Neil_Question_year(){
    double Neil_year_answer;
    std::cout << "Welcome to questionaire game." << endl;
    usleep(1500000);
    std::cout << "Starting off with Neil...";
    usleep(2000000);
    std::cout << "\nWhat year was he born?: ";
    std::cin >> Neil_year_answer;

    if (Neil_year_answer == '1969')
    {
        cout << "\nSeems about right.";
    }
    else if (Neil_year_answer == '1968' or '1967' or '1970' or '1971'){
        cout << "\bClose but its 1969.";
    }
    else if(Neil_year_answer != '1968' or '1967' or '1970' or '1971')
        cout << "\bWrong, its 1969.";
}

void Neil_Question_month(){
    usleep(180000);
    std::cout << "\nNext question....";
    usleep(1000000);
    std::cout << "\nStarting in 5..." << endl;
    usleep(1000000);
    std::cout << "Starting in 4..." << endl;
    usleep(1000000);
    std::cout << "Starting in 3..." << endl;
    usleep(1000000);
    std::cout << "Starting in 2..." << endl;
    usleep(1000000);
    std::cout << "Starting in 1...\n" << endl;
    usleep(1000000);

    std::cout << "\bWhat month was he born in?";
}

int main(){
    Neil_Question_year();
    Neil_Question_month();
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Edward578
  • 3
  • 1
  • 3
    And, `'1968' or '1967' or '1970' or '1971'`, too. Please consider reading a [good introductory C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead of random coding. – Evg Jun 28 '21 at 14:40
  • 2
    You have *two* errors: First you attempt to compare a `double` variable with a *character* value; Then the logical `or` operator doesn't work as you expect. You seem to have skipped quite a few classes of school, or many chapters in your text-book. Don't do that and end up guessing about things. – Some programmer dude Jun 28 '21 at 14:44
  • 1
    @DrewDormann No, it should be `1969`. – MikeCAT Jun 28 '21 at 14:45
  • @MikeCAT is correct. `Neil_year_answer` is a `double`, so `'1969'` should be changed to `1969` – Drew Dormann Jun 28 '21 at 14:46

2 Answers2

3

You have Neil_year_answer of type double while you compare it with '1968', which is a multicharacter literal. The same goes for all other clauses. A multicharacter literal:

An ordinary character literal that contains more than one c-char is a multicharacter literal . A multicharacter literal has type int and implementation-defined value.

Looks like you should set Neil_year_answer to be unsigned and make direct checks against integer literals. For example:

unsigned Neil_year_answer;
cin >> Neil_year_answer;
if (Neil_year_answer == 1969) /* do the processing */;

Next, your use of the logical operator won't give you the desired logic. You have to do it like in the following form:

if (Neil_year_answer == 1969 || Neil_year_answer == 1970 || Neil_year_answer == 1971);

Where this:

if (Neil_year_answer == '1968' or '1967' or '1970' or '1971')

Will always yield true, since even if the Neil_year_answer == '1968' part is wrong, the next '1967' is not 0, hence not false. Thus, the logical operator returns true.

rawrex
  • 4,044
  • 2
  • 8
  • 24
2
  • '1969' is a multiple-character character constant and its value is implementation-defined. You should write like 1969 withput '' to express numbers.
  • You cannot use or like that. You should write comparision one-by-one or use std::set for defining sets to find from.
  • Your second else if looks redundant because it seems intended to just check for negate of previous condition.

One-by-one comparision:

if (Neil_year_answer == 1969)
{
    cout << "\nSeems about right.";
}
else if (Neil_year_answer == 1968 or Neil_year_answer == 1967 or Neil_year_answer == 1970 or Neil_year_answer == 1971){
    cout << "\bClose but its 1969.";
}
else {
    cout << "\bWrong, its 1969.";
}

Using std::set:

std::set<double> close_set = {1968, 1967, 1970, 1971};
if (Neil_year_answer == 1969)
{
    cout << "\nSeems about right.";
}
else if (close_set.find(Neil_year_answer) != close_set.end()){
    cout << "\bClose but its 1969.";
}
else {
    cout << "\bWrong, its 1969.";
}

Note that a condition like 1967 <= Neil_year_answer and Neil_year_answer <= 1971 will behave differently from the conditions above. For example, they will behave differently when Neil_year_answer = 1969.5.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • I'm a beginner to C++ well any programming language at all, watching: https://youtu.be/vLnPwxZdW4Y that to teach me, thanks for the help, but why is my question -3 and banned for asking other questions for 2 more days? I thought this was what Stackoverflow was for. – Edward578 Jun 29 '21 at 18:44