|
-
Sep 13th, 2002, 11:12 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] Returning a Char from a Function
I'm trying to return a single char from a function, but I'm not doing it right. Can someone tell me how? Here's what I have:
PHP Code:
#include <iostream.h>
#include <string>
char show_choice(bool clear_screen = true);
int main()
{
char user_choice[1] = "";
do //Loop while the users doesn't want to quit
{
//Get the user's choice
user_choice[1] = show_choice();
} while (strcmp(user_choice, "Q") != 0 && strcmp(user_choice, "q") != 0);
return 0;
}
char show_choice(bool clear_screen /*= true*/)
{
//if (clear_screen == true) ;
char user_choice[1] = "";
do
{
cout << "U = Enter web page URI\n" <<
"O = Options\n" <<
"H = Help\n" <<
"Q = Quit\n";
cout << "Please enter your choice: ";
cin >> user_choice;
} while (strcmp(user_choice, "U") != 0 && strcmp(user_choice, "u") != 0 &&
strcmp(user_choice, "O") != 0 && strcmp(user_choice, "o") != 0 &&
strcmp(user_choice, "H") != 0 && strcmp(user_choice, "H") != 0 &&
strcmp(user_choice, "Q") != 0 && strcmp(user_choice, "q") != 0);
return user_choice[1];
}
(they need some real C++ highlighting, so it says C++ code: )
Last edited by Rick Bull; Sep 13th, 2002 at 12:11 PM.
-
Sep 13th, 2002, 11:25 AM
#2
Monday Morning Lunatic
Code:
char user_choice[1] = "";
Try just char user_choice = '\0' and compare only the first character (using == rather than strcmp).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 13th, 2002, 11:26 AM
#3
Frenzied Member
Code:
char user_choice;
do
{
cout << "U = Enter web page URI\n" <<
"O = Options\n" <<
"H = Help\n" <<
"Q = Quit\n";
cout << "Please enter your choice: ";
cin >> user_choice;
} while ((user_choice != 'U') && (user_choice != 'u') && (user_choice != 'O') && (user_choice != 'o') &&
(user_choice != 'H') && (user_choice != 'h') &&
(user_choice != 'Q') && (user_choice != 'q'));
return user_choice;
Should work.
Z.
-
Sep 13th, 2002, 11:50 AM
#4
Thread Starter
Frenzied Member
Thanks for the help guys. It's working now! So what's the difference between using single and double quotes?
-
Sep 13th, 2002, 01:07 PM
#5
Monday Morning Lunatic
Double quotes creates a const char*, i.e. a pointer to a pre-allocated section of memory. Single quotes surround *one* character.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 13th, 2002, 01:52 PM
#6
Thread Starter
Frenzied Member
I get ya (I think ). Thanks.
-
Sep 13th, 2002, 05:18 PM
#7
Frenzied Member
'a' == 'a' != "a" == |'a'|'\0'|
If that makes sense =).
Z.
-
Sep 14th, 2002, 04:41 AM
#8
Thread Starter
Frenzied Member
Umm, almost! So "a" is just 'a' with a null char at the end?
-
Sep 14th, 2002, 05:00 AM
#9
Monday Morning Lunatic
Yes, but stored elsewhere.
Ok, you have your function:
Code:
void function() {
char a = 'b';
const char* b = "hello world!";
char c[] = "another string";
}
There, on the function's stack, there is the character 'b', then a pointer, then enough space to fit in "another string". This is why I made the distinction with const.
The "hello world!" is stored somewhere completely different, all that is kept with the function is a pointer that you cannot change.
You can, of course, modify c as long as you don't change the length
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 14th, 2002, 08:36 AM
#10
Thread Starter
Frenzied Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|