Passing Strings
I have this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <stdio.h>
#include <string.h>
using namespace std;
bool validateSelection(string); //Prototype
int main()
{
int f;
string p = "QTR";
f = validateSelection(p);
if(f == 1){
cout << "Works";
}
}
bool validateSelection(string g)
{
if(g[0] == "Q") //error forbidding comparison between pointer and integer
{
return true;
}
}
|
What this code is supposed to do is check for the first letter of a string.
I'm new to pointers and how strings actually work. I have tried a mess of things and made bigger messes I didn't understand.
Thanks for any help in advance!
g[0] gives you a character. "Q" is a string. Use single quotes 'Q' if you want a character.
Oh my, it was that simple. It works fine now. I was starting to use strcmp and all these things I didn't understand.
Thank you Peter.
Topic archived. No new replies allowed.