if statement issues

argument error and a diappearing command window, again

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 2050 Views - Last Post: 05 March 2010 - 09:28 PM Rate Topic: -----

#16 mnm2317   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 31-January 10

Re: if statement issues

Posted 05 March 2010 - 08:38 PM

Ok, I need a loop (while or if statement) to make sure 'T' or 'F' is the only input accepted inputted characters (case does not matter). Here's what I have:
if ( (p!='t') || (p!='T') || (p!='f') || (p!='F') ){
	cout << "Error: Must be 1 or 2";
}



As it acts right now, no matter what I input for 'p' it always displays the 'Error' message.
Was This Post Helpful? 0
  • +
  • -

#17 PlasticineGuy   User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: if statement issues

Posted 05 March 2010 - 08:39 PM

Use && instead of ||.

By the way, an if statement is not a loop.

This post has been edited by PlasticineGuy: 05 March 2010 - 08:47 PM

Was This Post Helpful? 0
  • +
  • -

#18 mnm2317   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 31-January 10

Re: if statement issues

Posted 05 March 2010 - 08:44 PM

This is a final line in my homework, and it determines if what's been inputted is logically correct. As you can read the user needs to input 'f' and 't' in that order in order to get the 'consistent' message. However if 'f' and 'f' are input, it gives the 'consistent' message, which is wrong. Also, I have test it, and if 't' and 't' are entered, it gives the 'inconsistent' message, so it is not simply giving the first message, so somewhere my logic is off I believe.

if( (p=='f') || (p=='F') && (q=='t') || (q=='T') ){
	cout << "This is consistent.";
}
else{
	cout << "This is inconsistent.";
}


Was This Post Helpful? 0
  • +
  • -

#19 PlasticineGuy   User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: if statement issues

Posted 05 March 2010 - 08:44 PM

Don't double post.
Was This Post Helpful? 0
  • +
  • -

#20 mnm2317   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 31-January 10

Re: if statement issues

Posted 05 March 2010 - 08:47 PM

Wow so simply but worked like a charm. Weird too, because I thought I tried that. Oh well.

Now here's a related question: Why won't it continue to loop if I input something other than T or F? Like when I input Y, it gives me the error message but still moves through the code. How do I keep it looping until I get T or F?
Was This Post Helpful? 0
  • +
  • -

#21 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: if statement issues

Posted 05 March 2010 - 08:47 PM

*** Merged ***
Was This Post Helpful? 0
  • +
  • -

#22 PlasticineGuy   User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: if statement issues

Posted 05 March 2010 - 08:49 PM

Use a while loop and continually read into "p" until the same condition is true.
Was This Post Helpful? 0
  • +
  • -

#23 mnm2317   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 31-January 10

Re: if statement issues

Posted 05 March 2010 - 08:55 PM

Ahh ok, the while loop worked perfectly! I think I'm starting to get my coding footing back....just a little. :)

Ok, I can see the call for the merging. However I don't want this part to get lost:
if( (p=='f') || (p=='F') && (q=='t') || (q=='T') ){
	cout << "This is consistent.";
}
else{
	cout << "This is inconsistent.";
}



Since fixing the other if statement by turning it into a while loop, this still does not work right. I can still get a 'consistent' message with inputs of 'F' and 'F'. But I should only get that message if 'F' and 'T' are inputted. Where am I going wrong?
Was This Post Helpful? 0
  • +
  • -

#24 PlasticineGuy   User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: if statement issues

Posted 05 March 2010 - 08:57 PM

http://www.cpprefere...ator_precedence
&& takes precedence over ||.
Was This Post Helpful? 1
  • +
  • -

#25 mnm2317   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 31-January 10

Re: if statement issues

Posted 05 March 2010 - 09:11 PM

Hey I was just on that cite, lol. Ok that makes sense, but now how to I get the code to read it the way I'm trying to write it?
Was This Post Helpful? 0
  • +
  • -

#26 PlasticineGuy   User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: if statement issues

Posted 05 March 2010 - 09:13 PM

Use brackets to force the correct precedence.
if((x == 5 || x == 9) && (y == 1 || y == 19)) {
    //...
}

This post has been edited by PlasticineGuy: 05 March 2010 - 09:14 PM

Was This Post Helpful? 1
  • +
  • -

#27 mnm2317   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 31-January 10

Re: if statement issues

Posted 05 March 2010 - 09:17 PM

Awesome! Thanks Guy, you're my personal tutor today, thx!
Was This Post Helpful? 0
  • +
  • -

#28 PlasticineGuy   User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: if statement issues

Posted 05 March 2010 - 09:21 PM

It's always a good idea to use parentheses anyway to avoid possible bugs like this.
Was This Post Helpful? 0
  • +
  • -

#29 mnm2317   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 31-January 10

Re: if statement issues

Posted 05 March 2010 - 09:28 PM

Yea, I'm going to work on doing that. I never really used them in the past, but I've never done this complicated of comparing like I did here.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2