943,915 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 634
  • C RSS
Feb 26th, 2008
0

quit on non integer?

Expand Post »
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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Matt You is offline Offline
6 posts
since Jan 2008
Feb 26th, 2008
0

Re: quit on non integer?

Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 26th, 2008
0

Re: quit on non integer?

By not using scanf() to input your data. See this series to see why, and how to fix it.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is online now Online
7,738 posts
since May 2006
Feb 26th, 2008
0

Re: quit on non integer?

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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Matt You is offline Offline
6 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Access to com dll from c
Next Thread in C Forum Timeline: very QUICK question ON casting . plz help :)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC