c++ if and else, help thanks!!!?

Here is my code.

It's a form for, quiz game, with points and difrent categorys.

The problem is that, when i'm running the program and answers a question. the answer always tell me that the answer is correct.

The other promblem is that the point system won't work

---MAIN.CPP---

#include "Definers.h"

int main()

{

//srand(static_cast<unsigned int>(time(0)));

//srand (time(NULL));

//CatKon = rand() % 3 + 1;

Points == 0;

system("CLS");

cout<<"Category: \n \n"

"1. Office \n"

"2. Room \n"

"3. Bathroom \n"

"4. Nersury Room \n"

"5. Living Room \n"

" #Bodil \n";

cin>>answer;

if(answer == 1)

{

{

cout<<"_______________________________ \n"

"The choosed category is Office. \n";

Sleep(2000);

CatKonPoints == 0;

CatKonStart:

srand (time(NULL));

CatKon = rand() % 3 + 1;

system("CLS");

}

if(CatKon == 1)

{

string answer;

cout<<"\n\n\n Translate: \n\n"

"-Chair- \n\n\n\n\n";

cin>>CharAnswer;

if (CharAnswer = "Stol" || "stol")

{

cout<<"Your guess was correct!";

CatKonPoints + 10;

Sleep(2000);

CatKonPoints + 1; //Out of 15

if(CatKonPoints == 3)

{

goto CatKonEnd;

}

system("CLS");

goto CatKonStart;

}

else

{

cout<<"Your answer was incorrect. \n";

system("CLS");

Sleep(2000);

CatKonPoints + 1; //Out of 15

if(CatKonPoints == 3)

{

goto CatKonEnd;

}

goto CatKonStart;

}

}

if(CatKon == 2)

{

string answer;

cout<<"\n\n\n Translate: \n\n"

"-Desktop- \n\n\n\n\n";

cin>>CharAnswer;

if (CharAnswer = "Skrivebord" || "skrivebord")

{

cout<<"Your guess was correct!";

CatKonPoints + 10;

Sleep(2000);

CatKonPoints + 1; //Out of 15

if(CatKonPoints == 3)

{

goto CatKonEnd;

}

system("CLS");

goto CatKonStart;

}

else

{

cout<<"Your answer was incorrect. \n";

system("CLS");

Sleep(2000);

CatKonPoints + 1; //Out of 15

if(CatKonPoints == 15)

{

goto CatKonEnd;

}

goto CatKonStart;

}

}

if(CatKon == 3)

{

string answer;

CatKon3:

cout<<"\n\n\n Translate: \n\n"

"-Bookshelf- \n\n\n\n\n";

cin>>answer;

if (CharAnswer = 'Reol' || 'reol' || "Boghylde" || "boghylde" || "BogHylde" || "BOGHYLDE")

{

cout<<"Your guess was correct!";

CatKonPoints + 10;

Sleep(2000);

CatKonPoints + 1; //Out of 15

if(CatKonPoints == 15)

{

goto CatKonEnd;

}

system("CLS");

goto CatKonStart;

}

else

{

cout<<"Your answer was incorrect. \n";

Sleep(2000);

CatKonPoints + 1; //Out of 15

if(CatKonPoints == 15)

{

goto CatKonEnd;

}

system("CLS");

goto CatKonStart;

}

}

else

{

cout<<"Critical Error!";

}

}

CatKonEnd:

cout<<"You got "<<Points<<" Points\n";

if(Points == 0)

{

"You are terrible at this game! \n"

"You should train ALOT more, \n"

"on your english. \n";

}

if(Points<20)

{

"Not that awful \n"

"You should train much more, \n"

"on your english. \n";

}

if(Points>30)

{

"Not THAT bad. \n"

"You should train more, \n"

"on your english. \n";

}

else

{

cout<<"Critical Error!";

}

system("PAUSE>nul");

return 0;

}

---DEFINERS.H---

#include <iostream>

#include <Windows.h>

#include <ctime>

#include <time.h>

#include <math.h>

#include <string>

#define VK_C 0x43

#define EXITCONDITION 42

using namespace std;

//Default

int UserGuess;

int answer;

int Points;

//Categorys

int CatKon;

int CatRoom;

int CatBath;

int CatNers;

int CatLiv;

int Bodil;

char CharAnswer;

int CatKonPoints;

___END___

I'm quite new to this language

and i want some detailed answers,

so i can learn to understand, why i got a problem.

Thanks if answering:3

Merry Christmas all:D

2 Answers

Relevance
  • Anonymous
    7 years ago
    Favorite Answer

    Points == 0;

    This is a equality check to see if points is equal to "0". IF you want to assign "0" to points, then use a single "=". As such, you never started your point system up in the first place. I also note that you are doing this in other places in place of assignments.

    =================

    if (CharAnswer = "Stol" || "stol")

    Three problems which also occurs elsewhere in your code:

    First, your assigning "Stol" into CharAnswer with "=". You want "==" for equality check for char types.

    Second, the boolean OR operator doesn't work that way. The operator checks both sides or the || and sees if either is true. Because c/c++ assumes that anything that is not "0" or NULL is true, it means that "stol" is evaluated as true, making the if statement condition true overall.

    Third, you are comparing a char variable with a string. This cannot be done as they are too different. Either make your CharAnswer a string type and use the correct function for the comparison, or make the constant comparison a char. Remember that single/double quotes make a difference between char/string, respectively.

    //if doing string compare

    string someString = "Hello there";

    if (someString.compare("Stol") == 0 || someString.compare("stol") == 0)

    //if doing char compare

    char CharAnswer = 'J';

    if (CharAnswer == 'A' || CharAnswer == 'B')

    ===========

    While you did not mention this, you are going to have issues with outputting. As far as I can tell, you are doing multi-line outputs incorrectly. Do also note that you will have to use "cout" for each separate code statement, and the code in the IF needs its own cout as such. The correct format is as below:

    cout << "This is the first line" << endl <<

    "This is another line" << endl <<

    "You can also use the new line char" << "\n" <<

    "to make new lines \n" <<

    "whether by itself or in the string";

    /* you can also write that entire code on one line, but this way is neater and easier to read code-wise*/

    if(true){

    ... cout << "you need a separate cout here";

    }

    • Commenter avatarLogin to reply the answers
  • roger
    Lv 7
    7 years ago

    this is wrong:

    if (CharAnswer = "Stol" || "stol")

    shoule be

    if(CharAnswer =="Stol" || CharAnswer=="stol"){

    the single = is for assignment NOT equality...

    AND your || is not doing what you want...

    Also

    you have a problem here:

    char CharAnswer; // char is for a single character a single character will NEVER equal the starting address of "Stol"

    use string instead

    string CharAnswer;

    this will not work either... you cannot use || that way and you need == not =

    if (CharAnswer = 'Reol' || 'reol' || "Boghylde" || "boghylde" || "BogHylde" || "BOGHYLDE")

    bzzzt--------------------------^^^ see above.

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.