473,386 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Simple Bool Function Malfunction

16
Hey, I'm guessing my bool never returns true for some reason. There's no compiler error, it just continually prints back that the operator is invalid, even when the user enters a valid operater. I know I'm missing something glarlingly obvious in this section of my program, but I just can't spot it for the life of me:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. bool OpStatus(false);
  6. char TestOp, UseOp;
  7. int i;
  8. void IsValidOp(char TestOp, int i);
  9.  
  10. void main()
  11.      {    
  12.      while (1) 
  13.           {                                           
  14.           cout << "Enter desired operation: ";
  15.           cin >> TestOp;
  16.  
  17.           IsValidOp(OpStatus,i);
  18.     if (OpStatus==false)
  19.     {
  20.                 cout << "Invalid operator. Try again." << endl;
  21.     continue;
  22.     } else TestOp = UseOp;
  23.           }
  24.      }
  25.  
  26.  
  27. void IsValidOp(char TestOp, int i)
  28.     {
  29.     char ValidOp[] = "+-*/cCxX";
  30.     for (i=0; OpStatus=false, (i<strlen(ValidOp)); i++)
  31.         {
  32.         if (TestOp==ValidOp[i])
  33.             OpStatus = true;
  34.         }
  35.     }
  36.  
Mar 11 '07 #1
5 1760
sicarie
4,677 Expert Mod 4TB
Hey, I'm guessing my bool never returns true for some reason. There's no compiler error, it just continually prints back that the operator is invalid, even when the user enters a valid operater. I know I'm missing something glarlingly obvious in this section of my program, but I just can't spot it for the life of me:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. bool OpStatus(false);
  6. char TestOp, UseOp;
  7. int i;
  8. void IsValidOp(char TestOp, int i);
  9.  
  10. void main()
  11.      {    
  12.      while (1) 
  13.           {                                           
  14.           cout << "Enter desired operation: ";
  15.           cin >> TestOp;
  16.  
  17.           IsValidOp(OpStatus,i);
  18.     if (OpStatus==false)
  19.     {
  20.                 cout << "Invalid operator. Try again." << endl;
  21.     continue;
  22.     } else TestOp = UseOp;
  23.           }
  24.      }
  25.  
  26.  
  27. void IsValidOp(char TestOp, int i)
  28.     {
  29.     char ValidOp[] = "+-*/cCxX";
  30.     for (i=0; OpStatus=false, (i<strlen(ValidOp)); i++)
  31.         {
  32.         if (TestOp==ValidOp[i])
  33.             OpStatus = true;
  34.         }
  35.     }
  36.  
I think your problem might lie in the declaration:
Expand|Select|Wrap|Line Numbers
  1. bool OpStatus(false);
  2.  
Try doing:
Expand|Select|Wrap|Line Numbers
  1. bool opStatus;
  2.  
And then right before you use it, set it to whatever you want the initial value to be.
Mar 11 '07 #2
sicarie
4,677 Expert Mod 4TB
And upon closer inspection:
[code]
for (i=0; OpStatus=false, (i<strlen(ValidOp)); i++)
[/quote]

You set OpStatus to false every time you go through the loop. Even if this is set to true, the next time to loop is done, it's set to false - wiping out true.
Expand|Select|Wrap|Line Numbers
  1.     OpStatus = false;
  2.     for (i=0; (i<strlen(ValidOp)); i++) {
  3.  
That will keep OpStatus from being reset every time.
Mar 11 '07 #3
Randeh
16
Changed the bool declaration to just bool OpStatus; and altered the function as follows:

Expand|Select|Wrap|Line Numbers
  1.  
  2. void IsValidOp(char TestOp, int i)
  3.      {
  4.      char ValidOp[] = "+-*/cCxX";
  5.      OpStatus = false;
  6.      for (i=0; (i<strlen(ValidOp)); i++)
  7.           {
  8.           if (TestOp==ValidOp[i])
  9.     OpStatus = true;
  10.           }
  11.        }
  12.  
... and I'm still constantly getting a false returned! What I wouldn't give for an obvious solution once and a while.
Mar 11 '07 #4
Ganon11
3,652 Expert 2GB
You are going about this the wrong way. Instead of having a void function that changes a global boolean value, why not write a bool returning function? If you find the operator inside the ValidOp array, return true - if, after searching, you haven't found it, return false.
Mar 11 '07 #5
Randeh
16
Thanks, all makes much more sense than what I was trying!
Mar 11 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

31
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? ...
7
by: Ben | last post by:
Hi all, I'm not yet good at thinking the right way in c++ so although I could solve this problem, I'm not sure if they way I'm thinking of is the best way to do it. I need a data type or class...
32
by: someone else | last post by:
hi all I'm a newbie to this group. my apologies if I break any rules. I've wrote a simple program to find the first 1,000,000 primes, and to find all primes within any range (up to 200 *...
2
by: Norvin Laudon | last post by:
Hi, Should be simple, but I can't seem to get a handle on it. I've over-ridden the == operator in my class. public static bool operator==(XrayImage lhs, XrayImage rhs) { return (...
0
by: Tal Sharfi | last post by:
Hi everyone I recently had the need for StringGrid object same as the one that Delphi has. An object that helps show lists of other objects in a simple grid. I searched the news groups and...
8
by: Bern McCarty | last post by:
I have a simple ref class in its own namespace that needs to coexist with a legacy typedef alias for "unsigned int" in the global namespace that has the identifier as itself. Everything compiles...
5
by: Randeh | last post by:
Short story: in a beginning C++ class in college, was in a car accident that caused central spinal stenosis, some new Schmorl's nodes, disc problems and an incredible amount of pain. So far I've...
0
by: rich | last post by:
Hi all, I have a fairly complex "feed" application that recieves messages from an external user-supplied API via a callback function, and attempts to forward these messages to another...
7
by: aaragon | last post by:
Hi everyone, The idea is quite simple: generate a container with random values in it. For that, I decided to create a class that I called RandomContainer that inherits from a container (with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.