User Input Issues w/ Bool values

Unable to get user input with class that has bool functions

Page 1 of 1

10 Replies - 2713 Views - Last Post: 03 November 2009 - 03:57 PM Rate Topic: -----

#1 Chaosnub  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 19-October 09

User Input Issues w/ Bool values

Posted 02 November 2009 - 07:48 PM

Okay so, I've been having some trouble getting a project to build. The issue is that I'm unsure of how to properly get user input involving bool values. I'll post the code I have so far so you can have an idea of what I'm saying - and then explain a bit after.

The .h file, Logic.h:
#pragma once

class Logic
  {
   public:
	Logic(void);
	~Logic(void);
	Logic(bool x);
	bool Get(); //accessor function
	void Set(bool x); //mutator function
	bool Not();
	bool And(Logic x);
	bool Or(Logic x);
	bool Nand(Logic x);
	bool Nor(Logic x);
	bool ExOr(Logic x);
	bool ExNor(Logic x);
	bool Print();
   private:
	bool b;
  };


The implementation file, Logic.cpp:
#include "StdAfx.h"
#include "Logic.h"

Logic::Logic(void)
  {
   b = false;
  }
Logic::Logic(bool x)
  {
   b = x;
  }

Logic::~Logic(void)
  {
  }
bool Logic::Get()
  {
   return b;
  }
void Logic::Set(bool x)
  {
   b = x;
  }
bool Logic::Not()
  {
   return !b;
  }
bool Logic::And(Logic x)
  {
   return b && x.b;
  }
bool Logic::Or(Logic x)
  {
   return b || x.b;
  }
bool Logic::Nand(Logic x)
  {
   return !(b && x.b);
  }
bool Logic::Nor(Logic x)
  {
   return !(b || x.b);
  }
bool Logic::ExOr(Logic x)
  {
   return b && x.Not() || !b && x.b;
  }
bool Logic::ExNor(Logic x)
  {
   return !(b && x.Not() || !b && x.b);
  }
bool Logic::Print()
  {
   return b;
  }


...and the source file:
#include "stdafx.h"
#include <iostream>
#include "Logic.h"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
  {Logic a, b, sum;
   cout << "Enter a value for the first bit... " << endl;
   cin >> //this is where variable " a " would be input
   cout << "Enter a value for the second bit... " << endl;
   cin >> //this is where variable " b " would be input.
   
   sum = a.And(b);
   
   cout << "Sum = " << sum.Print() << endl;
   
	  return 0;
  }



As you can probably guess, I'm trying create a program that performs boolean algebra - pretty simple in itself, but I can't for the life of me figure out how to get user input. The user must input either 1 or 0. I've been toying with the Get() and Set() functions, but I can't quite seem to get it right. Build fails with errors each time. I must be completely missing something obvious, it's kind of depressing...

EDIT: Just a hint is fine too, I feel like I should know it, I've just kind of hit a wall.

This post has been edited by Chaosnub: 02 November 2009 - 09:11 PM


Is This A Good Question/Topic? 0
  • +

Replies To: User Input Issues w/ Bool values

#2 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: User Input Issues w/ Bool values

Posted 02 November 2009 - 08:48 PM

return b && x.b;
  }
bool Logic::Or(Logic x)
  {
   return b || x.b;
  }



One problem I see is in the above code. I've always been taught that a function can only ever return one thing. Take this, for example:
return b || x;


How on Earth is the compiler supposed to know what to return?
It always returns 1 for me. This:
return b && x;

Always returns 0 for me.
Was This Post Helpful? 0
  • +
  • -

#3 Chaosnub  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 19-October 09

Re: User Input Issues w/ Bool values

Posted 02 November 2009 - 09:07 PM

View PostOliveOyl3471, on 2 Nov, 2009 - 07:48 PM, said:

return b && x.b;
  }
bool Logic::Or(Logic x)
  {
   return b || x.b;
  }



One problem I see is in the above code. I've always been taught that a function can only ever return one thing. Take this, for example:
return b || x;


How on Earth is the compiler supposed to know what to return?
It always returns 1 for me. This:
return b && x;

Always returns 0 for me.

I can kind of see what you're getting at. But, try this in the main program:
{Logic a, b, sum;
   a.Set(true);
   b.Set(true);
   sum = a.And(b);
   
   cout << "Sum = " << sum.Print() << endl;
   
	  return 0;
  }


It should return 1. Same thing with the Or() function, it should return 1 or 0 depending on the values of a and b.

To further show my issue of setting the value of variables from user input, the above code is the only way I've been able to get it to work. It won't work alongside cin >> .

cin >> a;
cin >> b; //These won't work either.
Was This Post Helpful? 0
  • +
  • -

#4 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: User Input Issues w/ Bool values

Posted 02 November 2009 - 09:23 PM

Maybe you could get the user input and then, depending on what they input, set your bool values from that? Here's a simple example of what I mean.
#include <conio.h>
#include <iostream> 
using std::cout;
using std::cin;
using std::endl;


int main()
{
	int answer = 0;
	bool flag = false;
	cout<<"Enter 1 for true or 0 for false.";
	cin>>answer;

		while(answer != 1 && answer != 0){
			cout<<"Please enter only 1 or 0.";
			cin>>answer;
		}
	
		if(answer == 1){
			flag = true;
		}
		else{
			flag = false;
		}
	cout<<"You replied: "<<flag<<endl;
getch();
}

Was This Post Helpful? 1
  • +
  • -

#5 Chaosnub  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 19-October 09

Re: User Input Issues w/ Bool values

Posted 02 November 2009 - 09:28 PM

Hmm...

You may have just enlightened me. I'll see what I can work with with that example, thanks!

If I run into some more trouble, I'll check back in.
Was This Post Helpful? 0
  • +
  • -

#6 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: User Input Issues w/ Bool values

Posted 02 November 2009 - 09:31 PM

You're welcome. I surely hope that I was able to help. This seems to work just as well as my previous code, though.
	int answer = 0;
	bool flag = false;
	cout<<"Enter 1 for true or 0 for false.";
	cin>>answer;

		while(answer != 1 && answer != 0){
			cout<<"Please enter only 1 or 0.";
			cin>>answer;
		}
			flag = answer;
			
	cout<<"You replied: "<<flag<<endl;

Was This Post Helpful? 0
  • +
  • -

#7 Chaosnub  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 19-October 09

Re: User Input Issues w/ Bool values

Posted 03 November 2009 - 04:23 AM

Well, I managed to get the program working correctly. However, I need some quick help with another, smaller, issue.

Here's the code for the main program:
#include "stdafx.h"
#include <iostream>
#include "Logic.h"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
  {Logic a, b, cIn, cOut, sum;
   bool bit1, bit2, in; //Need separate bool variables since using Logic class variables gives errors.
		 cout << "Enter a value for the first bit... ";
		  cin >> bit1;
		 cout << "Enter a value for the second bit... ";
		  cin >> bit2;
		 cout << "Enter a value for the carry in bit... ";
		  cin >> in;
		 //Set a, b, and cIn equal to what was input for the bool variables.
			a.Set(bit1);
			b.Set(bit2);
			cIn.Set(in);
	   
		  //Find the sum.
		  sum = cIn.ExOr(a);
		  sum = sum.ExOr(b);
	   
		  if(in == 0) //Check what the carry in was, then find the carry out
		   {
		  cOut = a.And(b);
		   }
		  else if(in == 1)
		   {
		  cOut = a.Or(b);
		   }
	   
   cout << "The sum is " << sum.Print() << endl;
   cout << "The carry out is " << cOut.Print() << endl;
   
	  return 0;
  }



All I need to figure now is how to get the program to loop asking for variables and solving until the user enters all zeroes. I've tried
while(bit1 != 0 && bit2 != 0 && in != 0)


But I get errors. I think I'm on the right track, but I'm not quite sure. If I am, then I must be placing the loop in the wrong spot (placed before the user prompt).

This post has been edited by Chaosnub: 03 November 2009 - 04:45 AM

Was This Post Helpful? 0
  • +
  • -

#8 Jubb  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 88
  • Joined: 06-May 09

Re: User Input Issues w/ Bool values

Posted 03 November 2009 - 04:32 AM

bool Logic::Or(Logic x)
  {
   return b || x.b;
  }


Why are you doing this? If you're trying to perform bitwise logical operations, shouldn't you just use the operators directly?

For example:

 b = b | x; 


When you use two |'s it means boolean OR, not what you seem to want to do. Same with &.
Was This Post Helpful? 0
  • +
  • -

#9 Chaosnub  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 19-October 09

Re: User Input Issues w/ Bool values

Posted 03 November 2009 - 04:44 AM

Honestly, I have no clue. But it works.

The only issue I seem to have now is getting the program to actually loop until the user enters all zeroes...

This post has been edited by Chaosnub: 03 November 2009 - 04:44 AM

Was This Post Helpful? 0
  • +
  • -

#10 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: User Input Issues w/ Bool values

Posted 03 November 2009 - 07:18 AM

Try this (works for me)
	int bit1 = 1, bit2 = 1, in = 1;
	do{
		//your program code would go here, of course
		cout<<"enter bit1: ";
		cin>>bit1;
		cout<<"enter bit2: ";
		cin>>bit2;
		cout<<"enter in: ";
		cin>>in;
		
		cout<<"You entered: "<<bit1<<", "<<bit2<<", and "<<in<<endl;
				 
	   }while(bit1 != 0 || bit2 != 0 || in != 0);


You would think that AND would be the right thing to put in there, instead of OR. But the reason OR works and AND does not is this. If you say
do{
}while(bit1 != 0 && bit2 != 0 && in != 0);

You are saying while bit1 is not zero, AND bit2 is not zero AND in is not zero. So if any of them is zero, that whole statement is false. If bit1 is zero, but bit2 and in are not zero, the statement bit1, bit2 and in are all 'not zero' will be false. So your while loop ends.

However, if you say
do{
}while(bit1 != 0 || bit2 != 0 || in != 0);

You are saying while bit1 is not zero, OR bit2 is not zero, OR in is not zero...In other words if any of these things is not zero, keep going. They all have to be zero in order for the loop to quit.

I hope I have explained that well enough. It's a mite confusing at first. :)
Was This Post Helpful? 1
  • +
  • -

#11 Chaosnub  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 19-October 09

Re: User Input Issues w/ Bool values

Posted 03 November 2009 - 03:57 PM

You have once again enlightened me, OliveOyl3471. I never would have considered OR. But, from your explanation, it makes quite a bit of sense.

I guess I need to think more critically on the AND and OR operators in loops.

Thanks again!

EDIT: This did indeed work.

This post has been edited by Chaosnub: 03 November 2009 - 04:04 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1