-1
char x;
bool tf;
void IsNumber(char x)
{
    switch (x)
    {
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
        case '0':
            bool tf(true);
            break;
        default:
            bool tf(false);
            break;
    }
}

int main(int argc, const char * argv[])
{
    using namespace std;
    cout << "Test if a character is a number: " << endl;
    char x;
    cin >> x;
    IsNumber((char) x);
    if (bool tf = true)
        cout << "True" << endl;
    if (bool tf = false)
        cout << "False" << endl;
    return 0;
}

I am getting an error after the default: saying I can't redefine the variable. Also, it says, at the bottom, that I the variable tf isn't a variable. I am really new to C++, I only know python can someone help me out?

New code giving me an error:

#include <iostream>
bool tf;
tf = true;
bool IsNumber(char x)
{
    switch (x)
    {
        case '1':
  • You need `==` for checking equality, you don't need to keep putting in all the `bool`. Also, if `x` is char, you don't need to cast it. I would suggest starting with a textbook or other tutorial to start with the basics. – crashmstr Nov 21 '13 at 23:09
  • Just change `bool tf(true);` to `tf = true;` – Jerry Jeremiah Nov 21 '13 at 23:11
  • This program should have no global variables and booleans should be compared with `if (b)` and `if (!b)`, not with == and !=. – chris Nov 21 '13 at 23:12
  • And `bool tf(false);` to `tf = false;` – john Nov 21 '13 at 23:12
  • 3
    The basic problem is that the code is so bad, and the question is about such a minor point, that you are getting multiple answers to questions you didn't ask. Take some time to learn C++, it's not an easy language and completely and utterly different from python. – john Nov 21 '13 at 23:14

4 Answers4

6

You're trying to declare two variables with the same name in the same scope. The switch statement itself defines a scope, but each case clause does not. If you want a scope within a case clause, then you'll have to provide one yourself:

case '0':
{
    bool tf(true);
    break;
}

Although it's not clear what good that is; you're declaring a local variable then ignoring it. Perhaps you meant to set the global variable:

case '0':
    tf = true;
    break;

so you could then check it with

if (tf) // NOT if (bool tf = true) - that also declares a new local variable

or, less freakily, perhaps you want to return a value:

bool IsNumber(char x) {
    switch (x) {
        case '0': 
        // and so on
            return true;
        default:
            return false;
    }
}

and test it with

if (IsNumber(x)) {
    cout << "True" << endl;
} else {
    cout << "False" << endl;
}

or, if verbosity isn't your thing

cout << (IsNumber(x) ? "True" : "False") << endl;

or even

cout << boolalpha << IsNumber(x) << endl;

You should probably take some time to read an introductory book until you're comfortable with the basics such as scopes, declarations and definitions. Here are some recommendations.

Community
  • 1
  • 1
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • This is helpful, except when i do the tf = true; part it says C++ needs a global identifier – user2312690 Nov 21 '13 at 23:19
  • @user2312690: I don't see how you could get that error from that code. You declare the global variable `tf` in the second line. (Although, once you've learnt the basics of the language, you'll understand why you don't want a global at all). – Mike Seymour Nov 21 '13 at 23:21
  • @user2312690: I meant, you need `tf = true;` *inside* the switch statement, *instead of* `bool tf(true);` which declares a new local variable instead of setting the global variable. You can't write random bits of code outside functions. – Mike Seymour Nov 21 '13 at 23:25
0

You have x defined at the very top as a global variable, and then try and define it again in main. You have to pick one or the other, or rename one. What you probably want to do is get rid of the one in main.

You also have your bools compared as = instead of how they should be like ==.

Edit: You actually don't need the comparison in there at all.

if(tf){
    cout << "True\n"
}
else cout << "False\n"

That will see if tf is true and output true and if not, output false.

Seldom
  • 259
  • 2
  • 4
  • 14
0

You haven't assigned anything to tf, for comparison operators we want =='s instead of just the ='s.

if (tf == true) {
}
Mathemats
  • 1,185
  • 4
  • 22
  • 35
  • If you insist on comparing booleans with `true`, then remember that the result of the comparison is also a boolean. That should be `if ((tf == true) == true)` – Mike Seymour Nov 21 '13 at 23:23
0

You can only define a variable one you can assign it as many times as you need.

bool tf = false 

means I am define a variable and assigning it

if I just want to assign it later

tf = false 

if I want to do a conditional test

if (tf){do stuff} 
rerun
  • 25,014
  • 6
  • 48
  • 78