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...
i know the loop needs to be right around in here, but i'm not certain how to do it...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); } } }
Code:if(initialint!=4) //checks to see if number is 4 digits in length { cout<<"Must be a 4 digit number."<<endl; system("pause"); }



LinkBack URL
About LinkBacks


