By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,339 Members | 1,973 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,339 IT Pros & Developers. It's quick & easy.

limited range of data type

P: 7
if (a[n] == 'a' || a[n] == 'e' || a[n] == 'i' || a[n] == 'o' || a[n] == 'u' || a[n] == 'w' || a[n] == 'y' || a[n] ==
'h' || a[n] == 'space' || a[n] == '-'){

Basically im asking the program to do something if it comes across any of the above characters, problem seems to be with the space. Ive tried ' ' which doesnt work, tried the unicode for a space but it said its only supported in c++, so how could I solve the error " warning: comparison is always false due to limited range of data type"? Help appreciated!
Nov 15 '06 #1
Share this Question
Share on Google+
14 Replies


Banfa
Expert Mod 5K+
P: 8,916
a[n] == 'space'

should be

a[n] == ' '

I know you say this doesn't work but since you don't say how it doesn't work and I know that this is the correct test for a space character I can only assume that whatever is causing it to not work is either a problem else where in your code or a problem in the input data to the program.

however that only checks for the space character, perhaps what you have is a tab character in the input data? so you could also try

isspace(a[n]);

defined in ctype.h which checks for all whitespace characters space, tab, new line and carridge return.

or you can check for just tabs as well with

a[n] == '\t'
Nov 15 '06 #2

P: 7
When using ' ' the output is incorrect. The program takes two words "For Example" and encodes them (much like the soundex algorithm). The output is fine if I use "For-Example" but taking away the '-' and leaving a space gives the wrong output.
Nov 15 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
Again since you are failing to give detail, the expected output and the actual output, and you have failed to give your code I can not comment. All I can say is that

a[n] == ' '

is the correct test for a space.
Nov 15 '06 #4

P: 7
int removevowels(char a[size], char b[3])
{
int i = 1;
int n = 1;
int c;
int x = strlen(a);
b[0] = a[0];

while (n <= x)
{
if (a[n] == 'a' || a[n] == 'e' || a[n] == 'i' || a[n] == 'o' || a[n] == 'u' || a[n] == ' ' || a[n] == '-'){
}else
{
b[i] = a[n];
i++;
}
n++;
}
c = strlen(b);
return 0;
}

The function is meant to remove all vowels, hyphens and spaces from a string.
Nov 15 '06 #5

Banfa
Expert Mod 5K+
P: 8,916
Well that works fine for me

Expand|Select|Wrap|Line Numbers
  1. int main(void)
  2. {
  3.     char string[] = "For Example";
  4.     char out[30];
  5.  
  6.     removevowels(string, out);
  7.  
  8.     cout << '"' << string << '"' << " to " << '"' << out << '"' << endl;
  9.  
  10.     return 0;
  11. }  
  12.  
output

Expand|Select|Wrap|Line Numbers
  1. "For Example" to "FrExmpl"
like I said it's probably an error in the rest of your code somewhere or the input data.
Nov 15 '06 #6

P: 7
input

Von Dutch

output

Vn

it doesnt seem to bother with the second word.
Nov 15 '06 #7

Banfa
Expert Mod 5K+
P: 8,916
No I get the output

Expand|Select|Wrap|Line Numbers
  1. "Von Dutch" to "VnDtch"
and I am using your removevowels function exactly as you have posted it (i.e. I just copied and pasted it into my program).

As I have already said twice the error must be in another part of your program code or related to the interaction between the rest of your code and this function.
Nov 15 '06 #8

P: 7
Ive commented out all the other code
Nov 15 '06 #9

10K+
P: 13,264
No I get the output

Expand|Select|Wrap|Line Numbers
  1. "Von Dutch" to "VnDtch"
and I am using your removevowels function exactly as you have posted it (i.e. I just copied and pasted it into my program).

As I have already said twice the error must be in another part of your program code or related to the interaction between the rest of your code and this function.
How are you getting the input string? It seems you are reading only one word at a time i.e reading the input until a space is returned so that you only have one word in the end.
Nov 15 '06 #10

P: 7
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define size 30


int removevowels(char a[size], char b[3])
{
int i = 1;
int n = 1;
int c;
int x = strlen(a);
b[0] = a[0];

while (n <= x)
{
if (a[n] == 'a' || a[n] == 'e' || a[n] == 'i' || a[n] == 'o' || a[n] == 'u' || a[n] == ' ' || a[n] == '-'){
}else
{
b[i] = a[n];
i++;
}
n++;
}
c = strlen(b);
return 0;
}

int main()

{
char name [size];
char removevowelsname [3];
printf("Surname:");
scanf("%s", &name);
removevowels(name, removevowelsname);
printf("Encoded: %s\n", removevowelsname);
return 0;
}


That is everything that is uncommented.
Nov 15 '06 #11

Banfa
Expert Mod 5K+
P: 8,916
char removevowelsname [3];

This is the problem, you have only allocate 3 bytes of memory to hold the converted string but "VnDtch" is 7 bytes.

Like I said the problem was in a piece of code that you had not posted.
Nov 15 '06 #12

P: 7
char removevowelsname [size];

taking the predefined limit of 30

output

Vn
Nov 15 '06 #13

10K+
P: 13,264
char removevowelsname [3];

This is the problem, you have only allocate 3 bytes of memory to hold the converted string but "VnDtch" is 7 bytes.

Like I said the problem was in a piece of code that you had not posted.
use a char*
Nov 15 '06 #14

Banfa
Expert Mod 5K+
P: 8,916
use a char*
Certainly it would be more usual to declare the function removevowels with char * parameters but the way they are declared the type of the parameters to removevowels are char * even though it may not be obvious, because you can't pass an array as a function parameter.

However the other mistake that is causing the error is in

scanf("%s", &name);

Mistake 1 (really minor)
&name, name is already a pointer and the & is not required.

Mistake 2 (the cause of the problem
scanf uses space as a string delimiter so in the string "Von Dutch" "Von" gets assigned to name and "Dutch" gets discarded as not required (or left in the input buffer).

scanf is an unsafe function to use anyway with %s due to the risk of buffer overruns, replace it with

fgets(name, sizeof name, stdin);

which reads a whole line into name and it all starts working.


But you do still need to resize the array removevowelsname
Nov 15 '06 #15

Post your reply

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