Trouble with I/O for functions

Hi, all.
This is my first post on this site and I'm VERY new to C++ (programming in general).

I wanted to practice setting up a basic function and have this function be called into main() if the user's response is correct.
Answering yes to the prompt should call the function; whereas, answering no (or anything else) won't call the function, but display a different message instead.

I have tried to go through the tutorials, but I still can't seem to figure this out.



Here's my code:


#include <iostream>

using namespace std;


// Function prototype
char test_display();


// The function that will be called

char test_display()
{
// This is what should be displayed
// If the user types "yes" or "Yes"

cout << "This is a test!" << '\n'
<< "and it worked!" << '\n';
return 0;
}


int main()
{
// This is where the user will input their response

char response;
cout << "Lets work on input & output!" << '\n'
<< response << "Should we test it?(yes / no)" << '\n';


// the input should be yes (non-case sensitive) to call the function
// the else is there to test the input

cin >> response;
if(response == "Yes" || response == "yes")
return test_display();
else
cout << "It doesn't work... you suck at this.";
return 0;
}

**************
These are the errors I get from my compiler:

In function 'int main()':
35:20: warning: comparison with string literal results in unspecified behaviour [-Waddress]
35:20: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
35:41: warning: comparison with string literal results in unspecified behaviour [-Waddress]
35:41: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
**************

Any & all help is appreciated!
Thanks!
char is 1 character. That is enough space to store one single letter, in other words. You need to either use the string class (string s = "hey") or a character array (char oldschool[20] = "stuff");

You can compare strings with ==, but not char arrays. Those use old functions like strcmp and are left over from C, looks like

if( strcmp(oldschool, "whatever") == 0) //they are the same, else not.

to do non case sensitive work, most programmers just upper case or lower case the input and compare that to one value. There are functions for this for both string and char array, look up "toupper" or "tolower".

your errors are trying to tell you that you can't compare a single character to a string literal ("words" is a string literal, and this is pretty much a constant character array) using ==

Thanks for the help! Sorry it took me so long to reply.

If I understand correctly, I should use string if they type the word "yes" or "no"; whereas, char should be used for input like "y" or "n", as these are single characters and not "literal strings".
Then, I can use strcmp() to compare the input to a value and do whatever the respective conditional says.

If that's the case, does it matter what value I compare it to?
Topic archived. No new replies allowed.