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;
}

New Topic/Question
Reply




MultiQuote






|