HELP!!

Hey all, hope you are doing good. I have a question to ask and it has been bothering me for a while now and I can't seem to figure it out. So i decided to create a simple program where it can detect if u enter a number or a letter. However, its not going the way i want it to cuz the logic in my head somehow it's not working with the program. Need some help.

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
28
29
30
  #include<iostream>
using namespace std;

void number (int);

int main ()
{
    int x = 0;
    cout << " \nPLease enter a something: ";
    cin >> x;
    cout << endl;

    number(x);


return 0;
}

void number(int x)
{
    if (x >= 0)
        cout << " it is a number!";
    else if (x <= 0)
        cout << " it is a Number!";
    else
        cout << "it is a Letter!";

    cout << endl;

}
Try outputting the letter or number with its type.
1
2
3
4
5
6
7
8
9
10
11
12
void number(int x)
{
	if (x >= 0)
		cout << " it is a number!" << x;
	else if (x <= 0)
		cout << " it is a Number!" << x;
	else
		cout << "it is a Letter!" << x;

	cout << endl;

}

See what happens?

If you input a number you get the number if you input a letter you get 0. Don't forget your variable is an int. It's not built to store a char.

Now to find a solution. If all you want to do is test if its a number or a character then you could test if the input failed. The one disadvantage is that if you wanted to use the letter you cant.
1
2
3
4
5
6
7
8
9
10
void number(int x)
{
	if (!cin.fail())
		cout << " it is a number!" << x;
	else
		cout << "it is a Letter!";

	cout << endl;

}


Another option would be to get the input as a string and then test which it is. In this case, you would also gather the number or the letter.
I'm not sure of the logic in your head, either. Being a psychic sure would help in times like these, haha. I think Duthomhas might be one.

So, to start... what makes you think that a negative number is not a number? You hurt -1's feelings, you know.

But on a more serious note, by starting your data type as an int, you are already starting off on the wrong leg, because what else can an int be interpreted as, other than an integer number? An int means integer, i.e the countable set {..., -2, -1, 0, 1, 2, 3, ...}. It couldn't hold a value like "a" or "apple" to begin with.

Basically, what you want to do is initially request the input in the form of a string, because a string could be "apple", "-3.14", "1", or "0xDEADBEEF", etc. Then, you need to logically parse the string to see if it can be interpreted as a number. C++ provides a relatively easy way to do this for most use cases, with std::stringstreams.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Example program
#include <iostream>
#include <string>
#include <sstream>

int main()
{
    using namespace std;
    
    cout << "Enter a number, or whatever: ";
    string str;
    cin >> str;
    
    istringstream ss(str);
    int num;
    if (ss >> num) // attempt conversion from istringstream to int
    {
        cout << "It's a number! num = " << num << endl;   
    }
    else
    {
        cout << "Not a number I can recognize." << endl;
    }
}


__________________________________

Edit: joe's method will work fine, too. My only minor criticism is that the cin.fail check is separated from the call to cin itself, leading to external dependencies in the interface.

The following could work as well, just a simplified version of joe's
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Example program
#include <iostream>
#include <string>

int main()
{
    using namespace std;
    
    cout << "Enter a number: ";
    int n;
    if (cin >> n)
    {
        cout << "You entered " << n << endl;
    }
    else
    {
        cout << "Hey! You didn't enter a number..." << endl;
    }
}

Last edited on
Yeah, something is really wrong with my logic, but thanks so much joe86464 and Ganado. i starting to understand how it works. but that #include<sstream> is new to me so it kinda lost me with that.
Topic archived. No new replies allowed.