Current location: Hot Scripts Forums » Programming Languages » C/C++ » Looping


Looping

Reply
  #1 (permalink)  
Old 01-21-11, 04:24 AM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 377
Thanks: 0
Thanked 1 Time in 1 Post
Looping

Hi, I have a small issue here.

c Code:
  1. int main()
  2. {
  3.     // Declaring variable
  4.     int n, r, y;
  5.     bool found;
  6.    
  7.     do
  8.     {
  9.         found = false;
  10.        
  11.         cout << "Enter an integer: ";
  12.         cin >> n;
  13.  
  14.         while (n > 0)
  15.         {   
  16.             r = n % 10;
  17.  
  18.             if (r == 0)
  19.             {
  20.                 found = true;
  21.                 break;
  22.             }
  23.             else if (r == 1)
  24.             {
  25.                 found = true;
  26.                 break;
  27.             }
  28.             else
  29.             {
  30.                 do
  31.                 {
  32.                     for (int k = r; k > 0; k--)
  33.                     {
  34.                         cout << " ";
  35.                     }
  36.        
  37.                     for (int y = r; y > 0; y--)
  38.                     {
  39.                         cout << y;
  40.                     }
  41.                            
  42.                     cout << endl;
  43.                            
  44.                     r--;
  45.                        
  46.                 } while (r > 0);
  47.             }
  48.  
  49.             n /= 10;
  50.  
  51.         }
  52.  
  53.        
  54.     } while (found == true);
  55.    
  56.     return 0;
  57. }

Basically, a user will input a integer and it will check if it's the integer contains 1 or 0, if it is, then reject the integer and ask the user to input again.

Once it is checked that it does not contain 1 or 0, it will print out the value using the for-loop.

My issue here is only when the the last digit is 1 then it will ask for the new integer straight else if it is something like 5215, it will loop through for the for-loop before asking for the new integer.

Some logic error here but I can't make it out.
Reply With Quote
  #2 (permalink)  
Old 01-21-11, 05:24 AM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 377
Thanks: 0
Thanked 1 Time in 1 Post
This is what I've come up with. Seem to be working fine, but not sure if I'm doing any extra step.

c Code:
  1. int main()
  2. {
  3.     // Declaring variable
  4.     int n, r, t, y, count;
  5.     bool found;
  6.    
  7.    
  8.    
  9.     do
  10.     {
  11.         found = false;
  12.         count = 0;
  13.        
  14.         cout << "Enter an integer: ";
  15.         cin >> n;
  16.         t = n;
  17.        
  18.         while (n > 0)
  19.         {   
  20.             r = n % 10;
  21.  
  22.             if (r == 0)
  23.             {
  24.                 found = true;
  25.                 // break;
  26.                 count++;
  27.             }
  28.             else if (r == 1)
  29.             {
  30.                 found = true;
  31.                 // break;
  32.                 count++;
  33.             }
  34.            
  35.             n /= 10;
  36.         }
  37.  
  38.         if (count == 0)
  39.         {
  40.             while (t > 0)
  41.             {
  42.                 r = t % 10;
  43.                
  44.                 do
  45.                 {
  46.                     for (int k = r; k > 0; k--)
  47.                     {
  48.                         cout << " ";
  49.                     }
  50.                    
  51.                     for (int y = r; y > 0; y--)
  52.                     {
  53.                         cout << y;
  54.                     }
  55.                                
  56.                     cout << endl;
  57.                                
  58.                     r--;
  59.                            
  60.                 } while (r > 0);
  61.                
  62.                 t /= 10;
  63.             }     
  64.         }
  65.  
  66.        
  67.     } while (found == true);
  68.    
  69.     return 0;
  70. }
Reply With Quote
  #3 (permalink)  
Old 01-21-11, 01:44 PM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
As far as I can tell, I'd say that's the way to do it.

An other option is to convert the number to a string, and do a character based search for 1 and 0. I have no idea though, if this has any advantages over the method you use
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #4 (permalink)  
Old 01-22-11, 10:19 AM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 377
Thanks: 0
Thanked 1 Time in 1 Post
Thanks for confirming that.
Reply With Quote
  #5 (permalink)  
Old 09-19-11, 07:24 PM
sagar474 sagar474 is offline
New Member
 
Join Date: Sep 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
making to many division operations is a hell to the processes.
Hear is your code you can easily convert it to c++

#include<stdlib.h>
#include<string.h>
#include<stdio.h>
int main(){
char *integer=(char *)malloc(10);
int len=0, i=0;
printf("Enter an integer");
while(1)
{
scanf("%s",integer);
len=strlen(integer);
while(i<len && integer[i]!='0' && integer[i]!='1')++i;//check for zeros and ones
if((integer[i]=='0' || integer[i]=='1'))printf("enter again \n"),i=0;//if there is zero or one prints enter again
else break;
}
return 0;
}

Last edited by sagar474; 09-19-11 at 07:27 PM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Looping problem mcrob PHP 2 04-13-05 10:21 AM
Looping to append a variable to $_POST[variable] names cstallins PHP 1 02-13-05 05:42 AM
Error looping array Eclipse PHP 6 11-18-04 04:24 PM
Looping with limit... URGENT Seldimi PHP 8 12-26-03 02:24 PM


All times are GMT -5. The time now is 01:20 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.