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?

``````#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);
}
}``````
3
Contributors
3
Replies
4
Views
10 Years
Discussion Span
Last Post by Matt You

By not using `scanf()` to input your data. See this series to see why, and how to fix it.

Thanks guys. Here's the solution btw:

``````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]);
}
}``````