Thread: entering numbers to array in a row whithout length input

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    so in this code.
    i use scanf or gets?
    Neither. This loop does what you want:
    Code:
        for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch != EOF; ++i)
        {
            input[i] = ch;
        }
        input[i] = '\0';
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #32
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by transgalactic2 View Post
    i can use scanf

    but i cant have the length being inputted by the user
    he can enter one coordinates and he can enter 5
    does this mean you can enter either one or five characters at a time into the array input[]??
    Quote Originally Posted by transgalactic2 View Post
    i know that the maximum string allowed is 40 chars long
    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]);

  3. #33
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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.

  4. #34
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by whiteflags View Post
    Well I wouldn't recommend that snippet itCbitC:
    1. The format string is wrong and would only store every other letter.
    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.
    Quote Originally Posted by whiteflags View Post
    2. The string must be forty characters, no more, no less. That seems like a mistake.
    It stores no more than 39 chars in the array input[], if you could be specific it would help.
    Quote Originally Posted by whiteflags View Post
    If transgalactic wants to read everything up to either 40 bytes or a newline (whichever happens first) then laserlight's solution is decent.
    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.

  5. #35
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by itCbitC
    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.
    Well, by now we all know that transgalactic2 tends to leave out important information until after the suggestions have been made

    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".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #36
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >> 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.

  7. #37
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by laserlight View Post
    Well, by now we all know that transgalactic2 tends to leave out important information until after the suggestions have been made
    yes that seems to the case here too
    Quote Originally Posted by laserlight View Post
    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".
    cool beans i finally see the light, so looks like the OP wants to enter a string of characters on stdin followed by a newline and nothing more.

  8. #38
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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';

  9. #39
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    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
    That is what it is supposed to do. Whether it actually does that depends on how you use the code.

    EDIT:
    Quoted for truth:
    Quote Originally Posted by whiteflags
    my name werre Jessica Albert
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #40
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by whiteflags View Post
    >> 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.
    Yes it's hungry and wants more if there is a shortage of data on stdin

  11. #41
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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.

  12. #42
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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++;
    	}
    I'm SURE this can be done simpler...

    Code:
        if (input[0] != '(')
        {
            flag=(int)NULL;
        }
    (Repeats in several places)
    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....

    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){
    	}
    Doesn't switch/case seem like a better choice here?

    Code:
    	i++; //increasing the  address by1
    Do not write comments that explain things that SHOULD BE OBVIOUS to the reader.

    Code:
        /* Match zero or more " (<number>,<number>)" */
        while (input[i]!= '\0')
        {
    ....
    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....

    --
    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.

  13. #43
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    why it doesnt let me to enter a string
    when i enter option2.

    ??

  14. #44
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    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.

  15. #45
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    I added a getchar line bellow the scanf.
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Problem getting the input from a temp variable into the Array
    By hello_moto in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2006, 01:50 AM
  3. array length
    By Wick in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2003, 04:53 PM
  4. Is there a bug in this part of my algorithm for connect 4?
    By Nutshell in forum Game Programming
    Replies: 8
    Last Post: 04-28-2002, 01:58 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM