Thread: can't figure out a loop...

  1. #1
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69

    can't figure out a loop...

    i finally crunched this program out... sorry for all the confusion, i'm new to C++ and am anxious to learn... the program below works fine but i need it to repeat a certain section if they enter a number that's not 4 digits long... i have it to say "Must be 4 digit number." but then it kicks out of the program, i know it's because of the "system(pause)" thing, but i have that in there only until i get the loop to work... i want it to keep asking them for a 4 digit num. until they enter a 4 dig. number.... thanks guys...
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    #include <process.h>
    #include <ctype.h>
    #include <iostream.h>
    
    char initial[5];     //declaration of variables and character string
    int initialint, i, x, y, num;
    
    void swap(int a, int b); // function prototypes for swapping/sorting.
    void sort_des();
    void sort_asc();
    
    void main()
    {
    
    	cout<<"Please enter a 4 digit non-zero number..."<<" ";  //gets initial value
    	cin>>initial;
    	initialint=strlen(initial);
    	if(initialint!=4)          //checks to see if number is 4 digits in length
    	{
    		cout<<"Must be a 4 digit number."<<endl;
            system("pause");
    	}
    
    	for(i=0; i<4; i++)
    	{
    		if(!isdigit(initial[i]))   //makes sure it is only digits and not letters or characters or something
    		{
    			cout<<"Must be numbers only."<<endl;
    			system("pause");
    		}
    	}
    
    	 while(strcmp(initial, "6174")!=0)  //checks to see if it is 6174 or not
    	{
    		cout<<""<<endl;
    		sort_des();                //prints out the new numbers of the desc. and asc. strings
    		x=atoi(initial);
    		cout<<"  "<<initial<<endl;
    		sort_asc();
    		y=atoi(initial);
    		cout<<"- "<<initial<<endl;
    
    		if(x==y)      // this is to exempt the '9999, 8888, etc.' or zero
    		{
    			cout<<"Does not apply..."<<endl;
    			break;
    		}
    
    		num=x-y;   // subtraction for descending and ascending
    		sprintf(initial, "%4d", num);
    		cout<<"------"<<endl;
    		cout<<"  "<<initial<<endl;
    
            cout<<""<<endl;    // repeated this section of code to double up the "6174" at the end like on the homework paper.
    		sort_des();
    		x=atoi(initial);
    		cout<<"  "<<initial<<endl;
    		sort_asc();
    		y=atoi(initial);
    		cout<<"- "<<initial<<endl;
    
    		if(x==y)
    		{
    			cout<<"Does not apply..."<<endl;
    			break;
    		}
    
    		num=x-y;
    		sprintf(initial, "%4d", num);
    		cout<<"------"<<endl;
    		cout<<"  "<<initial<<endl;
    	}
    
    	cout<<""<<endl;
    	system("pause");
    }
    
    
    
    void swap(int a, int b)   // function definition for swapping characters in the string
    {
    char t;
    
    	t=initial[a];
    	initial[a]=initial[b];
    	initial[b]=t;
    }
    
    void sort_des()         //function definition for sorting characters into descending order
    {
    int s, f;
    
    	for(s=0; s<initialint-1; s++)
    	{
    		for(f=s+1; f<initialint; f++)
    		{
    			if(initial[f]>initial[s]) swap(f, s);
    		}
    	}
    }
    
    void sort_asc()        //function definition for sorting characters into ascending order
    {
    int s, f;
    
    	for(s=0; s<initialint-1; s++)
    	{
    		for(f=s+1; f<initialint; f++)
    		{
    			if(initial[f]<initial[s]) swap(f, s);
    		}
    	}
    }
    i know the loop needs to be right around in here, but i'm not certain how to do it...
    Code:
    if(initialint!=4)          //checks to see if number is 4 digits in length
    	{
    		cout<<"Must be a 4 digit number."<<endl;
            system("pause");
    	}

  2. #2
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    while(initialint!=4) should work instead of if(initialint!=4)

  3. #3
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    Code:
    do             
    {              
         cout<<"Please enter a 4 digit non-zero number..."<<" ";            
         cin>>initial;
    }while(initial.size() != 4);
    I think that will work
    Keyboard Not Found! Press any key to continue. . .

  4. #4
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    deepfrye: didn't work...

  5. #5
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    what didnt work about it?
    Keyboard Not Found! Press any key to continue. . .

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    This will work:

    Code:
    do             
    {              
         cout<<"Please enter a 4 digit non-zero number..."<<" ";            
         cin>>initial;
    }while(strlen(initial) != 4 || atoi(initial) == 0);

  7. #7
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    thanks, i appreciate it!

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    5
    You don't see my reply, man?
    All ready complete, man.
    +Not limit number inputed.
    +Check number or char
    +Check size of number

    I've completed everything for you, man.

    Read carefully before post again, man.



    Code:
    //sort number
    //mybe algorithm is Bubble Sort
    //a bruce force algorithm
    //optimized for check input buffer
    #include <iostream>
    using namespace std;
    ////////////////////////////////////////////////////////////
    //convert from a character to a number
    ////////////////////////////////////////////////////////////
    int chartoint(char c)
    	{
    		char buffer[2];
    		buffer[1]=0;//string need to be terminated by ZERO
    		buffer[0]=c;
    		return atoi(buffer);
    	}
    ////////////////////////////////////////////////////////////
    //use to swap
    ////////////////////////////////////////////////////////////
    void c_swap(char &a, char &b)
    	{
    		char buffer;
    		buffer=a;
    		a=b;
    		b=buffer;
    	}
    ////////////////////////////////////////////////////////////
    //check "number" is number or not
    ////////////////////////////////////////////////////////////
    bool checknumberstring(char*s)
    	{
    		int l = strlen(s);
    		for (int i=0; i<l+1;i++)
    				if (s[i]<'0' && s[i]>'9')
    					return false;
    		return true;
    	}
    ////////////////////////////////////////////////////////////
    #define	NUMBER	10
    ////////////////////////////////////////////////////////////
    //main here//
    ////////////////////////////////////////////////////////////
    int main()
    	{
    	char initialdigit [NUMBER];
    	unsigned int firstinteger;
    ////////////////////////////////////////////////////////////
    	cout<<"Please enter a non-zero digit number..."<<endl;
    	fgets(initialdigit, NUMBER, stdin);
    ////////////////////////////////////////////////////////////
    	if (strlen(initialdigit)!=NUMBER-1 || checknumberstring(initialdigit)==false) 
    		{
    			cout << "Error! Input..." << endl;
    			system("PAUSE");
    			return 1;
    		}
    ////////////////////////////////////////////////////////////
    	firstinteger = atoi (initialdigit);
    ////////////////////////////////////////////////////////////
    	if (firstinteger > 0)
    		{
    		for (int i=0; i<NUMBER-1; i++)
    			for (int j=0; j<NUMBER-1; j++)
    				if (chartoint(initialdigit [j]) < chartoint(initialdigit [j+1]))
    					c_swap(initialdigit [j], initialdigit [j+1]);
    		}
    ////////////////////////////////////////////////////////////
    	cout<<initialdigit<<endl;
    	system("PAUSE");
    ////////////////////////////////////////////////////////////
    	return 0;
    	}
    ////////////////////////////////////////////////////////////
    Last edited by phoenixodin2; 09-20-2004 at 11:37 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 03:48 AM
  2. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  3. Trying to figure out a simple loop....
    By chadsxe in forum C++ Programming
    Replies: 9
    Last Post: 01-05-2006, 01:31 PM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM