Multiple user input and task execution
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: Multiple user input and task execution

  1. #1
    Join Date
    May 2017
    Posts
    182

    Multiple user input and task execution

    Hello there . SO I have a question about my program .

  2. #2
    Join Date
    May 2017
    Posts
    182

    Re: Multiple user input and task execution

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
        
       int n , i = 1 , sum = 0 , perfCount = 0;
    
       cout << "Please enter interger numbers ( or -1 to exit ) :\n" << endl;
       cin >> n;
    
       cout << "\nNo perfect numbers were found." << endl; 
       
       while ( n != 1 && i < n) {
    
       if ( n % i == 0) {
    
        sum += i;
           ++i;
         }
     
       if ( sum == n ) 
    
    	perfCount++;
        cin >> n;
    
      }
     
       cout << "There was " << perfCount << " perfect number.\n" << endl;
    
       system ("pause");
       return 0;
    
    }

    so this is my code . User can input as many number as he want till he enters -1 and loop should stop and print the number of perfects .I have a prob in it because it's not counting properly. thx for ur help ..

  3. #3
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    701

    Re: Multiple user input and task execution

    Code:
    #include <iostream>
    using namespace std;
    
    int main () {
        int n , i, sum, perfCount = 0;
        do {
           cout << "\nPlease enter interger numbers ( or -1 to exit ) :\n" << endl;
           cin >> n;
           i = 1;
           sum = 0;
           while( n != 1 && i < n) {
                if(!(n % i)) sum += i;
                ++i;
           }
           if(sum == n)
               ++perfCount, cout << "The perfecr number was entered";
           else
           cout << "The number wasn't perfect";
        }
        while(n!=-1);
        cout << endl << "There was " << perfCount << " perfect numbers entered.\n" << endl;
        system ("pause");
    }
    This code works fine for me. BTW, there's a really limited count of perfect numbers, so the user may got lucky during entering a random number without knowing its perfectness.
    Popular opinion is the greatest lie in the world.

  4. #4
    Join Date
    May 2017
    Posts
    182

    Re: Multiple user input and task execution

    this is not what I want . I want the user to keep input values and if he choose -1 it will then show from the list how many were perfect you see? you changed the hole prog its doing it for each value and display if perfect or not I want to write values and then at -1 it will show the count . Thx again

  5. #5
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    701

    Re: Multiple user input and task execution

    I think you can easily write yourself as far as you know what you're talking about.
    Popular opinion is the greatest lie in the world.

  6. #6
    Join Date
    May 2017
    Posts
    182

    Re: Multiple user input and task execution

    Yes that what I did but didnt work this is my code again

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
        
       int n , i = 1 , sum = 0 , perfCount = 0;
    
       cout << "Please enter interger numbers ( or -1 to exit ) :\n" << endl;
       cin >> n;
    
      
       
       while ( n != 1 && i < n) {
    
       if ( n % i == 0) {
    
        sum += i;
           ++i;
         }
     
       if ( sum == n ) 
    
    	perfCount++;
        cin >> n;
    
      }
     
       cout << "There was " << perfCount << " perfect number.\n" << endl;
      
       system ("pause");
       return 0;
    }
    and if I try some values its not working see 6 and 28 are perfect values

    Code:
    Please enter interger numbers ( or -1 to exit ) :
    
    6
    28
    496
    7
    -1
    There was 0 perfect number.
    u see its says no perfect on the list

  7. #7
    Join Date
    May 2017
    Posts
    182

    Re: Multiple user input and task execution

    Code:
    while ( n != 1 && i < n) {
    this part is wrong but I trying to figure out how to corret it . usually when you compute perfect it should find all divisor and conpute sum excluding the number it self if sum is equal number then that number is perfect . now I need it to stop on -1 but I need to include somewhere the condition i < n in order to exclude the number it self

  8. #8
    Join Date
    May 2017
    Posts
    182

    Re: Multiple user input and task execution

    can I just remove the condition let it do the sum of all divisors then remove n ? you see my logic or not ?
    something like that

    Code:
    sum = ( sum + i ) - n;
    if we take 6 it will get all divisor compute sum which is 12 - 6 = 6 where back to n if doesnt go back to n that means not perfect if yes then perfect can we do it ??

  9. #9
    Join Date
    May 2017
    Posts
    182

    Re: Multiple user input and task execution

    but the prob still that its not counting the perfect it always says o perfect found

  10. #10
    Join Date
    May 2017
    Posts
    182

    Re: Multiple user input and task execution

    updated code not that different

    Code:
      #include <iostream>
    using namespace std;
    
    int main ()
    {
        
       int n , i = 1 , sum = 0 , perfCount = 0;
    
       cout << "Please enter interger numbers ( or -1 to exit ) :\n" << endl;
       cin >> n;
    
      
       
       while ( n != -1) {
    	  
    	 while ( i < n/2 )  {
    	   
    		 if ( n % i == 0) 
    
    			 sum += i; 
    		
    	        ++i;
    	    
      }  
    
          if ( sum == n ) 
    
    	  perfCount++;    
           
        cin >> n;
    
     }
         cout << "\nThere was " << perfCount << " perfect number found.\n" << endl;
       
       system ("pause");
       return 0;
    
    }
    and here is some output

    Code:
      Please enter interger numbers ( or -1 to exit ) :
    
    4
    6
    14
    28
    -1
    
    There was 0 perfect number found.
    
    Press any key to continue . . .
    prog should display the number of perfect after user choose -1 . now perfect means sum of divisor excluded the actual number is equal to the exact number . divisor of any number n never exceed n/2 so I used from i to n/2 and but then I don't know whats wrong
    Last edited by david16; June 13th, 2017 at 02:07 PM.

  11. #11
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,882

    Re: Multiple user input and task execution

    Try

    Code:
    while ( i <= n/2 )  {
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.5.1)

  12. #12
    Join Date
    May 2017
    Posts
    182

    Re: Multiple user input and task execution

    yes too close

    this is output it only counted 6 but you see 28 is also a perfect . it seems that its getting only first one the list

    Code:
    Please enter interger numbers ( or -1 to exit ) :
    
    4
    6
    28
    16
    -1
    
    There was 1 perfect number found.
    
    Press any key to continue . . .
    Last edited by david16; June 13th, 2017 at 02:43 PM.

  13. #13
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    701

    Re: Multiple user input and task execution

    The way I have organized nested loops, the program is working correctly. Seems like you are confused by those loops.
    Popular opinion is the greatest lie in the world.

  14. #14
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,882

    Re: Multiple user input and task execution

    Variables used within the inner while loop are not being reset for each iteration of the outer loop!

    It would also be preferable IMO to separate the code for determining whether a number is prime or not from the rest. Consider
    Code:
    #include <iostream>
    using namespace std;
    
    bool isPerfect(unsigned int num)
    {
    	unsigned int sum = 1;
    	const unsigned int num2 = num / 2;
    	
    	if ((num < 6) || (num % 2))
    		return false;
    
    	for (unsigned int i = 2; i <= num2; ++i)
    		if ((num % i) == 0)
    			sum += i;
    
    	return sum == num;
    }
    
    int main()
    {
    	int num = -1;
    	unsigned int cnt = 0;
    
    	do {
    		while ((cout << "Enter positive number to check or -1 to terminate: ") && (!(cin >> num) || (cin && ((num < -1) || (num == 0))))) {
    			cout << "Invalid number entered" << endl;
    			cin.clear();
    			cin.ignore(1000, '\n');
    		}
    
    		if (num > 0)
    			cnt += isPerfect(num);
    
    	} while (num != -1);
    
    	cout << "\nThere " << ((cnt == 1) ? "was " : "were ") << cnt << " perfect number" << ((cnt != 1) ? "s " : " ") << "entered" << endl;
    }
    which also checks that the input is valid.

    Note that this doesn't actually display which of the entered numbers are the prefect ones. If this is needed, it can be easily added by storing the found perfect number in say a vector and then at the end enumerating the vector.
    Last edited by 2kaud; June 13th, 2017 at 05:19 PM. Reason: 1 is not a perfect number - and there are no known odd perfect numbers!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.5.1)

  15. #15
    Join Date
    May 2017
    Posts
    182

    Re: Multiple user input and task execution

    your design will check for each input . I wanted to enter multiple input till -1 is entered it will exit loop and display the result you see?
    my prog is working but not counting all perfects it only do count first 1 visible no the list can you tell me whats wrong with that loop ? thanks

Page 1 of 2 12 LastLast

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 a Codeguru.com survey!


On-Demand Webinars (sponsored)