Beginner C++ Question: I need to find the location of certain digits from user input
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Beginner C++ Question: I need to find the location of certain digits from user input

  1. #1
    Join Date
    Sep 2013
    Posts
    1

    Question 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!

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,423

    Re: Beginner C++ Question: I need to find the location of certain digits from user in

    Quote Originally Posted by hbarnes View Post
    ...
    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!
    Last edited by VictorN; September 24th, 2013 at 11:46 AM.
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 1999
    Posts
    27,431

    Re: Beginner C++ Question: I need to find the location of certain digits from user in

    Quote Originally Posted by hbarnes View Post
    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.
    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

  4. #4
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,013

    Re: Beginner C++ Question: I need to find the location of certain digits from user in

    Quote Originally Posted by hbarnes View Post
    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 View Post
    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 View Post
    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 View Post
    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?
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,464

    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??
    Last edited by 2kaud; September 24th, 2013 at 11:51 AM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center