I'm trying to figure out why I cannot delete elements from the dict array. Could somebody help me out? The function removeWord is working as it is supposed to when removing the last added word, but not when trying to remove some other word.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NUMBER_OF_WORDS 11
int clear(){
while(getchar()^'\n');
return 0;
}
int numberOfWordsInDict(char **dict){
int i = 0;
int c1 = 0;
for (i = 0; i < MAX_NUMBER_OF_WORDS; ++i){
if (dict[i] != 0){
c1++;
}
}
return c1;
}
void addWord(char **dict, char *word){
int c1 = numberOfWordsInDict(dict);
char *word1;
if (c1 >= 0 && c1 < 10){
word1 = (char*) malloc(sizeof(char)*(strlen(word)+1));
dict[c1] = word1;
strncpy(dict[c1], word, strlen(word));
dict[c1][strlen(word)] = '\0';
} else if (c1 >= 10){
printf("Dictionary is already full!\n");
}
}
void printDict(char **dict){
int i = 0;
int c1 = numberOfWordsInDict(dict);
printf("Dictionary:\n");
if (c1 == 0){
printf("The dictionary is empty.\n");
} else if (c1 > 0 && c1 <= 10){
while (dict[i] != NULL){
printf("- %s\n", dict[i]);
i++;
}
}
}
void removeWord(char **dict, char *word){
int i = 0;
for (i = 0; i < MAX_NUMBER_OF_WORDS; i++){
if (strncmp(dict[i], word, strlen(word)+1) == 0){
dict[i] = 0;// can only delete the last element of dict properly.
break;
}
}
}
int main(){
char *dict[MAX_NUMBER_OF_WORDS] = {};
char word[1024] = {};
char command;
while(1){
printf("Command (a/p/r/q): ");
while(scanf("%s", &command) == 1){
break;
}
;
clear();
if (command == 'a'){ // add word
scanf("%[^\n]s", &word);
clear();
addWord(dict, word);
} else if (command == 'p'){ // print dict
printDict(dict);
} else if (command == 'r'){ // remove word
printf("Remove a word: ");
scanf("%[^\n]s", &word);
clear();
removeWord(dict, word);
} else if (command == 'q'){ // quit
break;
}
}
int i = 0;
for (i = 0; i < MAX_NUMBER_OF_WORDS; i++){
free(dict[i]);
}
return 0;
}
example input:
a
dog
a
cat
a
apple
case 1:
r
apple
p
// output =
Dictionary:
- dog
- cat
a
uniform
p
// output =
Dictionary:
- dog
- cat
- uniform
// works fine
case 2
r
cat
p
// output =
Dictionary:
- dog
a
book
p
// output =
Dictionary:
- dog
// doesn't work as expected
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NUMBER_OF_WORDS 11
int clear(){
while(getchar()^'\n');
return 0;
}
int numberOfWordsInDict(char **dict){
int c1 = 0;
for(int i = 0; i < MAX_NUMBER_OF_WORDS; ++i){
if (dict[i] != 0){
c1++;
}
}
return c1;
}
int vacancy(char **dict){
for(int i = 0; i < MAX_NUMBER_OF_WORDS; ++i){
if (dict[i] == 0){
return i;
}
}
return -1;
}
void addWord(char **dict, char *word){
int c1 = vacancy(dict);//It is not possible to use the registration number as an additional index.
if (-1 != c1){
dict[c1] = malloc(strlen(word)+1);
strcpy(dict[c1], word);
} else {
printf("Dictionary is already full!\n");
}
}
void printDict(char **dict){
int c1 = numberOfWordsInDict(dict);
printf("Dictionary:\n");
if (c1 == 0){
printf("The dictionary is empty.\n");
} else {
for(int i = 0; i < MAX_NUMBER_OF_WORDS; ++i){
if(dict[i])
printf("- %s\n", dict[i]);
}
}
}
void removeWord(char **dict, char *word){
for(int i = 0; i < MAX_NUMBER_OF_WORDS; i++){
if (strcmp(dict[i], word) == 0){
free(dict[i]);//need free
dict[i] = 0;// can only delete the last element of dict properly.
break;
}
}
}
int main(){
char *dict[MAX_NUMBER_OF_WORDS] = { NULL };
char word[1024] = { 0 };//forbids empty initializer braces
char command;
while(1){
printf("Command (a/p/r/q): ");
scanf("%c", &command);//%s buffer over run
clear();
if (command == 'a'){ // add word
scanf("%[^\n]", word);
clear();
addWord(dict, word);
} else if (command == 'p'){ // print dict
printDict(dict);
} else if (command == 'r'){ // remove word
printf("Remove a word: ");
scanf("%[^\n]", word);
clear();
removeWord(dict, word);
} else if (command == 'q'){ // quit
break;
}
}
int i = 0;
for (i = 0; i < MAX_NUMBER_OF_WORDS; i++){
free(dict[i]);
}
return 0;
}
Inside printDict() use
while (i<MAX_NUMBER_OF_WORDS){
if(dict[i] != 0)
printf("- %s\n", dict[i]);
i++;
}
instead of
while (dict[i] != NULL){
printf("- %s\n", dict[i]);
i++;
}
because even if you know if there are c1 number of words, you don't know their location as in where those c1 words are present.
Also change
while(scanf("%s", &command) == 1)
to
while(scanf("%c", &command) == 1)
since command is of char type.
Related
I created the four in a row game in c. Now I want to dynamically allocate the memory of the board. It is a two-dimensional array and I want it stored there for use. I declare it as a global double-pointer and declare it in my initialization method in the code below. I am new to c and I'm not entirely sure how to go about handling the segmentation fault(core dumped) error that I get.
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 50
//size of board
#define rows 6
#define column 7
//player's name
char player1[100];
char player2[100];
//pointer to keep track of playes turn
char *playersTurn;
//counter to keep track of where we are on each column
int a = rows - 1;
int b = rows - 1;
int c = rows - 1;
int d = rows - 1;
int e = rows - 1;
int f = rows - 1;
int g = rows - 1;
//array of the board
//int board[rows][column];
int **board;
//boardlaying array elements
void boardlayArray(){
printf("Two Dimensional array elements:\n");
for(int i=0; i<rows; i++) {
for(int j=0;j<column;j++) {
printf("%d ", board[i][j]);
if(j==6){
printf("\n");
}
}
}
}
//recostruct board to empty places
void teardown(){
/*Counter variables for the loop*/
int i, j;
for(int i=0; i<rows; i++) {
for(int j=0;j<column;j++) {
board[i][j]=0;
}
}
boardlayArray();
}
//checks if the four in a row are the same
int checkFour(int a, int b, int c, int d){
return a == b && b == c && c == d;
}
//check for the horizontal win
void checkHorizontal(int player){
for(int i=0; i < rows; i++){
for(int j=0; j < column - 3; j++){
if ((board[i][j] != 0) && (board[i][j+1] != 0) && (board[i][j+2]!= 0) && (board[i][j+3] != 0)){
if (checkFour(board[i][j],board[i][j+1],board[i][j+2],board[i][j+3]) == 1){
printf("Game Over\n");
exit(0);
}
}
}
}
}
//check for vertical win
void checkVertical(int player){
for(int j=0; j < column; j++){
for(int i=rows - 1; i > rows - 3; i--){
if ((board[i][j] != 0) && (board[i-1][j] != 0) && (board[i-2][j]!= 0) && (board[i-3][j] != 0)){
if (checkFour(board[i][j],board[i-1][j],board[i-2][j],board[i-3][j]) == 1){
printf("Game Over\n");
exit(0);
}
}
}
}
}
//check for diagonal win
void checkDiagonal (int player){
// ascendingDiagonalCheck
for (int i=3; i<column; i++){
for (int j=0; j<rows-3; j++){
if ((board[i][j] != 0) && (board[i-1][j+1] != 0) && (board[i-2][j+2]!= 0) && (board[i-3][j+3] != 0)){
if (checkFour(board[i][j],board[i-1][j+1],board[i-2][j+2],board[i-3][j+3]) == 1){
printf("Game Over\n");
exit(0);
}
}
}
}
// descendingDiagonalCheck
for (int i=3; i<column; i++){
for (int j=3; j<rows; j++){
if ((board[i][j] != 0) && (board[i-1][j-1] != 0) && (board[i-2][j-2]!= 0) && (board[i-3][j-3] != 0)){
if (checkFour(board[i][j],board[i-1][j-1],board[i-2][j-2],board[i-3][j-3]) == 1){
printf("Game Over\n");
exit(0);
}
}
}
}
}
//places the players puck into the correct place and gives it a corresponding value
//Also checks and correspondingly updtaes the turn to same player if row is full
int updateWorld(char w, int playerNumber){
switch(w)
{
case 'A':
if(a == -1){
printf("This row is full\n");
return 0;
}
board[a][0] = playerNumber;
a--;
break;
case 'B' :
if(b == -1){
printf("This row is full\n");
return 0;
}
board[b][1] = playerNumber;
b--;
break;
case 'C' :
if(c == -1){
printf("This row is full\n");
return 0;
}
board[c][2] = playerNumber;
c--;
break;
case 'D' :
if(d == -1){
printf("This row is full\n");
return 0;
}
board[d][3] = playerNumber;
d--;
break;
case 'E' :
if(e == -1){
printf("This row is full\n");
return 0;
}
board[e][4] = playerNumber;
e--;
break;
case 'F' :
if(a == -1){
printf("This row is full\n");
return 0;
}
board[f][5] = playerNumber;
f--;
break;
case 'G' :
if(g == -1){
printf("This row is full\n");
return 0;
}
board[g][6] = playerNumber;
g--;
break;
case ' ' : printf("Nothing was entered\n");
break;
case 'Q' : printf("%s has quit game\n", playersTurn);
teardown();
exit(0);
break;
default: printf("Enter a wrong option\n");
}
boardlayArray();
checkHorizontal(playerNumber);
checkVertical(playerNumber);
checkDiagonal(playerNumber);
return 1;
}
//get player's names and dynamicaaly allocate memory for board
void initialization(){
printf("Enter the name of player 1:\n");
fgets(player1,MAX, stdin);
printf("Enter the name of player 2:\n");
fgets(player2,MAX, stdin);
board = (int **)malloc(rows * sizeof(int *));
for (int i=0; i<rows; i++)
board[i] = (int *)malloc(column * sizeof(int));
}
//get where player wants to play
char acceptInput(){
printf("Enter where you want to put the disc A-G. Enter Q if you want to quit.\n");
char w;
scanf("%[^\n]",&w);
return w;
}
int main(){
initialization();
char w;
//runs for the size of the board
for(int i = 1; i <= (rows * column); i++){
w = acceptInput();
w = toupper(w);
if(i%2 == 0){
playersTurn = (char*) player2;
if(updateWorld(w,2) == 0){
playersTurn = (char*) player1;
printf("%s won the game",playersTurn);
exit(0);
}
}
else{
playersTurn = (char*) player1;
if(updateWorld(w,1) == 0){
playersTurn = (char*) player2;
printf("%s won the game",playersTurn);
exit(0);
}
}
while(getchar() != '\n');
}
return 0;
}
How About using the heap?? That's the goto of a programmer for dynamic memory allocation , here is a simple 2d array allocation code :
int main()
{
int r = 3, c = 4, i, j, count;
int *arr[r];
//allocating memory for an array
for (i=0; i<r; i++)
arr[i] = (int *)malloc(c * sizeof(int));
/*code to debug */
//free memory before allocating again
for(i=0; i<r; i++)
free(arr[i]);
//reallocating memory for an array
for(i=0; i<r+2; i++)
arr[i] = (int *)malloc(c * sizeof(int));
return 0;
/* code to work on reallocated array */
}
I am having some issues using write(). I am writing a 2d array to a .txt file but when it gets written to the file a bunch of NUL chars(not NULL) get added to the file. so I went searching around and found this to remove characters but was of no help I tried removing \0, 00, not sure how to deal with the nuls.
trying some things out I tried doing sizeof(table[0][0]) would give me 4 then I did strncpy(buffer, table[0][0], 2) but I guess it doesn't work like that and was of no help.
This is part of my program, I can post the rest if needed.
update:Added whole program at bottom
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
int main()
{
char *table[8][8] = {{NULL}};
int fdi;
char *buffer;
char msg[1] = "\n";
char msg1[1] = ",";
fdi = open("board.txt", O_RDWR);
if (fdi != -1)
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
buffer = table[i][j];
//strcpy(buffer,table[i][j]);
write(fdi, buffer, sizeof(buffer));
write(fdi, msg1, sizeof(msg1));
}
write(fdi, msg, sizeof(msg));
}
close(fdi);
}
}
This is the output I get
This is what I'm wanting to get
Thanks for any input.
#include <stdio.h>
#include <stdlib.h> // For exit() function
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>//throwing a warning write() seems to work fine without it
#define MAXC 1024
int getColRow(char *input)
{
int val = 0;
for (int i = 0; input[i]; i++)
{
input[i] = tolower(input[i]);
}
switch (input[0])
{
//row
case 97:
val = 0;
break;
case 98:
val = 1;
break;
case 99:
val = 2;
break;
case 100:
val = 3;
break;
case 101:
val = 4;
break;
case 102:
val = 5;
break;
case 103:
val = 6;
break;
case 104:
val = 7;
break;
//col
case 49:
val = 7;
break;
case 50:
val = 6;
break;
case 51:
val = 5;
break;
case 52:
val = 4;
break;
case 53:
val = 3;
break;
case 54:
val = 2;
break;
case 55:
val = 1;
break;
case 56:
val = 0;
break;
default:
break;
}
return val;
}
int main()
{
char c[1000];
char ch[1000];
FILE *fp, *fd, *op;
char word[1000], *token;
char *table[8][8] = {{NULL}};
int i = 0;
int j = 0;
char input[50];
char *endPos;
char *buffer;
char *exit = "exit";
char *show = "show";
char *move = "mv";
char *cp = "cp";
char *prq = "prq";
char *prr = "prq";
char *prb = "prb";
char *prk = "prk";
int a0Row, b0Col, a1Row, b1Col;
int fdi;
int len = 10;
if ((fp = fopen("board.txt", "r")) == NULL)
{
printf("Error! opening file");
return 0;
}
// if ((fd = fopen("board.txt", "r")) == NULL)
// {
// printf("Error! opening file");
// return 0;
// }
//print csv file(formated) one first time to see starting board
// while (fgets(ch, MAXC, fd))
// {
// for (char *tok = strtok(ch, ","); tok; tok = strtok(NULL, ","))
// {
// size_t len;
// tok[strcspn(tok, "\n")] = 0; /* trim '\n' from end tok */
// printf("%s ", tok);
// }
// printf("\n");
// }
//fill array
while (fscanf(fp, "%3s", c) != EOF)
{
token = strtok(c, ",");
len = strlen(token);
//printf("%s ", token);
table[i][j] = malloc(len + 1);
memcpy(table[i][j++], token, len + 1);
if (j >= 8)
{
i++;
j = 0;
//printf("\n");
}
}
do
{
int counter1 = 8;
int counter2 = 8;
fd = fopen("board.txt", "r");
printf("\n\t\t\tCHESS BOARD\n\n");
printf(" a b c d e f g h");
printf("\n |-------------------------------------------------------|\n");
while (fgets(ch, MAXC, fd))
{
i = 0;
j = 0;
for (char *tok = strtok(ch, ","); tok; tok = strtok(NULL, ","))
{
if (i == 0)
{
printf("%d |", counter1);
i++;
counter1--;
}
if (j >= 8)
{
i == 0;
}
size_t len;
tok[strcspn(tok, "\n")] = 0; /* trim '\n' from end tok */
printf(" %s |", tok);
j++;
if (j == 8)
{
printf(" %d", counter2);
counter2--;
j == 0;
}
}
printf("\n |-------------------------------------------------------|\n");
}
printf(" a b c d e f g h");
fclose(fd);
printf("\n\n\nwhats your move (mv, cp, prQ, prR, prB, prK, show, exit): ");
scanf("%s", &input);
for (int i = 0; input[i]; i++)
{
input[i] = tolower(input[i]);
}
if (strcmp(input, show) == 0)
{
fp = fopen("board.txt", "r");
if (fp == NULL)
{
printf("Could not open file");
return 1;
}
printf("\ncontents of board.csv\n\n");
while (fgets(input, 10, fp) != NULL)
printf("%s", input);
fclose(fp);
printf("\n\n");
}
else if (strcmp(input, move) == 0)
{
printf("What Piece are you wanting to move\nEnter row(1-8): ");
scanf("%s", &input);
a0Row = getColRow(input);
printf("Enter column(a-h): ");
scanf("%s", &input);
b0Col = getColRow(input);
printf("\nWhere do you want to move it to\nEnter row(1-8): ");
scanf("%s", &input);
a1Row = getColRow(input);
printf("Enter column(a-h): ");
scanf("%s", &input);
b1Col = getColRow(input);
endPos = table[a1Row][b1Col];
table[a1Row][b1Col] = table[a0Row][b0Col];
table[a0Row][b0Col] = endPos;
printf("\n\n\n");
for (i = 0; i < 8; i++)
{
//printf("\n");
for (j = 0; j < 8; j++)
{
printf("%s ", table[i][j]);
}
printf("\n");
}
//
//this is where the problem starts
//
//
fdi = open("board.txt", O_RDWR);
char msg[1] = "\n";
char msg1[1] = ",";
for(i =0; i<8;i++)
{
for(j=0; j<8;j++)
{
printf("size %d ", sizeof(table[i][j]));
}
printf("\n");
}
buffer = malloc(*table[0][0]);
if (fdi != -1)
{
for (i = 0; i < 8; i++)
{
for (j = 0; j < 8; j++)
{
buffer = table[i][j];
//removeChar(buffer, (int)NULL);
//strcpy(buffer,table[i][j]);
write(fdi, buffer, sizeof(buffer));
write(fdi, msg1, sizeof(msg1));
}
write(fdi, msg, sizeof(msg));
}
close(fdi);
}
}
//
// Issue Ends here
//
//
} while (strcmp(input, exit) != 0);
printf("\n\n\n");//testing printing
for (i = 0; i < 8; i++)
{
//printf("\n");
for (j = 0; j < 8; j++)
{
//printf("table[%d][%d] = %s|", i, j, table[i][j]);
printf("%s ", table[i][j]);
}
printf("\n");
}
fclose(fp);
return 0;
}
I am confused, when i use array values to assign another array values. the original array deletes the values used
int main(int argc, char *argv[]) {
char original[ORIGINAL_SIZE];
int isbn[ISBN_SIZE];
int index = 0;
int code;
int weight = 10;
int weightedValue;
int weightedSum = 0;
printf("Enter an ISBN to validate: ");
validateISBNArray(original);
while(index < ORIGINAL_SIZE){
if(original[index] != '-'){
if(original[index] == 'x' || original[index] == 'X') isbn[index] = 10;
else if(original[index] == 0) isbn[index] = 0;
else isbn[index] = original[index]-48;
code = isbn[index];
//printf("%d", code);
weightedValue= code*weight;
weight--;
weightedSum += weightedValue;
}
index++;
}
printf("%s",original);
if(weightedSum%11==0) printf("The ISBN %s, is VALID", original);
else printf("The ISBN %s, is NOT VALID", original);
return 0;
}
validateISBNArray has no effect on original array
this is the code for the not affecting function
void validateISBNArray(char array[]){
int index = 0;
int countDigits = 0;
int value = 0;
clearArray(array, ORIGINAL_SIZE);
scanf("%s",array);
while(index < ORIGINAL_SIZE){
//printf("%d %c %d\n", index, array[index], countDigits);
if(((array[index]-48) >= 0 && (array[index]-48) <= 9) || (array[index] == 'x'|| array[index] == 'X')) {
countDigits++;
index++;
}
else if(array[index] == '-' || array[index] == 0) index++;
else{
printf("INVALID CHARACTER %d = %c. Please Enter Digits Or/And Hyphens Only: ", index, array[index]);
index = 0;
countDigits = 0;
clearArray(array, ORIGINAL_SIZE);
scanf("%s",array);
}
if(index == ORIGINAL_SIZE && countDigits != 10){
printf("INVALID NUMBER OF DIGITS %d. Please Enter 10 Digits: ", countDigits);
index = 0;
countDigits = 0;
clearArray(array, ORIGINAL_SIZE);
scanf("%s",array);
}
}
//printf("%s", array);
}
Ok I fixed it removing the need for the isbn array
int main(int argc, char *argv[]) {
char original[ORIGINAL_SIZE];
int isbn[ISBN_SIZE];
int index = 0;
int code;
int weight = 10;
int weightedValue;
int weightedSum = 0;
printf("Enter an ISBN to validate: ");
validateISBNArray(original);
while(index < ORIGINAL_SIZE){
if(original[index] != '-'){
if(original[index] == 'x' || original[index] == 'X') code = 10;
else if(original[index] == 0) code = 0;
else code = original[index]-48;
weightedValue = code*weight;
weight--;
weightedSum += weightedValue;
}
index++;
}
if(weightedSum%11==0) printf("The ISBN %s, is VALID", original);
else printf("The ISBN %s, is NOT VALID", original);
return 0;
}
Originally I was using scanf, but I was running into the newline char getting stuck in the stdin. Everything I have read was saying to switch to fgets and use sscanf instead. With that, I decided to switch to that...but that still is not working. Below you will find my code. My question is, what am I doing wrong with my fgets and sscanf that is causing it to not wait for the user input?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct f_in{
char outline;
int lines;
int rows;
int num_args;
} F_IN;
typedef struct args_in {
char in_string[20];
int t_format;
} ARGS_IN;
void printInterface(char argQs[5][50], char argChar);
int main(int argv, char** argc){
char defaultQuestions[5][50] = { { "1) What char for border?" }
, { "2) Add question" }
, { "3) Remove Question" }
, { "4) Print last answers" }
, { "5) Exit" } };
int commandEntry, exitFlag;
char borderChar = '*', addQ[50],userInp[1];
exitFlag = 1;
while (exitFlag){
printInterface(defaultQuestions, borderChar);
printf("Enter the integer value for the command you wish to select: ");
fgets(userInp, sizeof(userInp),stdin);
sscanf(userInp,"%d", &commandEntry);
printf("\nYou selected: %s\n", defaultQuestions[commandEntry - 1]);
userInp[0] = 0;
if (commandEntry == 1){
printf("Please enter the character you wish to be the border: ");
fgets(userInp,sizeof(userInp),stdin);
sscanf(userInp,"%c",&borderChar);
}
else if (commandEntry == 2){
printf("What question would you like to add? (only enter 50 char max)\n");
fgets(addQ, 50, stdin);
printf("This was your question: %s", addQ);
}
else if (commandEntry == 5){
printf("Goodbye!\n");
exitFlag = 0;
}
}
return 0;
}
void printInterface(char argQs[5][50], char argChar){
int i, j;
int lineCnt = 13;
int borderLen = 75;
for (i = 0; i<100; i++){
printf("\n");
}
for (i = 0; i<lineCnt; i++){
if (i == 0 || i == lineCnt - 1){
for (j = 0; j<borderLen; j++){
printf("%c", argChar);
}
printf("\n");
}
else if (i >= 3 && i <= 10){
printf("%c %s", argChar, argQs[i - 3]);
for (j = 0; j < ((borderLen - strlen(argQs[i - 3]))-6); j++){
printf(" ");
}
printf("%c\n", argChar);
}
else{
for (j = 0; j<borderLen; j++){
if (j == 0){
printf("%c", argChar);
}
else if (j == (borderLen - 1)){
printf("%c\n", argChar);
}
else{
for (j = 0; j<borderLen; j++){
if (j == 0){
printf("%c", argChar);
}
else if (j == (borderLen - 1)){
printf("%c\n", argChar);
}
else{
printf(" ");
}
}
}
}
for (i = 0; i<10; i++){
printf("\n");
}
}
"userInp[1] only allows enough memory to store the terminating '\0'"
- user312023
I am trying have a program end when the user hits the Enter key. For some reason it doesn't seem to work. When I use "char c is not equal to enter key" it takes in an extra integer in c (the last inputted integer). What is the problem with this code?
#include <stdio.h>
#include <stdlib.h>
#define framenumber 4
int test1 =0;
int test2=1;
int main(void)
{
int mainarray[framenumber][2] = {0}, nHP = 3, takein, iPT;
char c = getchar();
printf("Enter: ");
while(1)
{
char c = getchar();
if(c == '\n') {
printf("here");
}
else
{
printf("not enter\n");
takein = atoi(&c);
for (iPT = 0; mainarray[iPT][test2] != takein && iPT < framenumber; iPT++);
if (mainarray[iPT][test2] != takein)
{
//search for a victim
do {
nHP = (nHP + 1) % framenumber;
} while ( !( mainarray[nHP][test1] == 1 ? mainarray[nHP][test1] = 0 : 1 ) );
//update the page table
mainarray[nHP][test1] = 1;
mainarray[nHP][test2] = takein;
}
else
{
mainarray[iPT][test1] = 1;
}
puts("page table:");
for (iPT = 0; iPT < framenumber; iPT++)
{
printf("%s %d, %d.\n", iPT == (nHP + 1) % 4 ? ">": " ", mainarray[iPT][test1], mainarray[iPT][test2]);
}
putchar('\n');
printf("Enter: ");
}
}
return 0;
}
Do not create block variable. (In while loop).
char c='\0'; /* initialize with 0 */
printf("Enter: ");
while(c!='\n') /* loop terminate condition */
{
c= getchar(); /* remove declaration */
if(c =='\n')
{
printf("here");
}
else
{
getchar(); /* read (eat) an extra input */
printf("not enter\n");
....