Neither. This loop does what you want:Originally Posted by transgalactic2
Code:for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch != EOF; ++i) { input[i] = ch; } input[i] = '\0';
Neither. This loop does what you want:Originally Posted by transgalactic2
Code:for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch != EOF; ++i) { input[i] = ch; } input[i] = '\0';
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
does this mean you can enter either one or five characters at a time into the array input[]??
how about something that places one char at a time into input[] upto a max of 39 chars while ignoring the newline as in
Code:char name[40]; printf("enter string:\n"); for (i=0; i < (sizeof name) - 1; i++) scanf("%c%*c", &name[i]);
Well I wouldn't recommend that snippet itCbitC:
1. The format string is wrong and would only store every other letter.
2. The string must be forty characters, no more, no less. That seems like a mistake.
If transgalactic wants to read everything up to either 40 bytes or a newline (whichever happens first) then laserlight's solution is decent.
I'm not sure where it's wrong. Yes it will store every other letter if the OP enters a string of chars on stdin; though it appears that the OP wants to enter a single character followed by a newline on stdin. I could be totally wrong though.
It stores no more than 39 chars in the array input[], if you could be specific it would help.
This is based on the OP entering a char at a time on stdin, the char goes into input[] and the newline gets skipped. Ofcourse if the OP enters a string of chars on stdin followed by a newline then it will store every other char into input[] and yes laserlight's solution is decent.
Well, by now we all know that transgalactic2 tends to leave out important information until after the suggestions have been madeOriginally Posted by itCbitC
That said, the "in a row" makes it sound like your interpretation is wrong: in your case, the user enters the string "in a column".
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
>> It stores no more than 39 chars in the array input[], if you could be specific it would help.
No, it reads exactly 40 bytes into input[]. Actually run your snippet. If my name werre Jessica Albert, and I typed that in, the program hangs. This is because the name is too short to actually stop your loop.
my input is like this:
i enter all the string in one time no new line
only one input in one line
the string ends when i press enter
if i will write down "(1,2)(3,5)"
it need to put these chars on the start of the 40 char array
the first ten chars will be
"(1,2)(3,5)"
is that what this code does
Code:for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch != EOF; ++i) { input[i] = ch; } input[i] = '\0';
That is what it is supposed to do. Whether it actually does that depends on how you use the code.Originally Posted by transgalactic2
EDIT:
Quoted for truth:
Originally Posted by whiteflags
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
i tried to use this loop in a while loop and it does not let me to enter a string.
when i choose option 2 its supposed to let me input a string in a row.
(i marked the loop with "start input" "end input")
but its just skips this part and shows the main menu again.
why??
Code:#include <stdio.h> #define N 9 int is_valid(const char input[],int leng); void mainPlayGame(board); int playGame(char board[N][N],int size); void printBoard(char board[N][N],int size); int removeMine(char board[N][N],int size,int x,int y); void mainMenu(); int unCover(char board[N][N],int size,int x,int y); int main(){ char input[40]; char input2[40]; char board[N][N]; mainPlayGame(board); printf("bye, please press enter to exit!\n"); getchar(); return 0; } void mainPlayGame(board){ int opt,leng,size=-1; int index,kndex; char input[40]; char ch_cords[40]; char input2[40]; int tr; int cords[8][2]; int tndex; int i,k,j,ch; int leng2; do { mainMenu(); scanf("%d",&opt); if (opt==1){ printf("enter board size [1..9] (no input control is needed):\n"); scanf("%d",&size); } if (opt==2){ //start input printf("enter string\n"); for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch != EOF; ++i) { input2[i] = ch; } input2[i] = '\0'; }//end if opt 2 //end input if (opt==3){ } if (opt==4){ } if (opt==5){ } if (opt==0){ } }while(opt!=0); } int playGame(char board[N][N],int size){ } void mainMenu() { printf("--------------------------\n"); printf("welcome to the game\n"); printf("1. choose board size\n"); printf("2. place mines\n"); printf("3. remove mines\n"); printf("4. show mines\n"); printf("5. start the game\n"); printf("0. exit\n"); printf("please enter your choice (input control is needed): \n"); } void printBoard(char board[N][N],int size){ int index,kndex; for(index=0;index<size;index++){ for(kndex=0;kndex<size;kndex++){ printf("+-+"); } printf("\n"); for(kndex=0;kndex<size;kndex++){ printf("|%c|",board[index][kndex]); } printf("\n"); } for(kndex=0;kndex<size;kndex++){ printf("+-+"); } } int removeMine(char board[N][N],int size,int x,int y) { if (board[y-1][x-1]=='*'){ board[y-1][x-1]=' '; printf("mine removed"); return 1; } else { printf("wrong input or no mine"); return 0; } } int unCover(char board[N][N],int size,int x,int y){ int count=0; if (board[y-1][x-1]=='*'){ printf("you lost\n"); board[y-1][x-1]='X'; printBoard(board, size); return 0; } else { if (((y-2)>=0) && ((x-2)>=0) && (board[y-2][x-2]=='*')){ count++; } if (((y-2)>=0) && ((x-1)>=0) && (board[y-2][x-1]=='*')){ count++; } if (((y-2)>=0) && ((x)<size) && (board[y-2][x]=='*')){ count++; } if (((y)<size) && ((x-2)>=0) && (board[y][x-2]=='*')){ count++; } if (((y)<size) && ((x-1)>=0) && (board[y][x-1]=='*')){ count++; } if (((y)<size) && ((x)<size) && (board[y][x]=='*')){ count++; } if (((y-1)>=0) && ((x-2)>=0) && (board[y-1][x-2]=='*')){ count++; } if (((y-1)>=0) && ((x)<size) && (board[y-1][x]=='*')){ count++; } board[y-1][x-1]='0' +count; printBoard(board, size); return 1; } } int is_valid(const char input[],int leng) { int flag=3; int i=1; /* Match "(<number>,<number>)" */ int index; for(index=0;index<leng;index++){ if((input[index]!='(')&&(input[index]!=',')&&(input[index]!=')')){ if (((input[1])<'0')||((input[1])>'9')){ return 0; } } } if (input[0] != '(') { flag=(int)NULL; } if (((input[1])<'0')||((input[1])>'9')){ flag=(int)NULL; } i++; while((input[i]<='9')&&(input[i]>='0')){ i++; //increasing the address by1 } //it wiil get char '2' if (input[i] == ',') { //'2' differs ',' if (((input[i+1])<'0')||((input[i+1])>'9')){ flag=(int)NULL; } i++; while((input[i]<='9')&&(input[i]>='0')){ input++; //increasing the address by1 } if (input[i]==')'){ i++; } else flag=(int)NULL; } else{ flag=(int)NULL; } ////////////////////////////////////////////////////////////// if (flag ==(int)NULL) { return 0; } /* Match zero or more " (<number>,<number>)" */ while (input[i]!= '\0') { if (input[i] != '(') { flag=(int)NULL; } if (((input[i+1])<'0')||((input[i+1])>'9')){ flag=(int)NULL; } i++; while((input[i]<='9')&&(input[i]>='0')){ i++; //increasing the address by1 } //it wiil get char '2' if (input[i] == ',') { //'2' differs ',' if (((input[i+1])<'0')||((input[i+1])>'9')){ flag=(int)NULL; } i++; while((input[i]<='9')&&(input[i]>='0')){ i++; //increasing the address by1 } if (input[i]==')'){ i++; } else flag=(int)NULL; } else{ flag=(int)NULL; } if (flag ==(int)NULL) { return 0; } } return 1; }
Last edited by transgalactic2; 01-01-2009 at 02:40 PM.
I'm SURE this can be done simpler...Code:if (((y-2)>=0) && ((x-2)>=0) && (board[y-2][x-2]=='*')){ count++; } if (((y-2)>=0) && ((x-1)>=0) && (board[y-2][x-1]=='*')){ count++; } if (((y-2)>=0) && ((x)<size) && (board[y-2][x]=='*')){ count++; } if (((y)<size) && ((x-2)>=0) && (board[y][x-2]=='*')){ count++; } if (((y)<size) && ((x-1)>=0) && (board[y][x-1]=='*')){ count++; } if (((y)<size) && ((x)<size) && (board[y][x]=='*')){ count++; } if (((y-1)>=0) && ((x-2)>=0) && (board[y-1][x-2]=='*')){ count++; } if (((y-1)>=0) && ((x)<size) && (board[y-1][x]=='*')){ count++; }
(Repeats in several places)Code:if (input[0] != '(') { flag=(int)NULL; }
Surely this doesn't need to be using "(int)NULL", when what you REALLY want is flag=0? It's much clearer and simpler, and does what you want, doesn't it? NULL in C is used to indicate that a pointer is invalid - should not be used for integer variables....
Doesn't switch/case seem like a better choice here?Code:if (opt==1){ printf("enter board size [1..9] (no input control is needed):\n"); scanf("%d",&size); } if (opt==2){ //start input printf("enter string\n"); for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch != EOF; ++i) { input2[i] = ch; } input2[i] = '\0'; }//end if opt 2 //end input if (opt==3){ } if (opt==4){ } if (opt==5){ } if (opt==0){ }
Do not write comments that explain things that SHOULD BE OBVIOUS to the reader.Code:i++; //increasing the address by1
Doesn't this loop do EXACTLY what the code above it does - repeating the same (particularly rather complex) code more than once in a program should be avoided - either break the code into a function, or do something else that doesn't involve having the same code twice....Code:/* Match zero or more " (<number>,<number>)" */ while (input[i]!= '\0') { ....
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
why it doesnt let me to enter a string
when i enter option2.
??
As usual: Because you are mixing scanf() with an input function that immediately exits when newline is found in the input stream - since scanf() leaves a newline at the end of input, it finds that immediately...
You could make it work by entering "2somestring", as scanf will read the 2, and then somestring will be read by the read string function.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I added a getchar line bellow the scanf.
thanks