13 Replies - 311 Views - Last Post: 19 April 2013 - 09:39 PM Rate Topic: -----

#1 makeitloud  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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;

	char answer;

	cout << "What is your name?\n";
	cin >> name;
	cout << "Were going to take a quiz, are you ready? <y/n>\n";
	cin >> answer;
	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  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10385
  • View blog
  • Posts: 38,434
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 makeitloud  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • 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

Was This Post Helpful? 1
  • +
  • -

#5 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5182
  • View blog
  • Posts: 26,880
  • Joined: 10-May 07

Re: if and else problem

Posted 19 April 2013 - 08:40 PM

View Postmacosxnerd101, 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.
Was This Post Helpful? 1
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10385
  • View blog
  • Posts: 38,434
  • 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! :)
Was This Post Helpful? 0
  • +
  • -

#7 makeitloud  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#8 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • 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

Was This Post Helpful? 0
  • +
  • -

#9 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#10 makeitloud  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#11 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • 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' */


Was This Post Helpful? 0
  • +
  • -

#12 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5182
  • View blog
  • Posts: 26,880
  • Joined: 10-May 07

Re: if and else problem

Posted 19 April 2013 - 09:17 PM

View PostButchDean, 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.
Was This Post Helpful? 0
  • +
  • -

#13 makeitloud  Icon User is offline

  • D.I.C Head

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

Re: if and else problem

Posted 19 April 2013 - 09:17 PM

i dont quite understand.
Was This Post Helpful? 0
  • +
  • -

#14 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • 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;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1