| | |
quit on non integer?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jan 2008
Posts: 6
Reputation:
Solved Threads: 0
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?
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdbool.h> #include <math.h> int printAr(int n[], const int SIZE){ unsigned int i; printf("Array contents: "); for( i=0 ; i<SIZE ; i++ ){ printf("%d ", n[i]); } printf("\n"); } int shifty(int userAns, int n[], const int SIZE){ int i, j; //counter int temp_pivotNum; for( j=0 ; j<(sqrt(userAns*userAns)) ; j++ ){ if( userAns >= 0 ){ temp_pivotNum = n[SIZE-1]; for( i=(SIZE-1) ; i>0 ; i-- ){ n[i] = n[i-1]; } n[0] = temp_pivotNum; } else{ temp_pivotNum = n[0]; for( i=0 ; i<(SIZE-1) ; i++ ){ n[i] = n[i+1]; } n[SIZE-1] = temp_pivotNum; } } } int main(){ const int SIZE = 10; int n[SIZE], i, userNum; int userAns; for( i=0 ; i<SIZE ; i++ ){ n[i] = i+1; } printAr(n, SIZE); while( 1 ){ //while(1) is temporary printf("Shift how many positions? "); scanf("%d", &userAns); shifty(userAns, n, SIZE); printAr(n, SIZE); } }
"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
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Jan 2008
Posts: 6
Reputation:
Solved Threads: 0
Thanks guys. Here's the solution btw:
C Syntax (Toggle Plain Text)
int intCheck(){ int userNum, count = 0; int SIZE = 5; char userNum_asChar[SIZE]; int negFlag; fgets ( userNum_asChar, SIZE, stdin ); printf( "String %s", userNum_asChar ); negFlag = (userNum_asChar[0] == '-')? 1 : 0 ; printf("Negative flag %d\n", negFlag); if( userNum_asChar[negFlag] >= '0' && userNum_asChar[negFlag] <= '9' ){ userNum = userNum_asChar[negFlag]-'0'; userNum -= (negFlag)? userNum*2 : 0 ; printf("userNum %d\n", userNum); return userNum; } else{ printf("Non number %c\n", userNum_asChar[1]); } }
![]() |
Similar Threads
- Winsock Multi-Client Servers (C++)
- factorial using a for loop (C++)
- integer without a cast (C)
- Segmentation Fault (C)
- cprog integer calculator (C)
- Switch Case Statement (Java)
- Mortgage Program help (Java)
- Round Robin algorithm? (Java)
Other Threads in the C Forum
- Previous Thread: Access to com dll from c
- Next Thread: very QUICK question ON casting . plz help :)
Views: 570 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes radix read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi






