another c++ help thread =)

Pinkpig

Member
Jul 12, 2001
134
1
81
Hello all

can this code be written with a recursive function??
if it could, can anyone show me?? thanks!!


int main()
{



string olo("I love C++");
int k1, k2=0;

for (k1=0;k1<olo.length();k1++)
{ k2=k2+olo.at(k1);
}
cout<<k2<<endl;


cout << "decimal: " << k2 << endl;

// print hex with leading zeros
cout << "hex : ";
for (int i=2*sizeof(int) - 1; i>=0; i--)
{
cout << "0123456789ABCDEF"[((k2 >> i*4) & 0xF)];
}
cout << endl << endl;




return 0;



}


Laura
 

gwlam12

Diamond Member
Apr 4, 2001
6,946
1
71
i dont think main can call itself so you'll probably have to create a separate function. i think anything that looops can be recursive.
 

gwlam12

Diamond Member
Apr 4, 2001
6,946
1
71
dang, i hope this isn't for a hs course cuz i never learned that stuff in high school.

anyway...are u converting your string to decimal and hexadecimal format?
 

Pinkpig

Member
Jul 12, 2001
134
1
81
I think i kinda got it..... but i cant seem to get it compiled. Can anyone take a look at it?
errors i get are
syntax error : missing ';' before identifier 'olo'
'string' : missing storage-class or type specifiers
unexpected end of file found

help!! =)

#include <iostream.h>
#include <string.h>

string olo("I love C++");

char recur(int nPos)
{
return olo.at(nPos);
}

int main()
{

int k1, k2=0;

for (k1=0;k1<olo.length();k1++)
{ k2=k2+recur(k1);
}
cout<<k2<<endl;


cout << "decimal: " << k2 << endl;

// print hex with leading zeros
cout << "hex : ";
for (int i=2*sizeof(int) - 1; i>=0; i--)
{
cout << "0123456789ABCDEF"[((k2 >> i*4) & 0xF)];
}
cout << endl << endl;




return 0;



}


thanks
laura
 

Calin

Diamond Member
Apr 9, 2001
3,112
0
0
You could use plain char[] for string, and have something like:

long recur(char *string)
{
if(string[0] = '\0'
return 0
return string[0] + recur(string+1)
}

main()
{
long n;
char string[] = "I love C";
n = recur(string);
printf("%h\n", n
}


this should work (but I haven't tested it). Anyway:
string[0] = the first char in the string
string+1 = the string without his first character

Tell me if it helped you
rolleye.gif


Calin

EDIT: porting this C program to C++ (including string) is a easy to do task, and should be let as an exercise for the reader. I think it's better to let the one that actually should do the work to do a bit of it
 

manly

Lifer
Jan 25, 2000
10,124
1,362
126
You've got to love cryptic compiler error messages.

I'm pretty sure the compile errors are syntax mistakes in declaring and initializing strings. Your code should be:

string old = "A literal string";

I didn't really look through the rest of the code, so let us know if there are follow-up questions.

By the way, advocating a C string (a char array) vs. a C++ string type is not recommended. C++ strings are simply safer and more powerful at the same time. All Calin did was port the code to C, which is probably not what you wanted.
 

Ynog

Golden Member
Oct 9, 2002
1,782
1
0
Just to point out. Some compilers have include a string data type. Others do not.
This is because String is not part of the C++ ANSI standard (As of yet).

Some compilers or systems come with a string type so you can use that as a string
data type. However if you are using a compiler such as g++, string will cause
compiler errors.

If you have a string data type, borland does or did (been awhile since I used that compiler),
use that one. Its always better. However like Calin pointed out. You must use char arrays
otherwise. Manly is sort of right, if you can use a C++ string type, use it, however it most
cases the string type doesn't exist. There is another option though.

Now there is always the Standard Template Library String class. This is different than
a string type. Using this is adviced as well, over creating your own String class to deal with
Strings however since this is a simpile program (mostlikely some time of hw assignment)
use a char array, because the use of the String class in the standard template library
might not be what your teacher/professor had in mind.
 

manly

Lifer
Jan 25, 2000
10,124
1,362
126
I think your information is a bit outdated. ISO standardized C++ in 1998, so the compiler makers have had 4 years to implement the official standard. AFAIK string is part of the standard C++ library.

g++ 2.95.3, known as an older C++ compiler that isn't fully up to spec, does in fact support the string type. GCC 3.2 (the latest version) is supposed to have very good ISO C++ support.

The only reason nobody implements the full standard is because C++ is a ridiculously gargantuan spec that's hard enough to master as a programmer, let alone as a language tools vendor. But most recent compilers do a fairly good job of things, even the notoriously proprietary MS VC++ compilers (as of version 6.0 or later). Sorry for diverting this thread from the original topic.
 

Ynog

Golden Member
Oct 9, 2002
1,782
1
0
I apologize for still being completely off topic, but I am curious

Manly,

I mean no offense here. My information may be a bit outdated, I won't refute that. However a current flip through a ANSI C++, I cannot
find string listed as a keyword. Also I have a 2.96 g++ compiler, a MS VC++ compilter and a SGI C++ compiler, and cannot get
string data type to work. I can however get the String Class out of the Standard Template Library to work. Not sure what AFAIK is?
I'll be the first to admit, I could be doing something wrong. However I mean no offense but where in the Standard is string pointed out.
String literals do not mean string data type. And the only reference to string "descriptor", that I found, noted with a comment that it came
from the Standard Template Library.

Now I don't argue that the C++ standard is ridiculously gargantuan. I would like to be pointed in the right direction on where exactly
it says its part of the standard. If you don't know, you don't have to go look it up, I am just curious and am by no means trying to attack you.
I have looked for it for awhile without finding it and would like to be pointed in the right direction. Also if you could give me a sliver of code
you know runs under g++ 2.95, I would appreciate it. I do program in C++, but mostly C due to real time requirements. However I am begining
a new project with C++, and if String is indeed part of the Standard and not just part of the Standard Template Library, meaning that I have a compiler
that doesn't support it, I would like to force the system adminstrator of the Lab I am working in to upgrade to a compiler that is ANSI complinant C++
compiler. Thanks Alot


One more thought,

If you want to argue that the standard template library implemenation of string, makes it a string type, then I will sort of agree with you.
 

helppls

Senior member
Jun 19, 2001
216
0
0
recurisve main.. fuctions.... hm.....

i'd try this:

int main(int argc, char* argv[])
{
/* do stuff here */
system(argv[0]);
return 0;
}

kind of like a recursive fuction...
 

Calin

Diamond Member
Apr 9, 2001
3,112
0
0
Dear manly

However hard I look into the example of code I "translated to C", I am unable to find a recursive function. And I could use the <string>.Right, and <string>.Left methods but I was not sure about their syntax/parameters and so on. As Laura was unable to solve the error messages, I considered was also unable to solve the errors coming from bad parameter order.
I know that "string" type is a much better solution than simple char[], but somehow I am used with stdlib.h and not the newer way
Please look also at my edit on first message

Calin

EDIT: and the solution is (tested this time):

#include <stdio.h>

long recur(char *s)
{
if(s[0] == '\0')
return 0;
return s[0] + recur(s+1);
}

main()
{
long n;
char string[] = "I love C";
n = recur(string);
printf("%x\n", n);
}

where s[0] == '\0' stands for "string s is empty"
 

manly

Lifer
Jan 25, 2000
10,124
1,362
126
Ynog, the following trivial application compiles with GCC 2.95.3, GCC 3.2 and VC++ 7.0:
#include <string>
#include <iostream>

using namespace std;

int main() {
string s = "Hello, standard C++!";

cout << s << endl;

return 0;
}
AFAIK = As far as I know. I have to admit I'm not much of a C++ developer these days, so don't debate me on any finer points of the language. ;) Supposedly GCC 2.95.3 is a relatively ancient C++ compiler compared to either 2.96 (Red Hat's fork) or 3.2.

I don't have the standard language spec handy, but referring to Stroustrup's classic text, The C++ Programming Language, 3e on page 48, he writes:
3.5 Strings
The standard library provides a string type to complement the string literals used earlier. The string type provider a variety of useful string operations, such as concatenation.
...
I generally refer to this text for basic language details of modern/standard C++.
 

helppls

Senior member
Jun 19, 2001
216
0
0
...unlike many STL include files, string.h did not get upgraded to string... no ANSI compiler would ever put a string class in string.h because ANSI uses string.h for its own things (strcat,memset...) in other words, that's what pinkpig's error is comming from in his second pasting

also, the c++ ANSI standard has reached its final modification, from at least my understanding, and string is not a keyword...

(from memory, sorry if i forget one): char,wchar_t,bool,int,double,float are the only types that are 'keyworded'...
 

manly

Lifer
Jan 25, 2000
10,124
1,362
126
All of The C++ Programming Language 3e chapter 20 is devoted to C++ strings. string is not a keyword, but it's a class in the standard library. At least to the best of my reading ability. ;)

And yeah the compile error was due to incorrectly including <string.h> instead of simply <string>

The constructor style initialization syntax string olo("I love C++"); happens to be perfectly valid.
 

ASK THE COMMUNITY