 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
lauryn Guest
|
Posted: Tue Dec 13, 2005 12:49 pm Post subject: RECURSIVELY....Printing Vowels from a string |
|
|
I am trying to write a program to recursively print all the vowels in a
string...I keep getting this error message.(Value of type void is not
allowed in function printVowels)It is directing me to the last line in
the function printvowels...... and I am using turbo C++ 4.5??
Any help will be appreciated....thanx....
#include <stdio.h>
#define size 10
void printVowels(char*);
void main()
{
char str[size];
char ch;
int i=0;
printf("Vowels will be printedn");
printf("Enter a stringn");
/*Reading the string into an array*/
/*the loop terminates when the newline character is read*/
while((ch = getchar()) !='n')
if (i < size)
/*Storing each character into the string*/
str [i++] = ch;
/*Inserting the null terminator at the end of the string*/
str[i]=' ';
printf("%sn",str);
/*Call function to printvowels*/
printVowels(str);
}
void printVowels(char *s)
{
/*Base Case...an EMpty string*/
if (*s==' ')
printf("NO string enteredn");
/*Checking for Vowels*/
if((*s=='a')||(*s=='e')||(*s=='i')||(*s=='o')||(*s=='u'))
printf("%sn",printVowels(s));
}
--
comp.lang.c.moderated - moderation address: [email]clcm (AT) plethora (DOT) net[/email] -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
|
|
| Back to top |
|
 |
Kenneth Brody Guest
|
Posted: Tue Jan 10, 2006 3:35 am Post subject: Re: RECURSIVELY....Printing Vowels from a string |
|
|
lauryn wrote:
| Quote: |
I am trying to write a program to recursively print all the vowels in a
string...I keep getting this error message.(Value of type void is not
allowed in function printVowels)It is directing me to the last line in
the function printvowels...... and I am using turbo C++ 4.5??
Any help will be appreciated....thanx....
[...]
void printVowels(char *s)
{
[...]
printf("%sn",printVowels(s));
}
|
If printVowels() returns void, how can you pass it to printf()?
--
+-------------------------+--------------------+-----------------------------+
| Quote: | Kenneth J. Brody | www.hvcomputer.com | |
kenbrody/atspamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+ |
Don't e-mail me at: <mailto:ThisIsASpamTrap (AT) gmail (DOT) com>
--
comp.lang.c.moderated - moderation address: [email]clcm (AT) plethora (DOT) net[/email] -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
|
|
| Back to top |
|
 |
Targeur fou Guest
|
Posted: Tue Jan 10, 2006 3:35 am Post subject: Re: RECURSIVELY....Printing Vowels from a string |
|
|
lauryn wrote:
| Quote: | I am trying to write a program to recursively print all the vowels in a
string...I keep getting this error message.(Value of type void is not
allowed in function printVowels)It is directing me to the last line in
the function printvowels...... and I am using turbo C++ 4.5??
Any help will be appreciated....thanx....
#include
#define size 10
void printVowels(char*);
void main()
|
int main(void)
| Quote: | {
char str[size];
char ch;
int i=0;
printf("Vowels will be printedn");
printf("Enter a stringn");
/*Reading the string into an array*/
/*the loop terminates when the newline character is read*/
while((ch = getchar()) !='n')
|
getchar() returns an int, ch should have the int type.
| Quote: | if (i < size)
/*Storing each character into the string*/
str [i++] = ch;
/*Inserting the null terminator at the end of the string*/
str[i]=' ';
printf("%sn",str);
/*Call function to printvowels*/
printVowels(str);
}
void printVowels(char *s)
{
/*Base Case...an EMpty string*/
|
You should test the validity of s.
| Quote: | if (*s==' ')
printf("NO string enteredn");
/*Checking for Vowels*/
if((*s=='a')||(*s=='e')||(*s=='i')||(*s=='o')||(*s=='u'))
printf("%sn",printVowels(s));
|
The printf %s format specifier waits for a string, a char * argument,
but you're passing the address of the printVowels() function which
doesn't return anything. Even if printVowels() returned a char and you
used the correct format specifier, it would'nt do the correct job,
since you don't update your pointer to advance in the string.
void printVowels(char *s)
{
/* The break condition is when we reach the end of the string (or if
s is NULL) */
if (s == NULL || *s==' ') {
fflush(stdout);
return;
}
/* Print the character if it's a vowel */
if((*s=='a')||(*s=='e')||(*s=='i')||(*s=='o')||(*s=='u')) {
putc(*s, stdout);
}
/* Call recursively printVowels with the next character */
printVowels(++s);
}
HTH
Regis
--
comp.lang.c.moderated - moderation address: [email]clcm (AT) plethora (DOT) net[/email] -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
|
|
| Back to top |
|
 |
Chuck F. Guest
|
Posted: Tue Jan 10, 2006 3:35 am Post subject: Re: RECURSIVELY....Printing Vowels from a string |
|
|
lauryn wrote:
| Quote: |
I am trying to write a program to recursively print all the
vowels in a string...I keep getting this error message.(Value
of type void is not allowed in function printVowels) It is
directing me to the last line in the function printvowels......
and I am using turbo C++ 4.5?? Any help will be appreciated.
..... snip ...
void printVowels(char *s)
{
/*Base Case...an EMpty string*/
if (*s==' ')
printf("NO string enteredn");
/*Checking for Vowels*/
if((*s=='a')||(*s=='e')||(*s=='i')||(*s=='o')||(*s=='u'))
printf("%sn",printVowels(s));
}
|
Change the last statement to:
printf("%cn", *s);
instead of the recursive call to printVowels, which returns void.
Your indentation could be seriously improved.
--
Read about the Sony stealthware that is a security leak, phones
home, and is generally illegal in most parts of the world. Also
the apparent connivance of the various security software firms.
http://www.schneier.com/blog/archives/2005/11/sonys_drm_rootk.html
--
comp.lang.c.moderated - moderation address: [email]clcm (AT) plethora (DOT) net[/email] -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
|
|
| Back to top |
|
 |
Douglas A. Gwyn Guest
|
Posted: Tue Jan 10, 2006 3:35 am Post subject: Re: RECURSIVELY....Printing Vowels from a string |
|
|
lauryn wrote:
| Quote: | ...I keep getting this error message.(Value of type void is not
allowed in function printVowels)It is directing me to the last line in
the function printvowels...
|
Yes, where you have
| Quote: | printf("%sn",printVowels(s));
|
which is evidently trying to use the return value of printVowels
on the assumption that it has type char *, contrary to its actual
definition.
--
comp.lang.c.moderated - moderation address: [email]clcm (AT) plethora (DOT) net[/email] -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
|
|
| Back to top |
|
 |
Michael Tiomkin Guest
|
Posted: Tue Jan 10, 2006 3:35 am Post subject: Re: RECURSIVELY....Printing Vowels from a string |
|
|
lauryn wrote:
| Quote: | I am trying to write a program to recursively print all the vowels in a
string...I keep getting this error message.(Value of type void is not
allowed in function printVowels)It is directing me to the last line in
the function printvowels...... and I am using turbo C++ 4.5??
Any help will be appreciated....thanx....
#include
#define size 10
void printVowels(char*);
void main()
{
char str[size];
char ch;
int i=0;
printf("Vowels will be printedn");
printf("Enter a stringn");
/*Reading the string into an array*/
/*the loop terminates when the newline character is read*/
while((ch = getchar()) !='n')
if (i < size)
/*Storing each character into the string*/
str [i++] = ch;
/*Inserting the null terminator at the end of the string*/
str[i]=' ';
printf("%sn",str);
/*Call function to printvowels*/
printVowels(str);
}
void printVowels(char *s)
{
/*Base Case...an EMpty string*/
if (*s==' ')
printf("NO string enteredn");
/*Checking for Vowels*/
if((*s=='a')||(*s=='e')||(*s=='i')||(*s=='o')||(*s=='u'))
printf("%sn",printVowels(s));
}
|
You are using the result of printVowels function as a parameter to
another function. You cannot do this because the function printVowels
doesn't have a result.
You also have at least 3 other problems, but I assume it was your
assignment, and you'd better find them yourself.
When your program compiles and properly runs for a small example, and
this means that you fixed the first two bugs, pls try to run/debug it
with 10 or more characters in the string. This might show you the third
problem.
Just two suggestions:
1. You can use 0 instead of ' ', they have the same value, and 0 is
shorter.
2. It's better to use an array of vowels instead of a compound logical
expression in the 'if' statement, this will make changing a program
much easier if, for example, you'll need to print the vowels of a
different language.
Michael
--
comp.lang.c.moderated - moderation address: [email]clcm (AT) plethora (DOT) net[/email] -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
|
|
| Back to top |
|
 |
Ross Wilson Guest
|
Posted: Tue Jan 10, 2006 3:35 am Post subject: Re: RECURSIVELY....Printing Vowels from a string |
|
|
lauryn wrote:
<snip>
| Quote: | void printVowels(char *s)
{
/*Base Case...an EMpty string*/
if (*s==' ')
printf("NO string enteredn");
/*Checking for Vowels*/
if((*s=='a')||(*s=='e')||(*s=='i')||(*s=='o')||(*s=='u'))
printf("%sn",printVowels(s));
}
|
I'm not going to talk about setting up the environment as that's boring,
but the recursive function ain't going to work. The error you get is
due to you using the return value of printVowels() that you've defined
as returning void. That aside, the function is broken.
When designing a recursive function you must first decide what the
recursive function will do. In this case one way to design the function
is to say 'it will print to stdout the vowels in the passed string' and
return void. Note that the function WON'T print a 'n'.
Now you assume you *have* a function that can do that and you use that
function to write printVowels(). Initially let's just worry about the
*recursive* part of the function:
void printVowels(char *s)
{
if (isvowel(*s))
printf("%c", *s);
printVowels(s + 1);
}
The trick in this case is to write code that handles the first char
and calls the recursive function to handle the rest. For the purposes
of keeping the example short we also assume we have an isvowel()
function. The problem in the code above is that it won't stop at the
end of the string. Try:
void printVowels(char *s)
{
if (*s == ' ') // the recursion 'stop' code
return;
if (isvowel(*s)) // the recursion code
printf("%c", *s);
printVowels(s + 1);
}
There are lots of ways to improve the above, but the basic
idea is there (modulo bugs of course: I haven't tested the code .
HTH,
Ross
-----------------------------------------------------------
To understand recursion one must first understand recursion.
--
comp.lang.c.moderated - moderation address: [email]clcm (AT) plethora (DOT) net[/email] -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
|
|
| Back to top |
|
 |
sid Guest
|
Posted: Tue Jan 10, 2006 3:36 am Post subject: Re: RECURSIVELY....Printing Vowels from a string |
|
|
Where are you incrementing the char* s?
--
comp.lang.c.moderated - moderation address: [email]clcm (AT) plethora (DOT) net[/email] -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
|
|
| Back to top |
|
 |
Barry Schwarz Guest
|
Posted: Tue Jan 10, 2006 3:36 am Post subject: Re: RECURSIVELY....Printing Vowels from a string |
|
|
On 13 Dec 2005 12:49:12 GMT, "lauryn" <brownluv25 (AT) iwon (DOT) com> wrote:
| Quote: | I am trying to write a program to recursively print all the vowels in a
string...I keep getting this error message.(Value of type void is not
allowed in function printVowels)It is directing me to the last line in
the function printvowels...... and I am using turbo C++ 4.5??
Any help will be appreciated....thanx....
#include <stdio.h
#define size 10
void printVowels(char*);
void main()
|
int main(void)
| Quote: | {
char str[size];
char ch;
int i=0;
printf("Vowels will be printedn");
printf("Enter a stringn");
/*Reading the string into an array*/
/*the loop terminates when the newline character is read*/
while((ch = getchar()) !='n')
|
getchar returns an int, not a char. You also need to test for EOF.
| Quote: | if (i < size)
/*Storing each character into the string*/
str [i++] = ch;
|
When i is 9, you store the 10th character and increment i.
| Quote: | /*Inserting the null terminator at the end of the string*/
str[i]=' ';
|
And then try to store a ' ' in str[10]. You have just overflowed str
and invoked undefined behavior.
| Quote: |
printf("%sn",str);
/*Call function to printvowels*/
printVowels(str);
}
void printVowels(char *s)
{
/*Base Case...an EMpty string*/
if (*s==' ')
printf("NO string enteredn");
|
Surely you want to quit the function after determining there is
nothing to do.
| Quote: | /*Checking for Vowels*/
if((*s=='a')||(*s=='e')||(*s=='i')||(*s=='o')||(*s=='u'))
printf("%sn",printVowels(s));
|
You are telling printf to use the value returned by printVowels as the
address of a string to print. But printVowels is defined as returning
void, that is, it returns nothing. You can't pass a nothing to
printf.
<
--
comp.lang.c.moderated - moderation address: [email]clcm (AT) plethora (DOT) net[/email] -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
|
|
| Back to top |
|
 |
Frodo Baggins Guest
|
Posted: Tue Jan 10, 2006 3:37 am Post subject: [comp.lang.c.moderated] Re: RECURSIVELY....Printing Vowels f |
|
|
lauryn wrote:
| Quote: | I am trying to write a program to recursively print all the vowels in a
string...I keep getting this error message.(Value of type void is not
allowed in function printVowels)It is directing me to the last line in
the function printvowels...... and I am using turbo C++ 4.5??
This particular problem does not depend on the compiler |
<code truncated>
I have corrected the printVowels() function
There is no problem in the main() function except for the return value
- trivial issue.
My comments begin with /** to differentiate from existing comments
The modified code is below:
#include <stdio.h>
#define size 10
void printVowels(char*);
int main()
{
char str[size];
char ch;
int i=0;
printf("Vowels will be printedn");
printf("Enter a stringn");
/*Reading the string into an array*/
/*the loop terminates when the newline character is read*/
while((ch = getchar()) !='n')
if (i < size) /*Storing each character into the
string*/
str [i++] = ch;
/*Inserting the null terminator at the end of the string*/
str[i]=' ';
/** if we use fgets or scanf , it is much easier as we do not have to
add the
* manually
* fgets(str,10,stdin) OR
* scanf("%[^n]s",str) to read upto newline , including spaces
*/
printf("%sn",str);
/*Call function to printvowels*/
printVowels(str);
return 0;
}
void printVowels(char *s)
{
/*Base Case...an EMpty string*/
if (*s==' '){
/** printf("NO string enteredn"); */
/** This case occurs when we reach end of string.
* Hence commented out
*/
return; /* nothing left to do */
}
/*Checking for Vowels*/
if((*s=='a')||(*s=='e')||(*s=='i')||(*s=='o')||(*s=='u')){
printf("n%c",*s);
/** print char if it is a vowel
statement changed to print specified character
only
*/
s++; /** to move on to next character
value in caller is not modified as
pointer is passed by value
*/
printVowels(s);
/** printf("%sn",printVowels(s));
* Error was because printVowels() was returning nothing
* but %s fmt specifier was used indicating a char* (string)
*/
}
Any comments are welcome.
Regards,
Frodo son of Drogo
| Quote: | --
comp.lang.c.moderated - moderation address: [email]clcm (AT) plethora (DOT) net[/email] -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
-- |
comp.lang.c.moderated - moderation address: [email]clcm (AT) plethora (DOT) net[/email] -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
|
|
| Back to top |
|
 |
WillerZ Guest
|
Posted: Tue Jan 10, 2006 3:37 am Post subject: Re: RECURSIVELY....Printing Vowels from a string |
|
|
lauryn wrote:
| Quote: | I am trying to write a program to recursively print all the vowels in a
string...I keep getting this error message.(Value of type void is not
allowed in function printVowels)It is directing me to the last line in
the function printvowels...... and I am using turbo C++ 4.5??
Any help will be appreciated....thanx....
#include
#define size 10
void printVowels(char*);
void main()
{
char str[size];
char ch;
int i=0;
printf("Vowels will be printedn");
printf("Enter a stringn");
/*Reading the string into an array*/
/*the loop terminates when the newline character is read*/
while((ch = getchar()) !='n')
if (i < size)
/*Storing each character into the string*/
str [i++] = ch;
/*Inserting the null terminator at the end of the string*/
str[i]=' ';
printf("%sn",str);
/*Call function to printvowels*/
printVowels(str);
}
void printVowels(char *s)
{
/*Base Case...an EMpty string*/
if (*s==' ')
printf("NO string enteredn");
/*Checking for Vowels*/
if((*s=='a')||(*s=='e')||(*s=='i')||(*s=='o')||(*s=='u'))
printf("%sn",printVowels(s));
|
You are asking for the return-value of a void function to be printed as
a string -- this is not possible.
What you meant to write (replace the printf line) is:
fputc(*s, stdout);
printVowels(s);
Although it's a dumb idea to write this function recursively.
comp.lang.c.moderated - moderation address: [email]clcm (AT) plethora (DOT) net[/email] -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
|
|
| Back to top |
|
 |
Rahul Chandok Guest
|
Posted: Tue Jan 10, 2006 3:38 am Post subject: Re: RECURSIVELY....Printing Vowels from a string |
|
|
HI,
In the function "void printVowels(char *s)" you are calling the below
mentioned line.
printf("%sn",printVowels(s));
As printVowels function will return void, so it is equivalent of
passing void to the printf function.
Please modifiy this line accordingly and the error will go away.
--> printf("%sn",printVowels(s));
HTH
Rahul
--
comp.lang.c.moderated - moderation address: [email]clcm (AT) plethora (DOT) net[/email] -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
|
|
| Back to top |
|
 |
rayw Guest
|
Posted: Tue Jan 10, 2006 3:40 am Post subject: Re: RECURSIVELY....Printing Vowels from a string |
|
|
"lauryn" <brownluv25 (AT) iwon (DOT) com> wrote
| Quote: | I am trying to write a program to recursively print all the vowels in a
string...I keep getting this error message.(Value of type void is not
allowed in function printVowels)It is directing me to the last line in
the function printvowels...... and I am using turbo C++ 4.5??
Any help will be appreciated....thanx....
#include
#define size 10
void printVowels(char*);
void main()
{
char str[size];
char ch;
int i=0;
printf("Vowels will be printedn");
printf("Enter a stringn");
/*Reading the string into an array*/
/*the loop terminates when the newline character is read*/
while((ch = getchar()) !='n')
if (i < size)
/*Storing each character into the string*/
str [i++] = ch;
/*Inserting the null terminator at the end of the string*/
str[i]=' ';
printf("%sn",str);
/*Call function to printvowels*/
printVowels(str);
}
void printVowels(char *s)
{
/*Base Case...an EMpty string*/
if (*s==' ')
printf("NO string enteredn");
/*Checking for Vowels*/
if((*s=='a')||(*s=='e')||(*s=='i')||(*s=='o')||(*s=='u'))
printf("%sn",printVowels(s));
}
|
int main(void)
{
char str[size];
printf("Vowels will be printedn");
printf("Enter a stringn");
fgets(str, sizeof(str), stdin);
printVowels(str);
return 0;
}
void printVowels(char * s)
{
if(*s == ' ')
{
puts("nDone");
}
else
{
if((*s == 'a') || (*s == 'e') || (*s == 'i') || (*s == 'o') || (*s
== 'u'))
{
printf("vowel %cn", *s);
}
else
{
printf("non-vowel %cn", *s);
}
printVowels(s + 1);
}
return;
}
--
comp.lang.c.moderated - moderation address: [email]clcm (AT) plethora (DOT) net[/email] -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
|
|
| Back to top |
|
 |
ronald29x Guest
|
Posted: Tue Jan 10, 2006 3:41 am Post subject: Re: RECURSIVELY....Printing Vowels from a string |
|
|
The Error
-------------
the last line:
printf("%sn",printVowels(s)); <-- this is definitely wrong
that's why u got the error message. The reason is because you want to
print a string from a void type function ( which is printVowels ).
printVowels is a void type function, you can do that if the printVowels
has a string type ( char* )
Solution:
------------
there are many ways to solve this problem, this one is just one of them
( remember that ok )
void printVowels( char* str, int n )
{
if( str[n] == ' ' ) return;
if( ( s[n] == 'a' ) || ( s[n] == 'e' ) || ( s[n] == 'i' ) || ( s[n]
== 'o' ) || ( s[n] == 'u' ) ) printf( "%c", s[n] );
printVowels( str, n+1 );
}
fill n with zero at first call of the function ( printVowels( "abcdef",
0 ); )
Tips:
------
- you should not use the recursive algorithm to solve this problem. You
can use iteration to solve this problem, and you will get a faster and
simpler code.
- it will be more simple if u use scanf() for the input
hope this helps
--
comp.lang.c.moderated - moderation address: [email]clcm (AT) plethora (DOT) net[/email] -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
|
|
| Back to top |
|
 |
Keith Thompson Guest
|
Posted: Wed Jan 25, 2006 11:08 am Post subject: Re: RECURSIVELY....Printing Vowels from a string |
|
|
"Michael Tiomkin" <tmk (AT) netvision (DOT) net.il> writes:
| Quote: | lauryn wrote:
[...]
void printVowels(char *s)
{
/*Base Case...an EMpty string*/
if (*s==' ')
printf("NO string enteredn");
/*Checking for Vowels*/
if((*s=='a')||(*s=='e')||(*s=='i')||(*s=='o')||(*s=='u'))
printf("%sn",printVowels(s));
}
[...]
1. You can use 0 instead of ' ', they have the same value, and 0 is
shorter.
|
But ' ' is clearer in this case, since *s is a character.
The fact that 0 is shorter isn't a significant advantage; the Great
Keystroke Shortage ended several years ago.
--
Keith Thompson (The_Other_Keith) [email]kst-u (AT) mib (DOT) org[/email] <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
--
comp.lang.c.moderated - moderation address: [email]clcm (AT) plethora (DOT) net[/email] -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
|
|
| Back to top |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|