# if and else problem

Page 1 of 1

## 13 Replies - 533 Views - Last Post: 19 April 2013 - 09:39 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=319031&amp;s=272270181f4e9eee3d31f346afcaa79e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 makeitloud

Reputation: 0
• Posts: 64
• Joined: 08-April 13

# if and else problem

Posted 19 April 2013 - 08:25 PM

```#include <iostream>
#include <string>
#include <ctime>
using namespace std;

int main()
{
srand (time(NULL));

int grade, math[2], MathA;

string quiz, name;

cout << "What is your name?\n";
cin >> name;
cout << "Were going to take a quiz, are you ready? <y/n>\n";
if(answer == 'y' || answer == 'Y')
{
cout << "Ok lets get started " << name << endl;
cout << "First lets do a math problem.\n";
math[0] = rand()%50;
math[1] = rand()%20;
cout << "What is " << math[0] << " + " << math[1] << " ?\n";
cin >> MathA;
if(MathA=math[0] + math[1])
{
cout << "You got the Problem right, off to the next question\n";
}
else
cout << "Problem incorrect\n";

}

system("pause");
return 0;
}

```

whether i input right or wrong answer it always puts
```if(MathA=math[0] + math[1])
{
cout << "You got the Problem right, off to the next question\n";
}

```

Is This A Good Question/Topic? 0

## Replies To: if and else problem

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11796
• Posts: 44,320
• Joined: 27-December 08

## Re: if and else problem

Posted 19 April 2013 - 08:27 PM

You're using the single = operator, which is for assignment. The == operator is for comparisons.

### #3 makeitloud

Reputation: 0
• Posts: 64
• Joined: 08-April 13

## Re: if and else problem

Posted 19 April 2013 - 08:32 PM

problem solved, i didint use conditional operator sorry.
```if(MathA=math[0] + math[1])
```

nevermind, i still get both conditions if i put right answer.

### #4 jjl

• Engineer

Reputation: 1169
• Posts: 4,785
• Joined: 09-June 09

## Re: if and else problem

Posted 19 April 2013 - 08:32 PM

Naming variables math, and mathA are little vague. You should strive for meaningful variable names. Maybe instead of math[2], you could rename it to operands[2], and instead of mathA, you could rename it as userAnswer

Quote

nevermind, i still get both conditions if i put right answer.

= is to do an assignment
== is to compare

if(mathA = math[0] + math[1]) is always true as long as math[0] + math[1] is not zero. Replace the '=' with a '=='

This post has been edited by jjl: 19 April 2013 - 08:35 PM

### #5 no2pencil

• Professor Snuggly Pants

Reputation: 6223
• Posts: 29,814
• Joined: 10-May 07

## Re: if and else problem

Posted 19 April 2013 - 08:40 PM

macosxnerd101, on 19 April 2013 - 11:27 PM, said:

You're using the single = operator, which is for assignment. The == operator is for comparisons.

Using the single equals sign, being assignment, the conditional if statement will always return true because the condition is check if the variable can be assigned the value.

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11796
• Posts: 44,320
• Joined: 27-December 08

## Re: if and else problem

Posted 19 April 2013 - 08:41 PM

Forgot about that nuance with C/C++. Thanks for catching that!

### #7 makeitloud

Reputation: 0
• Posts: 64
• Joined: 08-April 13

## Re: if and else problem

Posted 19 April 2013 - 08:44 PM

sorry guys for being dumb, i should have relized that before i even posted.

### #8 jjl

• Engineer

Reputation: 1169
• Posts: 4,785
• Joined: 09-June 09

## Re: if and else problem

Posted 19 April 2013 - 08:46 PM

Quote

Using the single equals sign, being assignment, the conditional if statement will always return true because the condition is check if the variable can be assigned the value.

That's not true, the assignment will be made before the conditional check. After the assignment is made, the condition is based on the resulting value.

i.e.
```
int test;
if(test = 0) {
/* THIS WILL NEVER EXECUTE */
}
else {
/* THIS WILL ALWAYS EXECUTE */
}

```

This post has been edited by jjl: 19 April 2013 - 08:47 PM

### #9 anonymous26

• D.I.C Lover

Reputation: 1
• Posts: 3,638
• Joined: 26-November 10

## Re: if and else problem

Posted 19 April 2013 - 08:59 PM

I agree, it's the resulting value of the assignment that determines the outcome of the conditional, in other words jjl is right.

### #10 makeitloud

Reputation: 0
• Posts: 64
• Joined: 08-April 13

## Re: if and else problem

Posted 19 April 2013 - 09:05 PM

i have a question that is off topic, ok in that little test program im writing for a quiz i wanna make it so if you get it wrong, it will ask you if you wanna try again. so if you pick yes or 'y' is there a way to make it go back to the same question without having to re write(or copy paste) the question again?

### #11 jjl

• Engineer

Reputation: 1169
• Posts: 4,785
• Joined: 09-June 09

## Re: if and else problem

Posted 19 April 2013 - 09:07 PM

Yes, use a do-while loop

i.e.
```char again;
do { /* will always execute at least once */
/* do stuff */
std::cout<<"Again? (y/n): ";
std::cin>>again;
} while(again == 'y'); /* Reiterate loop if again equals 'y' */

```

### #12 no2pencil

• Professor Snuggly Pants

Reputation: 6223
• Posts: 29,814
• Joined: 10-May 07

## Re: if and else problem

Posted 19 April 2013 - 09:17 PM

ButchDean, on 19 April 2013 - 11:59 PM, said:

it's the resulting value of the assignment that determines the outcome of the conditional

I thought that's what I meant?!

Like system() always returns true, because calling system is success, but it doesn't return the assumed output of what command is passed to system.

### #13 makeitloud

Reputation: 0
• Posts: 64
• Joined: 08-April 13

## Re: if and else problem

Posted 19 April 2013 - 09:17 PM

i dont quite understand.

### #14 anonymous26

• D.I.C Lover

Reputation: 1
• Posts: 3,638
• Joined: 26-November 10

## Re: if and else problem

Posted 19 April 2013 - 09:39 PM

Run this code.

```#include <iostream>
using namespace std;

int main()
{
int a;

int b = 1;
int c = 0;

if(a = c + c)
{
cout << "I am never seen!" << endl;
}

if(a = b + c)
{
cout << "I am seen!" << endl;
}

return 0;
}

```