473,395 Members | 1,677 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,395 software developers and data experts.

Bool explanation?

2
Recently I was assigned to create a function that would return true if the string consisted of uppercase letters. I created this function but however whenever I run it, it states that not all control paths return a value. Could someone explain why it does that?

bool isUppercase(string text)
{
for (int k = 0; k < text.size(); k++)
{
if (text[k] = toupper(text[k]) || text =="")
return true;
else
return false;
}
}
Oct 27 '09 #1
8 2836
Markus
6,050 Expert 4TB
Looking at it, I don't understand why you would get that error as all code paths do return a value?
Oct 27 '09 #2
donbock
2,426 Expert 2GB
What if text.size is <= 0? The for loop won't execute at all, the next statement is the closing brace of the function.

Perhaps it is impossible for text.size to be <= 0. Apparently the static analysis tool isn't smart enough to realize that. On the other hand, perhaps you only think it is impossible.

However, you have bigger problems. This function only tests the first character in the string. My impression is that you want to test all characters in the string. Can't help you without more information. What should the function return for the following strings:
"HELLO"
"Hello"
"hello"
"1234"
Oct 27 '09 #3
Raiju
2
I've rewrote it as
bool isUppercase(string text)
{
for (string::size_type k = 0; k < text.size(); k++)
{
if(isalpha(text[k]))
{
if (islower(text[k]))
return false;
}
}
return true;
}

which doesn't seem to have any problems. Thanks for the help.
Oct 27 '09 #4
whodgson
542 512MB
Yes because you used toupper (convert to upper case) instead of isupper (is already upper)
Oct 28 '09 #5
weaknessforcats
9,208 Expert Mod 8TB
bool isUppercase(string text)
{
for (int k = 0; k < text.size(); k++)
{
if (text[k] = toupper(text[k]) || text =="")
return true;
else
return false;
}
}
Going back to your original question, the code above has an error that not all paths return a value. Note that the two return statements are part of the of the if statement.

You need a cosmtic return (one that is never reached but keeps the compiler happy):

Expand|Select|Wrap|Line Numbers
  1. bool isUppercase(string text)
  2. {
  3. for (int k = 0; k < text.size(); k++)
  4. {
  5.       if (text[k] = toupper(text[k]) || text =="")
  6.       return true;
  7.       else
  8.       return false;
  9.       }
  10.  
  11.       return false;  //cosmetic return
or simply omit the else part of the if statement:

Expand|Select|Wrap|Line Numbers
  1. bool isUppercase(string text)
  2. {
  3. for (int k = 0; k < text.size(); k++)
  4. {
  5.       if (text[k] = toupper(text[k]) || text =="")
  6.       return true;
  7.  
  8.       return false;  //cosmetic return
Oct 28 '09 #6
Ectara
24
Also, in the if statement on line 5, make sure you use the == operator for making comparisons, rather than the = assignment operator. It would give unexpected results otherwise.
Nov 3 '09 #7
Banfa
9,065 Expert Mod 8TB
@weaknessforcats
Actually this still has a path with no return statement, consider the case of isUppercase being passed an empty string, i.e. text.size() returning 0.

Assuming that the logic was correct (i.e. not calling toupper) then you need to return a value after the for loop.
Nov 3 '09 #8
weaknessforcats
9,208 Expert Mod 8TB
Actually this still has a path with no return statement,
Oops. I missed that.
Nov 3 '09 #9

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

Similar topics

3
by: Pierre Espenan | last post by:
A have a long integer class. The built integer type within a conditional statement returns bool false for int i=0 and bool true for any other non zero value. I want my long integer class to have...
19
by: daniel | last post by:
1) is C++ smart enough to automatically use "bits" for bool or will a bool have the size of a charcter (byte). 2) The index of a vector is it an integer (4 byte) or a "long long" with 8 bytes or...
4
by: ORC | last post by:
Is the bool type actually an Int32 with 0 as false and non zero as true? The reason for my question is that I've seen a lot of API calls that return a Int32 implemented in C# as an bool like: ...
6
by: zl2k | last post by:
hi, there I am using a big, sparse binary array (size of 256^3). The size may be changed in run time. I first thought about using the bitset but found its size is unchangeable. If I use the...
64
by: shaanxxx | last post by:
I have code which says #define MYBOOL int This code is very old. people who have written is not avaible. I was thinking what could be reason. 1) bool datatype was not available that time (10...
3
by: markb | last post by:
Hi My C# app is being called from a callback from an unmanaged DLL. One of the parameters of the callback is of type BOOL. I am using PInvoke to marshal this to a (managed) bool. The problem is...
57
by: Alan Isaac | last post by:
Is there any discussion of having real booleans in Python 3000? Say something along the line of the numpy implementation for arrays of type 'bool'? Hoping the bool type will be fixed will be...
51
by: AommiK | last post by:
First of all: I love C and think that it's beautiful. However, there is at least one MAJOR flaw: the lack of a boolean type. OK. Some of you might refer to C99 and its _Bool (what's up with the...
32
by: Simon L | last post by:
BOOL bMyarray; memset(bMyArray , 0xFF, sizeof(BOOL) * 1000); clean & quick until someone else's code found I was returning -1 to mean true Because my BOOL is 4 bytes long, TRUE in memory...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.