Beginner C++ Question: I need to find the location of certain digits from user input
I have been trying to finish this code (function) for a while now, but am stuck on the last part. In this code, I prompt the user to select a number of integers and any number of digits and then find the smallest and largest value within these digits. On the next part, I am supposed to determine which of the given digits the smallest and largest are located such that the output should be:
Digit _ can be found in integer number(s): _, _
I apologize in advance if my code is sloppy; I just started learning C++ and haven't fully grasped the language yet.
Here is what I have tried:
Code:
int digitSizeLoca() {
int userNumInteger;
int* iPtr;
int* iPtr2;
int* iPtr3;
int value;
int value2;
int value3;
std::cout << "\nHow many integers? ";
std::cin >> userNumInteger;
iPtr = new int[userNumInteger];
iPtr2 = new int[userNumInteger];
iPtr3 = new int[userNumInteger];
for (int i = 0; i < userNumInteger; i++) {
*(iPtr3 + 1) = *(iPtr2 + 1) = *(iPtr + 1);
std::cout << "\nEnter digit #" << i + 1 << ": ";
std::cin >> *(iPtr + 1);
}
value = *(iPtr + 1);
value2 = *(iPtr2 + 1);
value3 = *(iPtr3 + 1);
if (value != 0, value2 != 0, value3 != 0) {
if (value <= 0)
value = -value;
if (value2 <= 0)
value2 = -value2;
if (value3 <= 0)
value3 = -value3;
int lDigit;
int sDigit;
int curDigit;
int pot = 10;
lDigit = sDigit = value % pot;
while (value, value2, value3) {
if (value / pot == 0, value2 / pot == 0, value3 / pot == 0) break;
curDigit = (value / pot, value2 / pot, value3 / pot) % 10;
if (curDigit < sDigit)
sDigit = curDigit;
if (curDigit > lDigit)
lDigit = curDigit;
pot*=10;
}
std::cout << "\nThe smallest digit: " << sDigit << std::endl
<< "\n Digit " << sDigit
<< " can be found in integer number(s): ";
for (int i = 0; i < userNumInteger; i++) {
int temp;
if (value < 0, value2 < 0, value3 < 0)
temp = -value, temp = -value2, temp = -value3;
else
temp = value, temp = value2, temp = value3;
do {
if (temp % 10 == sDigit) {
std::cout << " " << i+1;
temp = 0;
if (i != 0 && i < userNumInteger - 1 || i != 1 && i < userNumInteger - 1)
std::cout << ",";
}
temp /= 10;
} while(temp);
}
std::cout << "\nThe largest digit: " << lDigit << std::endl
<< "\n Digit " << lDigit
<< " can be found in integer number(s): ";
}
return 0;
}
Where:
Code:
for (int i = 0; i < userNumInteger; i++) {
int temp;
if (value < 0, value2 < 0, value3 < 0)
temp = -value, temp = -value2, temp = -value3;
else
temp = value, temp = value2, temp = value3;
do {
if (temp % 10 == sDigit) {
std::cout << " " << i+1;
temp = 0;
if (i != 0 && i < userNumInteger - 1 || i != 1 && i < userNumInteger - 1)
std::cout << ",";
}
temp /= 10;
} while(temp);
}
Seems to do the job, but it always outputs 1, 2...
Any form of advice is greatly appreciated!
Re: Beginner C++ Question: I need to find the location of certain digits from user in
Quote:
Originally Posted by
hbarnes
...
I apologize in advance if my code is sloppy; I just started learning C++ and haven't fully grasped the language yet.
Well, now you have to start learning the debugging!
Without debugging you will never be able to develop programs. In any language!
Re: Beginner C++ Question: I need to find the location of certain digits from user in
Quote:
Originally Posted by
hbarnes
I apologize in advance if my code is sloppy; I just started learning C++ and haven't fully grasped the language yet.
First, your code has a memory leak:
Code:
iPtr = new int[userNumInteger];
iPtr2 = new int[userNumInteger];
iPtr3 = new int[userNumInteger];
There is no call to delete[] any of these dynamically allocated areas of memory.
Quote:
Here is what I have tried:
Another piece of advice. When you write code, you're supposed to know what every line, function, loop, etc. is supposed to do that you've written. There should be absolutely no guessing and hoping. This requires proper planning and design before you write a single line of code. If you are not familiar with some aspect of C++, then you write a very tiny main() program to get familiar with that aspect of C++ before writing a larger program.
Now if there is an error, the next step is to debug your code. Somewhere, the code deviates from your plan at some line or function. There is a function call that didn't return the correct results, or a variable should be a certain value but is an unexpected value, etc. After debugging, you will understand what needs to be changed to fix the bug, or in the worse case scenario, convince you that the plan is flawed and a new plan has to be devised.
Regards,
Paul McKenzie
Re: Beginner C++ Question: I need to find the location of certain digits from user in
Quote:
Originally Posted by
hbarnes
I apologize in advance if my code is sloppy; I just started learning C++ and haven't fully grasped the language yet.
Beginner or not, there is never any excuse for not properly indenting your code. Your code should be written to be read by people; the compiler doesn't mind.
It's bad C++ style to define variables before you can properly initialize them. You should try to define variables as late as possible. That way, there is less state to worry about at any point in a function, which makes the code easier to understand.
Quote:
Originally Posted by
hbarnes
Code:
iPtr = new int[userNumInteger];
iPtr2 = new int[userNumInteger];
iPtr3 = new int[userNumInteger];
You should learn to use std::vector. It's much easier and safer than dynamic arrays.
Quote:
Originally Posted by
hbarnes
Code:
*(iPtr3 + 1) = *(iPtr2 + 1) = *(iPtr + 1);
It's normal to write iPtr3[1], rather than *(iPtr3 + 1). This syntax also works when you use std::vector.
Don't know why you use a for-loop to write to the same memory over and over again, though.
Quote:
Originally Posted by
hbarnes
Code:
if (value != 0, value2 != 0, value3 != 0) {
This doesn't do what you think it does. If you want to test if multiple statements are all true in an if statement, you write
Code:
if (value != 0 && value2 != 0 && value3 != 0)
Where did you learn that syntax?
Re: Beginner C++ Question: I need to find the location of certain digits from user in
You seem to have several miscomprehensions about coding in c++.
Code:
for (int i = 0; i < userNumInteger; i++) {
*(iPtr3 + 1) = *(iPtr2 + 1) = *(iPtr + 1);
std::cout << "\nEnter digit #" << i + 1 << ": ";
std::cin >> *(iPtr + 1);
}
I don't think this line does what you are expecting. This is within a for loop yet you adding the same 1 to the base pointers every time round the loop so this statement does exactly the same for every iteration. For the first time round the loop it obtains the value at position 1 in the array pointed to by iptr - which hasn't yet been set - then assigns this unknown value to position 1 in the array pointed to by iptr2 and then assigns this unknown value also to position 1 in the array pointed to by iptr3! Now as Paul stated in post #3, what was your expectation from this line of code?
You then obtain an integer into position 1 of the iptr array - overwriting what ever value was there previously so when this loop terminates, the only value in the iptr array is in position 1 and is the last number entered.
D_Drmmr has mentioned re the if statement in post #4.
Code:
if (value <= 0)
value = -value;
if (value2 <= 0)
value2 = -value2;
if (value3 <= 0)
value3 = -value3;
You are trying to make sure that only positive integers are used. There is a standard function, abs() which returns the absolute positive value of a number. See http://msdn.microsoft.com/en-us/libr...=vs.84%29.aspx
but again, value, value2 and value3 are set from position 1 in the arrays. So the user is asked to input the number of integers required but only the number in position 1 in the arrays is used?
Code:
if (value < 0, value2 < 0, value3 < 0)
temp = -value, temp = -value2, temp = -value3;
else
temp = value, temp = value2, temp = value3;
Whatever you think these statements achieve, they dont! Where did you learn to use the , operator? Although this is valid c++ syntax, it won't do what you expect! Where two expressions are separated by a comma, each expression is evaluated left to right. The left operand is always evaluated, and all side effects are completed before the right operand is evaluated. See http://msdn.microsoft.com/en-us/library/zs06xbxh.aspx
As Paul says in post #3, you need to plan and design the program before you start to code. I would suggest that you first try to do what you require on paper so that you understand first the algorithm required to find the smallest and largest digits etc. When you understand the algorithm, design your program, then code and then test and debug.
How are you learining c++? From a book, via a course??