Finding Digit in char array

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 2313 Views - Last Post: 18 November 2012 - 12:55 PM Rate Topic: -----

#1 ibe333333  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 11-August 09

Finding Digit in char array

Posted 18 November 2012 - 09:22 AM

Hello,

I've hit a roadblock in a C++ program that I am trying to write for class. I am suppose to write a function that takes accepts a char array and step through the array searching for a digit number, if any. For example, if the char array is "mnp7", it should return 7. The commented section is part of the code that I was writing that I think works without sending the array into a function, but of course the assignment asks for char array to be sent into the function.

The code does work, but it doesn't give the result I am looking for. I've stared at this for about 2 hours now and nothing seems to be standing out to me as where/what I messing up on.

#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>

using namespace std;

int getHiddenNumber(char c[]);

int main()
{
    const int SIZE = 5;
	int HiddenNum;
	char UserEntry[SIZE];
	cout << "Enter a string (up to 4 characters): ";
	cin.getline(UserEntry, SIZE);

	HiddenNum = getHiddenNumber(UserEntry);
	cout << HiddenNum << " is the Hidden Number!\n";
	/*string UserEntry;
	cout << "Please enter a string: ";
	getline (cin, UserEntry);
	cout << endl << "User Entered: " << UserEntry << endl;

	for (int i = 0; i < UserEntry.length(); i++)
	{
		if (isdigit(UserEntry[i]))
		{
			cout << "Digit found in Element " << i << ": " << UserEntry[i] << endl;
        }
	}*/

    cout << endl;
    system("PAUSE");
    return 0;
}

int getHiddenNumber(char c[])
{
	int HiddenNumber;
	for (int i = 0; i < sizeof(c); i++)
	{
		if (isdigit(c[i]))
		{
			HiddenNumber = c[i];
		}
	}
	return HiddenNumber;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Finding Digit in char array

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,613
  • Joined: 23-August 08

Re: Finding Digit in char array

Posted 18 November 2012 - 09:23 AM

Please see the function tutorials in my signature, particularly part II, for the answer to your question.
Was This Post Helpful? 0
  • +
  • -

#3 ibe333333  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 11-August 09

Re: Finding Digit in char array

Posted 18 November 2012 - 09:24 AM

Thanks. Checking now.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4234
  • View blog
  • Posts: 13,300
  • Joined: 25-December 09

Re: Finding Digit in char array

Posted 18 November 2012 - 09:26 AM

Quote

The code does work, but it doesn't give the result I am looking for.

If it is giving you the wrong results it's not working. Just because a program compiles doesn't mean it works.

First you need to send both the array and the array size to your function. The sizeof() function will only work where you define the array, not after passing the address of this array to your function.

Jim

This post has been edited by jimblumberg: 18 November 2012 - 09:26 AM

Was This Post Helpful? 0
  • +
  • -

#5 ibe333333  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 11-August 09

Re: Finding Digit in char array

Posted 18 November 2012 - 09:34 AM

View Postjimblumberg, on 18 November 2012 - 09:26 AM, said:

Quote

The code does work, but it doesn't give the result I am looking for.

If it is giving you the wrong results it's not working. Just because a program compiles doesn't mean it works.

First you need to send both the array and the array size to your function. The sizeof() function will only work where you define the array, not after passing the address of this array to your function.

Jim


Thanks. The one sticking point of the assignment, that I don't seem to understand why, is that I am suppose to use this function prototype as is: int getHiddenNumber(char c[]);
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,613
  • Joined: 23-August 08

Re: Finding Digit in char array

Posted 18 November 2012 - 09:37 AM

OK, if you HAVE to use that particular prototype, then my tutorials won't help, but here's a clue:

what character signals the end of a string (a character array)?
Was This Post Helpful? 1
  • +
  • -

#7 ibe333333  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 11-August 09

Re: Finding Digit in char array

Posted 18 November 2012 - 09:39 AM

View PostJackOfAllTrades, on 18 November 2012 - 09:37 AM, said:

OK, if you HAVE to use that particular prototype, then my tutorials won't help, but here's a clue:

what character signals the end of a string (a character array)?


thanks. i'll try with the null terminator.
Was This Post Helpful? 0
  • +
  • -

#8 ibe333333  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 11-August 09

Re: Finding Digit in char array

Posted 18 November 2012 - 09:49 AM

View PostJackOfAllTrades, on 18 November 2012 - 09:37 AM, said:

OK, if you HAVE to use that particular prototype, then my tutorials won't help, but here's a clue:

what character signals the end of a string (a character array)?


here's what I have so far, but it does not give the results i want. using tt1a as the user input, it returns 49. am i using isdigit correctly?

#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>

using namespace std;

int getHiddenNumber(char c[]);

int main()
{
    const int SIZE = 5;
        int HiddenNum;
	char UserEntry[SIZE];
	cout << "Enter a string (up to 4 characters): ";
	cin.getline(UserEntry, SIZE);
	HiddenNum = getHiddenNumber(UserEntry);
	cout << HiddenNum << " is the Hidden Number!\n";
    cout << endl;
    system("PAUSE");
    return 0;
}

int getHiddenNumber(char c[])
{
	int HiddenNumber;
	int i = 0;
	while (c[i] != '\0')
	{
		if (isdigit(c[i]))
		{
			HiddenNumber = c[i];
		}
		i++;
	}
	return HiddenNumber;
}

This post has been edited by ibe333333: 18 November 2012 - 09:49 AM

Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 4234
  • View blog
  • Posts: 13,300
  • Joined: 25-December 09

Re: Finding Digit in char array

Posted 18 November 2012 - 09:55 AM

Quote

using tt1a as the user input, it returns 49.

That looks correct. Remember the ASCII value of '1' is ???

Quote

am i using isdigit correctly?

Yes.

The problem is that you are returning your character '1' thru an int. When you do this you return the numeric value of your character not it's character representation.

What happens if there is no number in your string?

Jim
Was This Post Helpful? 0
  • +
  • -

#10 ibe333333  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 11-August 09

Re: Finding Digit in char array

Posted 18 November 2012 - 10:15 AM

thanks a bunch everyone! i figured it out.

#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>

using namespace std;

int getHiddenNumber(char c[]);

int main()
{
    const int SIZE = 5;
	int HiddenNum;
	char UserEntry[SIZE];
	cout << "Enter a string (up to 4 characters): ";
	cin.getline(UserEntry, SIZE);
	HiddenNum = getHiddenNumber(UserEntry) - '0';
	cout << HiddenNum << " is the Hidden Number!\n";
    cout << endl;
    system("PAUSE");
    return 0;
}

int getHiddenNumber(char c[])
{
	int HiddenNumber;
	int i = 0;
	while (c[i] != '\0')
	{
		if (isdigit(c[i]))
		{
			HiddenNumber = c[i];
		}
		i++;
	}
	return HiddenNumber;
}


Was This Post Helpful? 1
  • +
  • -

#11 jimblumberg  Icon User is offline

  • member icon


Reputation: 4234
  • View blog
  • Posts: 13,300
  • Joined: 25-December 09

Re: Finding Digit in char array

Posted 18 November 2012 - 10:32 AM

What happens when there is no number in your string????


I would do this calculation in the function :
HiddenNum = getHiddenNumber(UserEntry) - '0';


Jim
Was This Post Helpful? 0
  • +
  • -

#12 ibe333333  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 11-August 09

Re: Finding Digit in char array

Posted 18 November 2012 - 11:38 AM

View Postjimblumberg, on 18 November 2012 - 10:32 AM, said:

What happens when there is no number in your string????

Jim


I am currently working on that question. The second part of the assignment is to use this prototype:

int getHiddenNumber(char c[], int lengthChars, bool &multipleDigits);


There's two questions that come up:
1) What if there are no numbers? I figure I can just step through the character array and print out "no digits" or something like that.
2) What if there are more than one number? That one is giving me issues, and I am having trouble starting it out.
Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is offline

  • member icon


Reputation: 4234
  • View blog
  • Posts: 13,300
  • Joined: 25-December 09

Re: Finding Digit in char array

Posted 18 November 2012 - 11:54 AM

Quote

1) What if there are no numbers? I figure I can just step through the character array and print out "no digits" or something like that.

Where do you intend to print this message? In your function, or in the calling function?

The question I asked is based on the code you provided:
int getHiddenNumber(char c[])
{
	int HiddenNumber;
	int i = 0;
	while (c[i] != '\0')
	{
		if (isdigit(c[i]))
		{
			HiddenNumber = c[i];
		}
		i++;
	}
	return HiddenNumber;
}


And I guess I should have worded the question a little differently. So here is a new question. If there are no numbers in your string what value will you return to the calling function, based on the above code? Try sending this function a string with no numbers, what happens?

Quote

The second part of the assignment is to use this prototype:

Is this a totally different function because you said in an earlier post you had to use this prototype:
int getHiddenNumber(char c[]); 

By adding the extra parameters you can do things differently. What is the purpose of the added parameters?

Jim

This post has been edited by jimblumberg: 18 November 2012 - 11:56 AM

Was This Post Helpful? 0
  • +
  • -

#14 ibe333333  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 11-August 09

Re: Finding Digit in char array

Posted 18 November 2012 - 12:13 PM

Thanks Jim. My phrasing of the question is confusing as well, so I do apologize.

Below is the code identifying if there are no digits with the input string:

#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>

using namespace std;

int getHiddenNumber(char c[]);
//int getHiddenNumber(char c[], int lengthChars, bool &multipleDigits);

int main()
{
    const int SIZE(1000);
	int HiddenNum;
	char UserEntry[SIZE];
	bool TwoDigits = false;
	cout << "Enter a string: ";
	cin.getline(UserEntry, SIZE, '\n');
	HiddenNum = getHiddenNumber(UserEntry); //- '0';
	if (HiddenNum >= 0)
		cout << HiddenNum << " is the Hidden Number!\n";
	else
		cout << "There is no number in the string!\n";
    cout << endl;
    system("PAUSE");
    return 0;
}
int getHiddenNumber(char c[])
{
	int HiddenNumber = -1;
	int i = 0;
	while (c[i] != '\0')
	{
		if (isdigit(c[i]))
		{
			HiddenNumber = c[i]-'0';
		}
		i++;
	}
	return HiddenNumber;
}



The second part of the assignment is to modify the previous prototype, and use this function protoype:

int getHiddenNumber(char c[], int lengthChars, bool &multipleDigits);


Using this protoype, the code should identify if there is more than 1 digit on the input string, and still output those digits as well. For example, if the input string is "TT1ABC33", it should output: 1 3 3

I was thinking of creating another array in the main, and have the getHiddenNumber function copy the digits to it, but then I believe you can't skip array elements when populating the array. And I am a little confused on the bool part of it.

I just wish that I can use what I wrote below but the point is to use functions:

#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>

using namespace std;

int main()
{
string UserEntry;
	cout << "Please enter a string: ";
	getline (cin, UserEntry);
	cout << endl << "User Entered: " << UserEntry << endl;

	for (int i = 0; i < UserEntry.length(); i++)
	{
		if (isdigit(UserEntry[i]))
		{
			cout << "Digit found in Element " << i << ": " << UserEntry[i] << endl;
        }
	}



I will keep working on this, but if there are any hints/advice on how to at least start looking at a problem like this, please let me know.

This post has been edited by ibe333333: 18 November 2012 - 12:16 PM

Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg  Icon User is offline

  • member icon


Reputation: 4234
  • View blog
  • Posts: 13,300
  • Joined: 25-December 09

Re: Finding Digit in char array

Posted 18 November 2012 - 12:29 PM

Quote

Below is the code identifying if there are no digits with the input string:

That looks good, you found the problem of the uninitialized variable, good.

Quote

The second part of the assignment is to modify the previous prototype, and use this function protoype:

Did your instructor provide a description of what the added parameters will contain? And also for this function what value does your instructor intend you to return. He must have at least given an explanation of how this function is to operate. It looks like you will need to print the numbers in the function, return an int and indicate whether or not there were multiple characters by changing the value of your bool.


Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2