Need help with single char value

I've look all over the forum for help but I don't understand those solutions. I'm a newbie. I need to check the first char in the customer number and test whether it is 0 or not.

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
//If customer number begins with 0 customer will qualify for discount
#include <iostream>
#include <string>
using namespace std;

bool qualifyForDiscount (string customerNoP)
{
    customerNoP = customerNoP[0];
    if (customerNoP == '0')
        return true;
    else
        return false;
}

int main()
{
    string customerNo;

    cout << "Enter customer number: ";
    cin >> customerNo;
    cout << qualifyForDiscount(customerNo) << endl;

    return 0;
}
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
bool qualifyForDiscount (string customerNoP)
{
    //customerNoP = customerNoP.at(0);
    if (customerNoP.at(0) == '0')
        return true;
    else
        return false;
}
Last edited on
Perhaps something like the following would work.
1
2
3
4
bool qualifyForDiscount (const std::string& customerNoP)
{
    return(customerNoP[0] == '0');
}
@AlexBoggs

I don't see anything that would print out the results of the return. You should add another bool variable in main().
 
bool result;


Then place ..
result = qualifyForDiscount(customerNo);
after your cin.

Then
1
2
3
4
if (result)
  cout << "Everything is ok"<< endl;
else
  cout << "Not valid." << endl;
Last edited on
closed account (48T7M4Gy)
That's quite right whitenite1. But at the moment it either prints out 0 or 1, which is basically OK. The main problem is getting the first digit which is .at(0), not the first element in an array that doesn't exist - i.e. square brackets
@kemort

Your above code could also be written as..
1
2
3
4
5
6
7
8
bool qualifyForDiscount (string customerNoP)
{
    //customerNoP = customerNoP.at(0);
    if (customerNoP[0] == '0')
        return true;
    else
        return false;
}


Square brackets for the first number in the string.
The expression some_string[X] and some_string.at(X) are equivalent, except .at(X) will throw an exception if X is outside of range of the string.

closed account (48T7M4Gy)
@whitenite1. Sorry but square brackets don't work with a string. They would work with a c-string.

Satisfy yourself and run it, that's what I did.

Also: http://www.cplusplus.com/reference/string/string/at/
closed account (E0p9LyTq)
@whitenite1, what about std::boolalpha?

std::cout << std::boolalpha << qualifyForDiscount(customerNo) << "\n";
The std::string class has overloaded the [] to act the same as array access, look at the bottom of the link you provided you'll see the documentation for this operator.

http://www.cplusplus.com/reference/string/string/operator[]/

closed account (E0p9LyTq)
@kemort, using the bracket operator on a std::string works just fine.

http://www.cplusplus.com/reference/string/string/operator%5B%5D/
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
bool qualifyForDiscount (string customerNoP)
{
    char customerNo = customerNoP[0];
    if (customerNo == '0')
        return true;
    else
        return false;
}
And your question is?

The snippet you posted is a valid function. It will return true if the first letter of the string is the '0' character, otherwise it will return false.

The function can be greatly simplified as shown in previous posts but it is valid.

Even the functions use in main is syntactically correct. The program will print either 1 (true) or 0 (false).

closed account (48T7M4Gy)
What makes you think there was a question?


What makes you think there was a question?

so, what message you want to deliver by your code, previously?
closed account (48T7M4Gy)
The message is - read it or try it if you want to.
Last edited on
Thanks for the replies

@kemort thanks that is exactly what i needed and works great.

1
2
3
4
5
6
7
8
bool qualifyForDiscount (string customerNoP)
{
    //customerNoP = customerNoP.at(0);
    if (customerNoP[0] == '0')
        return true;
    else
        return false;
}
Last edited on
closed account (48T7M4Gy)
... and jlb too for correction on square brackets which I'd forgotten.
Last edited on
Topic archived. No new replies allowed.