quit on non integer?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jan 2008
Posts: 6
Reputation: Matt You is an unknown quantity at this point 
Solved Threads: 0
Matt You Matt You is offline Offline
Newbie Poster

quit on non integer?

 
0
  #1
Feb 26th, 2008
I have a simple program (code follows) that circle_shifts an array, but I need it to be able to quit when the user enters a character, (ex. q). Where do I start?
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <math.h>
  4.  
  5. int printAr(int n[], const int SIZE){
  6. unsigned int i;
  7. printf("Array contents: ");
  8. for( i=0 ; i<SIZE ; i++ ){
  9. printf("%d ", n[i]);
  10. }
  11. printf("\n");
  12. }
  13.  
  14. int shifty(int userAns, int n[], const int SIZE){
  15. int i, j; //counter
  16. int temp_pivotNum;
  17.  
  18. for( j=0 ; j<(sqrt(userAns*userAns)) ; j++ ){
  19. if( userAns >= 0 ){
  20. temp_pivotNum = n[SIZE-1];
  21. for( i=(SIZE-1) ; i>0 ; i-- ){
  22. n[i] = n[i-1];
  23. }
  24. n[0] = temp_pivotNum;
  25. }
  26. else{
  27. temp_pivotNum = n[0];
  28. for( i=0 ; i<(SIZE-1) ; i++ ){
  29. n[i] = n[i+1];
  30. }
  31. n[SIZE-1] = temp_pivotNum;
  32. }
  33. }
  34. }
  35.  
  36. int main(){
  37. const int SIZE = 10;
  38. int n[SIZE], i, userNum;
  39. int userAns;
  40.  
  41. for( i=0 ; i<SIZE ; i++ ){
  42. n[i] = i+1;
  43. }
  44. printAr(n, SIZE);
  45.  
  46. while( 1 ){ //while(1) is temporary
  47. printf("Shift how many positions? ");
  48. scanf("%d", &userAns);
  49. shifty(userAns, n, SIZE);
  50. printAr(n, SIZE);
  51. }
  52. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: quit on non integer?

 
0
  #2
Feb 26th, 2008
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,127
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: quit on non integer?

 
0
  #3
Feb 26th, 2008
By not using scanf() to input your data. See this series to see why, and how to fix it.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 6
Reputation: Matt You is an unknown quantity at this point 
Solved Threads: 0
Matt You Matt You is offline Offline
Newbie Poster

Re: quit on non integer?

 
0
  #4
Feb 26th, 2008
Thanks guys. Here's the solution btw:
  1. int intCheck(){
  2. int userNum, count = 0;
  3. int SIZE = 5;
  4. char userNum_asChar[SIZE];
  5. int negFlag;
  6.  
  7. fgets ( userNum_asChar, SIZE, stdin );
  8.  
  9. printf( "String %s", userNum_asChar );
  10. negFlag = (userNum_asChar[0] == '-')? 1 : 0 ;
  11. printf("Negative flag %d\n", negFlag);
  12.  
  13. if( userNum_asChar[negFlag] >= '0' && userNum_asChar[negFlag] <= '9' ){
  14. userNum = userNum_asChar[negFlag]-'0';
  15. userNum -= (negFlag)? userNum*2 : 0 ;
  16. printf("userNum %d\n", userNum);
  17. return userNum;
  18. }
  19. else{
  20. printf("Non number %c\n", userNum_asChar[1]);
  21. }
  22. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 570 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC