Hello World V 3

This is a discussion on Hello World V 3 within the C++ Programming forums, part of the General Programming Boards category; I am just starting to learn C++ and having a wonderful time with it.
Here are my questions:
1: How ...

Hello World V 3

I am just starting to learn C++ and having a wonderful time with it.
Here are my questions:
1: How could I use 1 function to do the work of getting and returning both names, the shortest way possible?
2: I can not find how to compare strings in the if (which is why it compares 1 & 2 rather than Y & N)
3: I would like to find out how to return to verify name if an invalid answer is given.

Also, at one point, you combine firstName and lastName into fullName, when you could just skip that line of code and output both variables the same way you put them into fullName.

And you do need int to declare your main, not void.

In answer to your one function question, just take the code inside your firstName and lastName functions and put the code where you called those functions in main. They are already as small as you can get them.

And as for comparing strings, thankfully, you don't have to (I'm not sure how to do that myself). You only have to compare characters, which is possible, and easy. First, make sure to declare "char verify" instead of "int verify", and then, when comparing, do like so:

Code:

if ((verify == 'y') || (verify == 'Y'))
{
//conditional code here
}

The reason I have both the lower case and the upper case in there is because if you only have "Y" in the conditional, and the user inputs "y", the program will decide that the conditional was not met. The logical OR operator "||" allows for either occurrence to happen for a success.

You can use the same type of comparison to make a loop around the name verification process, such as do...while. You tell it to do (automatically run once) the name verification, and set the conditional to be "while (!((verify == 'y') || (verify == 'Y') || (verify == 'n') || (verify == 'N'));". This is just what I've found to work, and if there's a shorter way to do that, then tell me, please.

Finally, you'd probably want to put a loop around the name entry process AND the name verification loop to provide for the possibility that the person didn't enter their name correctly. That will eliminate the seeming need to call main() from within itself.

Never call the main() function from within a program. Its sole purpose is to act as the starting point of a program.

I don't really see a problem with that. It's a function like any else. But you probably won't do it since any code that's recursive will probably reside in its own function.

Originally Posted by Molokai

And as for comparing strings, thankfully, you don't have to (I'm not sure how to do that myself). You only have to compare characters, which is possible, and easy. First, make sure to declare "char verify" instead of "int verify", and then, when comparing, do like so:

Code:

if ((verify == 'y') || (verify == 'Y'))
{
//conditional code here
}

The reason I have both the lower case and the upper case in there is because if you only have "Y" in the conditional, and the user inputs "y", the program will decide that the conditional was not met. The logical OR operator "||" allows for either occurrence to happen for a success.[/code]
The only problem here is that if you use a single char for input, you'll get a buffer overrun (since it always always write a '\0' at the end of the array). At least you need to do an array 2 two elements. But that's risky too if the user enters more than one char. No, just use string.

[QUOTEMolokai;684731]You can use the same type of comparison to make a loop around the name verification process, such as do...while. You tell it to do (automatically run once) the name verification, and set the conditional to be "while (!((verify == 'y') || (verify == 'Y') || (verify == 'n') || (verify == 'N'));". This is just what I've found to work, and if there's a shorter way to do that, then tell me, please.

It's easier just to do a never-ending loop and use continue and break to control its flow. Because it's much easier to read.

Originally Posted by mikeman118

Also, I'm pretty sure it's not possible to call main() from inside your application. If you tried to compile that you would get an error.

Sure it is!

Code:

int main()
{
main();
return 0;
}

Sure, it will loop forever and you'll probably get a warning, but it works just fine (compiles without a hitch).

Also, at one point, you combine firstName and lastName into fullName, when you could just skip that line of code and output both variables the same way you put them into fullName.

And you do need int to declare your main, not void.

In answer to your one function question, just take the code inside your firstName and lastName functions and put the code where you called those functions in main. They are already as small as you can get them.

Is there not a way to pass a variable back to main from a function?
In another language it is possible by passing the input from the function back to the main through a function parameter. I do not know enough about C++ yet to know if that is even possible.

Originally Posted by Molokai

And as for comparing strings, thankfully, you don't have to (I'm not sure how to do that myself). You only have to compare characters, which is possible, and easy. First, make sure to declare "char verify" instead of "int verify", and then, when comparing, do like so:

Code:

if ((verify == 'y') || (verify == 'Y'))
{
//conditional code here
}

The reason I have both the lower case and the upper case in there is because if you only have "Y" in the conditional, and the user inputs "y", the program will decide that the conditional was not met. The logical OR operator "||" allows for either occurrence to happen for a success.

You can use the same type of comparison to make a loop around the name verification process, such as do...while. You tell it to do (automatically run once) the name verification, and set the conditional to be "while (!((verify == 'y') || (verify == 'Y') || (verify == 'n') || (verify == 'N'));". This is just what I've found to work, and if there's a shorter way to do that, then tell me, please.

Finally, you'd probably want to put a loop around the name entry process AND the name verification loop to provide for the possibility that the person didn't enter their name correctly. That will eliminate the seeming need to call main() from within itself.

Yes I understand the need for Y and y as acceptable replies, that leads into questions I reserved for later like how to force an input string into all lowercase rather than trying to check things like Yes, YES, yEs etc..
Unfortunately if(veryfy == "y") caused compile error stating:
"ANSI C++ forbids comparison between pointer and integer"
Which I did not understand because verify was declared as a string variable and I thought "Y" was also a string, so how is this comparing an integer? However I notice now you used 'y' and I used "Y".. what is the difference there? Single quote marks compiles and compares the character while the double quote marks causes compile error.

Also, I'm pretty sure it's not possible to call main() from inside your application. If you tried to compile that you would get an error.

Comparing string variable to string variable works fine, I had no trouble with that, it was comparing a string variable to a definate- hard coded string... Its the problem with '' and "" marks that I am not understanding which seems to be the problem. However the full code I posted V_3_00_00 compiles and executes error free. (with the exception of runtime errors in that the script does not operate the way it is desired)

>> can you explain int main(void) vs int main()
They are the same in C++. Use int main() since that is the normal way to do it.

>> will I also have to return a value at the end? ie return 0;
With a proper compiler you don't. For normal functions you would, but main is special in that you can write int main() but leave out the return 0 and the compiler will return 0 for you.

>> Thanks again, how should I return to the beginning?
Use a loop. You can also create a separate function that handles most of what is in your main function and then call that recursively. It's just that main is special and you shouldn't call it like a normal function.

>> Is there not a way to pass a variable back to main from a function?
Yes, you can return it. You can also pass a parameter or multiple parameters by reference so that the variable(s) in main are updated.

Single quotes in C++ indicate a single character of type char. So 'a' is a character. Double quotes indicates a string, so "a" is a string even though it includes only one character.

The C++ string class holds a string, so to compare a string variable to a literal string the literal string has to have double quotes, so this should work:

Code:

std::string str = "y";
if (str == "y")

If you want to check upper and lower together, you have to lower each character separately. Or, you could just make your variable a char and read in a single character:

Thanks for the tip, can you explain int main(void) vs int main(), and if using either of these will I also have to return a value at the end? ie return 0;

Thanks again, how should I return to the beginning?

int main()and int main(void) is the same in C++.
Usually, you return 0 at the end, but it's really up to you.

Originally Posted by SRS

Is there not a way to pass a variable back to main from a function?
In another language it is possible by passing the input from the function back to the main through a function parameter. I do not know enough about C++ yet to know if that is even possible.

Quite possible. See references which I replied about above.

Originally Posted by SRS

Yes I understand the need for Y and y as acceptable replies, that leads into questions I reserved for later like how to force an input string into all lowercase rather than trying to check things like Yes, YES, yEs etc..

There are member functions in the string class, I believe that can convert it all into lower case.

Originally Posted by SRS

Unfortunately if(veryfy == "y") caused compile error stating:
"ANSI C++ forbids comparison between pointer and integer"
Which I did not understand because verify was declared as a string variable and I thought "Y" was also a string, so how is this comparing an integer? However I notice now you used 'y' and I used "Y".. what is the difference there? Single quote marks compiles and compares the character while the double quote marks causes compile error.

verify has to be an int if you get that error.
As for the difference between " and ', 'y' == char, "y" == char* (pointer).
What is actually means is that "y" is the same as 'y', '\0'. When you compare just one character, use '. Otherwise, if you compare a string, use ".

>> There are member functions in the string class, I believe that can convert it all into lower case.
There aren't any for the string class. You have to loop through and lower each character separately. You can do it in a single line of code with an algorithm.

Daved & Elysia TY

Originally Posted by Daved

1. Use int main() since that is the normal way to do it.
2. With a proper compiler you don't. ...and the compiler will return 0 for you.
3. You can also create a separate function that handles most of what is in your main
4. Single quotes in C++ indicate a single character of type char. Double quotes indicates a string.
5.
code]char ans;
cin >> ans;
if (tolower(ans) == 'y')[/code]

1. Got it
2. Using Dev-C++ and it does ty
3. Working on that now, may have questions...
4. Got it, that was actually my problem I had used char verify
expecting a letter reply and compared to "Y"(string)
5. Good show... tolower is exactly what I had in mind TY greatly

----------------------------------------------------------------------------------------------------------------
Elysia
TY so much. I am working on the excellent sample you put up with my code introducing the for loop. (I errored as far as the return point I found out when I ran it, as a reply of 2 sends it into the endless loop, but starting the loop above the function calls at the beginning of main worked as intended) TYVM for the help.

>> The more I look at string, the more useless it seems.
I completely disagree with your opinion of string, but feel free to start a thread on it if you'd like to discuss it. Short answer is that many C++ experts feel the class is too bloated, while you seem to want it to have more functions. Also, if you can make the string lowercase in a single line of code, why does it matter that it doesn't use a member function?