Thread: i need help with getting input from the user.

  1. #1
    Registered User nyekknyakk's Avatar
    Join Date
    Aug 2010
    Posts
    35

    i need help with getting input from the user.

    Hello, everyone!

    I am currently working on a project, a Matrix Calculator.
    But unlike any matrix calculator there, mine seems to be more complex, or so it seems.
    One of my problems is getting the input from the user.
    For example, if the user typed MAKE A, he/she will be able to make matrix A;
    MAKE B, will do matrix B, and so on;
    also if she typed ADDM A B C, the matrix calculator will add matrix A and B and put the answer to matrix C, or something like that.
    I've already tried a code for this but it can not be compiled.
    What do you think seems to be the problem?

    Code:
    /*Matrix Calculator*/
    
    #include <stdio.h>
    int make_mat();
    int disp_mat();
    int add_mat();
    int cmd_str[20];
    int mat_A[20][20], r1, c1;
    int mat_B[20][20], r2, c2;
    int mat_C[20][20], r3, c3;
    int i, j;
    
    int main(){
    	do{
    		printf("\n>>");
    		scanf("%s",cmd_str[20]);
    			if(cmd_str[0]=="MAKE")
    				make_mat();
    			else if(cmd_str[0]=="DISP")
    				disp_mat();
    			else if(cmd_str[0]=="ADDM")
    				add_mat();
    			else
    				printf("Invalid Command!");			
    	}while(cmd_str[0] != "EXIT")
    }
    
    /*Make Matrix Function*/
    int make_mat(){
    	if(cmd_str[5] == 'A'){
    		printf("Enter rows: ");
    	    scanf("%d", &r1);
    	    printf("Enter columns: ");
    	    scanf("%d", &c1);
    	    
            for(i=0;i<r1;i++)
    			for(j=0;j<c1;j++){
    				printf("A[%d][%d] = ", i, j);
    				scanf("%d",&mat_A[i][j]); }
    		}
    	else if(cmd_str[5] == 'B'){
    		printf("Enter rows: ");
    	    scanf("%d", &r2);
    	    printf("Enter columns: ");
    	    scanf("%d", &c2);
    	    
            for(i=0;i<r2;i++)
    			for(j=0;j<c2;j++){
    				printf("B[%d][%d] = ", i, j);
    				scanf("%d",&mat_B[i][j]); }
    		}
    	else if(cmd_str[5] == 'C'){
    		printf("Enter rows: ");
    	    scanf("%d", &r3);
    	    printf("Enter columns: ");
    	    scanf("%d", &c3);
    	    
            for(i=0;i<r3;i++)
    			for(j=0;j<c3;j++) {
    				printf("C[%d][%d] = ", i, j);
    				scanf("%d",&mat_C[i][j]); } 
    		}
    	else
    		printf("Invalid argument!");
    }
    /*Display Matrix Function*/
    int disp_mat(){
    	if(cmd_str[5] == 'A'){
    		printf("Number of rows: %d \n", r1);
    		printf("Number of columns: %d \n", c1);
    	    
    		for(i=0;i<r1;i++){
    			for(j=0;j<c1;j++)
    				printf("|%d|", mat_A[i][j]);
    				printf("\n"); }
    		}
    	else if(cmd_str[5] == 'B'){
    		printf("Number of rows: %d \n", r2);
    		printf("Number of columns: %d \n", c2);
    			
    		for(i=0;i<r2;i++){
    			for(j=0;j<c2;j++)
    				printf("|%d|", mat_B[i][j]);
    				printf("\n"); }
    		}
    	else if(cmd_str[5] == 'C'){
    		printf("Number of rows: %d \n", r3);
    		printf("Number of columns: %d \n", c3);
    			
    		for(i=0;i<r3;i++){
    			for(j=0;j<c3;j++)
    				printf("|%d|", mat_C[i][j]);
    				printf("\n"); } 
    		}
    	else
    		printf("Invalid Argument!");
    
    /*Add Matrix Function*/
    int add_mat(){
    	if(cmd_str[5] == 'A'){
    		if(cmd_str[7] == 'A'){
    			if(cmd_str[9] == 'A'){
    				for(i=0; i<r1; i++){
    					for(j=0; j<c1; j++)
    						mat_A[i][j] = mat_A[i][j] + mat_A[i][j]; }
    				}
    			else if(cmd_str[9] == 'B'){
    				for(i=0; i<r1; i++){
    					for(j=0; j<c1; j++)
    						mat_B[i][j] = mat_A[i][j] + mat_A[i][j]; }
    				}
    			else if(cmd_str[9] == 'C'){
    				for(i=0; i<r1; i++){
    					for(j=0; j<c1; j++)
    						mat_C[i][j] = mat_A[i][j] + mat_A[i][j]; }
    				}
    			else
    				printf("Invalid Argument!");
    			}
    		else if(cmd_str[7] == 'B'){
    			if(cmd_str[9] == 'A'){
    				for(i=0; i<r1; i++){
    					for(j=0; j<c2; j++)
    						mat_A[i][j] = mat_A[i][j] + mat_B[i][j]; }
    				}
    			else if(cmd_str[9] == 'B'){
    				for(i=0; i<r1; i++){
    					for(j=0; j<c2; j++)
    						mat_B[i][j] = mat_A[i][j] + mat_B[i][j]; }
    				}
    			else if(cmd_str[9] == 'C'){
    				for(i=0; i<r1; i++){
    					for(j=0; j<c2; j++)
    						mat_C[i][j] = mat_A[i][j] + mat_B[i][j]; }
    				}
    			else
    				printf("Invalid Argument!");
    			}
    		else if(cmd_str[7] == 'C'){
    			if(cmd_str[9] == 'A'){
    				for(i=0; i<r1; i++){
    					for(j=0; j<c3; j++)
    						mat_A[i][j] = mat_A[i][j] + mat_C[i][j]; }
    				}
    			else if(cmd_str[9] == 'B'){
    				for(i=0; i<r1; i++){
    					for(j=0; j<c3; j++)
    						mat_B[i][j] = mat_A[i][j] + mat_C[i][j]; }
    				}
    			else if(cmd_str[9] == 'C'){
    				for(i=0; i<r1; i++){
    					for(j=0; j<c3; j++)
    						mat_C[i][j] = mat_A[i][j] + mat_C[i][j]; }
    				}
    			else
    				printf("Invalid Argument!");
    			}
    		else
    			printf("Invalid Argument!");
    		}
    	if(cmd_str[5] == 'B'){
    		if(cmd_str[7] == 'A'){
    			if(cmd_str[9] == 'A'){
    				for(i=0; i<r2; i++){
    					for(j=0; j<c1; j++)
    						mat_A[i][j] = mat_B[i][j] + mat_A[i][j]; }
    				}
    			else if(cmd_str[9] == 'B'){
    				for(i=0; i<r2; i++){
    					for(j=0; j<c1; j++)
    						mat_B[i][j] = mat_B[i][j] + mat_A[i][j]; }
    				}
    			else if(cmd_str[9] == 'C'){
    				for(i=0; i<r2; i++){
    					for(j=0; j<c1; j++)
    						mat_C[i][j] = mat_B[i][j] + mat_A[i][j]; }
    				}
    			else
    				printf("Invalid Argument!");
    			}
    		else if(cmd_str[7] == 'B'){
    			if(cmd_str[9] == 'A'){
    				for(i=0; i<r2; i++){
    					for(j=0; j<c2; j++)
    						mat_A[i][j] = mat_B[i][j] + mat_B[i][j]; }
    				}
    			else if(cmd_str[9] == 'B'){
    				for(i=0; i<r2; i++){
    					for(j=0; j<c2; j++)
    						mat_B[i][j] = mat_B[i][j] + mat_B[i][j]; }
    				}
    			else if(cmd_str[9] == 'C'){
    				for(i=0; i<r2; i++){
    					for(j=0; j<c2; j++)
    						mat_C[i][j] = mat_B[i][j] + mat_B[i][j]; }
    				}
    			else
    				printf("Invalid Argument!");
    			}
    		else if(cmd_str[7] == 'C'){
    			if(cmd_str[9] == 'A'){
    				for(i=0; i<r2; i++){
    					for(j=0; j<c3; j++)
    						mat_A[i][j] = mat_B[i][j] + mat_C[i][j]; }
    				}
    			else if(cmd_str[9] == 'B'){
    				for(i=0; i<r2; i++){
    					for(j=0; j<c3; j++)
    						mat_B[i][j] = mat_B[i][j] + mat_C[i][j]; }
    				}
    			else if(cmd_str[9] == 'C'){
    				for(i=0; i<r2; i++){
    					for(j=0; j<c3; j++)
    						mat_C[i][j] = mat_B[i][j] + mat_C[i][j]; }
    				}
    			else
    				printf("Invalid Argument!");
    			}
    		else
    			printf("Invalid Argument!");
    		}
    	if(cmd_str[5] == 'C'){
    		if(cmd_str[7] == 'A'){
    			if(cmd_str[9] == 'A'){
    				for(i=0; i<r3; i++){
    					for(j=0; j<c1; j++)
    						mat_A[i][j] = mat_C[i][j] + mat_A[i][j]; }
    				}
    			else if(cmd_str[9] == 'B'){
    				for(i=0; i<r3; i++){
    					for(j=0; j<c1; j++)
    						mat_B[i][j] = mat_C[i][j] + mat_A[i][j]; }
    				}
    			else if(cmd_str[9] == 'C'){
    				for(i=0; i<r3; i++){
    					for(j=0; j<c1; j++)
    						mat_C[i][j] = mat_C[i][j] + mat_A[i][j]; }
    				}
    			else
    				printf('Invalid Argument!');
    			}
    		else if(cmd_str[7] == 'B'){
    			if(cmd_str[9] == 'A'){
    				for(i=0; i<r3; i++){
    					for(j=0; j<c2; j++)
    						mat_A[i][j] = mat_C[i][j] + mat_B[i][j]; }
    				}
    			else if(cmd_str[9] == 'B'){
    				for(i=0; i<r3; i++){
    					for(j=0; j<c2; j++)
    						mat_B[i][j] = mat_C[i][j] + mat_B[i][j]; }
    				}
    			else if(cmd_str[9] == 'C'){
    				for(i=0; i<r3; i++){
    					for(j=0; j<c2; j++)
    						mat_C[i][j] = mat_C[i][j] + mat_B[i][j]; }
    				}
    			else
    				printf("Invalid Argument!");
    			}
    		else if(cmd_str[7] == 'C'){
    			if(cmd_str[9] == 'A'){
    				for(i=0; i<r3; i++){
    					for(j=0; j<c3; j++)
    						mat_A[i][j] = mat_C[i][j] + mat_C[i][j]; }
    				}
    			else if(cmd_str[9] == 'B'){
    				for(i=0; i<r3; i++){
    					for(j=0; j<c3; j++)
    						mat_B[i][j] = mat_C[i][j] + mat_C[i][j]; }
    				}
    			else if(cmd_str[9] == 'C'){
    				for(i=0; i<r3; i++){
    					for(j=0; j<c3; j++)
    						mat_C[i][j] = mat_C[i][j] + mat_C[i][j]; }
    				}
    			else
    				printf("Invalid Argument!");
    			}
    		else
    			printf("Invalid Argument!");
    		}
    	else
    		printf("Invalid Argument!");
    }
    Thanks!

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    What compiler are you using? Have you turned the warnings on when you attempt to compile? You have some issues here, such as missing braces, and wrong use of quotes. You also have some pointer problems. Get the braces and the quotes figured out. If you get that much done, and still can't get it, come back and post your updated code, along with any warning/error messages from the compiler.

    By the way, you might want to think about changing the way you code. Write your code in smaller (working) chunks, and compile often. By writing a large segment of code, that has a ton of errors, it is harder to track down what the trouble is. Aim to keep your code compiling cleanly as you go along.

    Also, you can't do string comparisons like this in C:

    Code:
    if (cmd_str[0] == "MAKE")
    Have a look at the strcmp() function. As for your array, cmd_str, keep in mind that cmd_str[0] would not be able to hold the string "MAKE" - but only one character, presumably 'M'.

    Edit:

    You will also want to address the fact that you declare your functions as returning ints, but never actually return anything. If you don't want to return a value from a function, declare its return type as void:

    Code:
    void my_func(void);
    Note that my_func() has no return type (and so I won't try to return a value from it later, because my compiler will complain. As well, I am not passing any arguments in, so I declare the arguments as void also.
    Last edited by kermit; 08-17-2010 at 05:51 PM.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    26
    Hi nyekknyakk. The compiler errors/warnings are usually helpful in finding the source of bugs.

    In your program, these lines strike to me as problematic:
    Code:
    int cmd_str[20];
    It seems you have declared cmd_str an array of integers instead of array of chars (string).

    Code:
    if(cmd_str[0]=="MAKE")
    if(cmd_str[0]=="DISP")
    if(cmd_str[0]=="ADDM")
    In these lines you are comparing the first character of string cmd_str to a char*. Use strcmp instead.

  4. #4
    Registered User nyekknyakk's Avatar
    Join Date
    Aug 2010
    Posts
    35
    kermit, i'm using gcc as my compiler. i'm sorry about the missing braces and quotes.

    qwertylurker, may i ask what's the problem with the line:
    Code:
    int cmd_str[20];
    i don't understand what it means when you said that i've declared cmd_str as an array of ints instead of array of chars and that i'm comparing the first character of string cmd_str to a char*.
    what should i use instead?
    i've also tried strcmp but i don't know how will i use it if the user typed ADDM A B C.
    how will i use strcmp to compare cmd_str to A, B, or C.

    i'm sorry for commiting a lot of errors. i'm still at the learning stage.
    thanks for your replies!

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Well, if you are using gcc, start by compiling with -Wall -Wextra (if you don't do so already). This will give you a lot of help tracking down what your troubles are in the code, as it gives you specific line numbers which have the problems. For example:

    Code:
    gcc -Wall -Wextra -o your_program your_program.c
    Quote Originally Posted by nyekknyakk
    i don't understand what it means when you said that i've declared cmd_str as an array of ints instead of array of chars
    Code:
    int cmd_str[20]; /* cmd_str is an array of 20 ints */
    
    char cmd_str[20]; /* cmd_str is an array of 20 chars */


    Quote Originally Posted by nyekknyakk
    i've also tried strcmp but i don't know how will i use it if the user typed ADDM A B C.
    how will i use strcmp to compare cmd_str to A, B, or C.
    You will need to parse the user input into tokens. Take one thing at a time though - since you are new to this. Get what you have done already working well. Then add functionality to it as you learn about the language.
    Last edited by kermit; 08-17-2010 at 06:41 PM.

  6. #6
    Registered User nyekknyakk's Avatar
    Join Date
    Aug 2010
    Posts
    35
    oh..how will i compile with -Wall -Wextra on my Windows Vista?
    thanks about chars, i forgot about that.
    but how will i use strcmp, to compare the 1st, 2nd, 3rd, and 4th words of the string to a letter?
    am i going to write the code like this?
    Code:
    if( strcmp(cmd_str[0],"MAKE") == 0 )
    
    if( strcmp(cmd_str[5],'A') == 0 )
    
    if( strcmp(cmd_str[7],'A') == 0 )
    
    if( strcmp(cmd_str[9],'A') == 0 )

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by nyekknyakk
    oh..how will i compile with -Wall -Wextra on my Windows Vista?
    Stick to Linux. But seriously, those flags will work with gcc. If you are using an IDE without the command line, I would suggest you learn how to work on the command line. Otherwise, change your warnings in your preferences. Perhaps you could indicate which IDE (if any) you are using so some concrete advice on how to set it up can be given.

    Now then, strcmp() compares strings, hence the name strcmp.


    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        char mystring[] = "This is a string";
    
        if (strcmp(mystring, "This is not a string") != 0) {
            puts("The strings don't match!");
        }
    
        return 0;
    }
    Now in my example, I compare the string, mystring to the string literal "This is not a string" - of course they do not match. How can I put a string literal into strcmp, when strcmp expects a const char * ? In C, you can use a string literal pretty much anywhere a char * is expected, because by using the string literal, you are really just giving the pointer to the first character of the string literal. In other words, when I do something like this:

    Code:
    strcmp(mystring, "This is not a string")
    The string literal "This is not a string" is stored in read only memory, and when the strcpy() function is called, the compiler has set things up so that a pointer to the memory containing the string literal is in its place. In short, by putting a string literal in that call to strcpy(), I am actually giving strcpy() a pointer to char.

    Knowing these things, let's consider your original code:

    Code:
    if(cmd_str[0]=="MAKE")
    Remember what qwertylurker said? "In these lines you are comparing the first character of string cmd_str to a char*" Now we just talked about string literals and how when they are used in this kind of context, they turn out to be a pointer to the string in memory. So, "MAKE", being a string literal, is really just giving your if statement a char * (that is, a pointer to char). Now you are trying to compare it to the first element of cmd_str, which happens to be an int. You cannot compare a pointer to char with an int. Now if you were to change the type of cmd_str to type char, this would still not work because you would then be trying to compare a pointer to char to a char. Still not the same thing. The bottom line is this - use strcmp to compare entire strings to strings.

    Code:
    if( strcmp(cmd_str[5],'A') == 0 )
    Now here, you don't need strcmp(), because you are comparing char with char.

    Code:
    if( cmd_str[5] == 'A' )
    The above would work.
    Last edited by kermit; 08-17-2010 at 07:14 PM.

  8. #8
    Registered User nyekknyakk's Avatar
    Join Date
    Aug 2010
    Posts
    35
    hi! i used linux before but windows suits me better..
    i have netbeans installed here but i don't know how to use it, that's why i'm only using gcc!
    thanks for the answers, i will try those later!

    edit:
    but what shall i do with the code:
    Code:
    if(cmd_str[0]=="MAKE")
    is it:
    Code:
    if( strcmp(cmd_str[0],"MAKE")==0)
    the user is the one to input the word, it could be MAKE or ADDM it depends on the user.
    the program i'm working on will also ask for an integer from the user.
    the user will also have the ability to perform scalar multiplication, SMUL, which asks an integer, and the matrices:
    SMUL (scalar) (matrix1-to be multiplied to the scalar) (matrix2-where the answer will be store)
    for example,
    >>SMUL 2 A B
    so is it still okay to use char cmd_str instead of int cmd_str?
    Last edited by nyekknyakk; 08-18-2010 at 08:02 AM. Reason: insufficient answer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. handling user input
    By rodrigorules in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2009, 06:21 AM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  4. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 10:53 AM
  5. comparing user input
    By lambs4 in forum C Programming
    Replies: 5
    Last Post: 12-15-2002, 10:28 AM