Thread: Multiplication question

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    13

    Multiplication question

    Hey all! Still very new to C; trying to learn during my free time after work, when I have it, whether on edX, codeacademy, or through some texts. Anyway, I have the following code:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    
    
    
    int main(void)
        {
            int showerTime[10]; // User input of how long shower is
            int bpm = 12; // Bottles per minute
            int bps = showerTime*bpm;
            
         
            
            printf("How many minutes does your average shower last? \n");
            scanf("%i", showerTime);
            
            
            
            if(isdigit(showerTime))
                {
                    printf("Your average shower is %i minutes long. This means that you consume roughly %i bottles of water per shower, on average.\n", showerTime, bps);
                }
            else
                {
                    printf("Please input a valid numerical value!\n");
                }
            
            
            return 0;
        }
    For some reason, I keep getting an error and I don't understand why. I assumed that my declaration of the variable "bps" was a simple x12 multiplication of the user input "showerTime". But when I originally declared "bps" as showerTime*12 I got an error so I tried assigning 12 to a variable as a workaround. I think the way the code is set up now is more verbose but I've been trying to bypass the error... anyway, here it is

    Code:
    water.c:11:29: error: invalid operands to binary expression ('int *' and 'int')
            int bps = showerTime*bpm;
                      ~~~~~~~~~~^~~~
    water.c:22:149: error: format specifies type 'int' but the argument has type 'int *' [-Werror,-Wformat]
                    printf("Your average shower is %i minutes long. This means that you consume roughly %i bottles of water per shower, on average.\n", showerTime, bps);

    Appreciate the help!
    Last edited by mcavanaugh8; 01-30-2016 at 04:52 PM.

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    678
    Code:
    int showerTime[10];
    You have declared an array of 10 integers, but they contain 10 garbage values since you have not initialized the integers.
    Code:
    int bps = showerTime*bpm;
    You are then multiplying the address of an array "showertime" by the integer "bpm".

    Similar problems:
    Code:
    scanf("%i", showerTime);
             
    if(isdigit(showerTime))
    Perhaps you meant?:
    Code:
    int showerTime;

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,671
    The variable showerTime is declared as an array of integers. Unless you're taking in more than one integer value from the user, you do not need an array.

    So it should be

    Code:
    int showerTime;
    Secondly, you are attempting to do calculations here before there are any valid values on which to perform calculations:

    Code:
    int bps = showerTime*bpm;
    You should simply declare the bps variable here, assigning it a known value to start, like 0:

    Code:
    int bps = 0;
    Next up, because you've changed showerTime to be a simple integer rather than an array, you need to pass scanf the address of the variable using the & operator:

    Code:
    scanf("%i", &showerTime);
    You were not getting any compiler error on that when it was an array because the name of an array is an alias to the address of the array's first element.

    The isdigit function operates on a character, but you're not giving it a character, you're actually giving it an integer. Therefore this method of checking the validity of the input won't work. Instead, if you look at the scanf man page, you'll see this:

    On success, the function returns the number of items of the argument list successfully filled.
    Therefore, you should instead change the scanf call such that you assign the value returned, like so:

    Code:
    int check = scanf("%i", &showerTime);
    If the value returned is 1, you can be certain that showerTime contains an integral value.

    Once you've determined that the user has provided an integer value, you can then do the bps calculation:

    Code:
    bps = showerTime * bpm
    Also, where bpm is constant, it is a better idea to declare it as such:

    Code:
    const int bpm = 12;
    Hope this helps you understand.

  4. #4
    Registered User
    Join Date
    Jan 2016
    Posts
    13
    Quote Originally Posted by rstanley View Post
    Code:
    int showerTime[10];
    You have declared an array of 10 integers, but they contain 10 garbage values since you have not initialized the integers.
    Code:
    int bps = showerTime*bpm;
    You are then multiplying the address of an array "showertime" by the integer "bpm".

    Similar problems:
    Code:
    scanf("%i", showerTime);
             
    if(isdigit(showerTime))
    Perhaps you meant?:
    Code:
    int showerTime;
    Ah, thanks! For some reason, I was under the impression that the number inside of the [] brackets was the length of the array--maybe that is just for characters? If not, I must have misunderstood something I read elsewhere. Much appreciated!

    Quote Originally Posted by rags_to_riches View Post
    The variable showerTime is declared as an array of integers. Unless you're taking in more than one integer value from the user, you do not need an array.

    So it should be

    Code:
    int showerTime;
    Secondly, you are attempting to do calculations here before there are any valid values on which to perform calculations:

    Code:
    int bps = showerTime*bpm;
    You should simply declare the bps variable here, assigning it a known value to start, like 0:

    Code:
    int bps = 0;
    Next up, because you've changed showerTime to be a simple integer rather than an array, you need to pass scanf the address of the variable using the & operator:

    Code:
    scanf("%i", &showerTime);
    You were not getting any compiler error on that when it was an array because the name of an array is an alias to the address of the array's first element.

    The isdigit function operates on a character, but you're not giving it a character, you're actually giving it an integer. Therefore this method of checking the validity of the input won't work. Instead, if you look at the scanf man page, you'll see this:



    Therefore, you should instead change the scanf call such that you assign the value returned, like so:

    Code:
    int check = scanf("%i", &showerTime);
    If the value returned is 1, you can be certain that showerTime contains an integral value.

    Once you've determined that the user has provided an integer value, you can then do the bps calculation:

    Code:
    bps = showerTime * bpm
    Also, where bpm is constant, it is a better idea to declare it as such:

    Code:
    const int bpm = 12;
    Hope this helps you understand.
    That was extremely helpful, thank you so much! As mentioned above, I think I may have misunderstood the purpose of an array.

    A follow-up question or two, if you don't mind:

    1. What is the benefit of declaring something a constant, rather than simply assigning it a value (like 12, in this example)?
    2. I will paste the new code (which works) below, but there is something odd with it. When the first user prompt from printf appears, my first input is not accepted. If I type "9" and hit ENTER, it will simply move to a new line as if it were awaiting another input. If I then input 7, it will execute the next step in the code and multiple 7*12 and execute the other printf. Any idea why this is?
    3. I have only a general understanding of loops, but I was wondering how I could adjust my code so that it not only tells the user to input a numerical value (if he/she types "k", for example), but then also re-executes the previous prompt (i.e.: asks the user how long he/she showers).

    Here's the new code:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    
    
    
    int main(void)
        {
            int showerTime; // User input of how long shower is
            const int bpm = 12; // Bottles per minute
            int bps = 0; // Bottles per shower. Value will be calculated later.
            
            
            printf("How many minutes does your average shower last?\n");
            scanf("%i", &showerTime);
            
            
            int check = scanf("%i", &showerTime);
            
            if(check == 1)
                {
                    bps = showerTime * bpm;
                    printf("Your average shower is %i minutes long. This means that you consume roughly %i bottles of water per shower, on average.\n", showerTime, bps);
                }
            else
                {
                    printf("Please input a valid numerical value!\n");
                    
                }
            
            
            return 0;
        }
    Thanks so much!

  5. #5
    Registered User
    Join Date
    Dec 2015
    Posts
    5
    Quote Originally Posted by mcavanaugh8 View Post
    2. I will paste the new code (which works) below, but there is something odd with it. When the first user prompt from printf appears, my first input is not accepted. If I type "9" and hit ENTER, it will simply move to a new line as if it were awaiting another input. If I then input 7, it will execute the next step in the code and multiple 7*12 and execute the other printf. Any idea why this is?
    You have called scanf() twice. First is the line right after the first printf (prompting user for shower length) and then right after again using check variable. As far as the program is concerned, you have asked for user input twice (for the same question and same variable). You can eliminate one. Actually...

    3. I have only a general understanding of loops, but I was wondering how I could adjust my code so that it not only tells the user to input a numerical value (if he/she types "k", for example), but then also re-executes the previous prompt (i.e.: asks the user how long he/she showers).
    Very basic user input verification would utilize something like the while loop.

    For instance, instead of having a variable to check the truth of the scanf input (that is, to verify whether or not user inputs an integer), you can combine them into something like:

    Code:
    printf("How many minutes does your average shower last? (q to quit)\n");
    while ((scanf("%i", &showerTime)) == 1)
    {
        blah blah blah
    }
    Since you are aware of "the truth" in C, you can see that the while loop will continue to execute so long as that particular scanf evaluates to 1 (that is, user inputs an integer). At this point, if the user printed anything other than an integer value, the program would cease. If the user inputs an integer, prompt the user after a run through to enter another value.

    A method like this is crude, of course, simply because you don't give the user another chance to input something correctly. But it's an example. Try looking at other forms of input and input verification.
    Last edited by feckless; 01-30-2016 at 08:29 PM.

  6. #6
    Registered User
    Join Date
    Jan 2016
    Posts
    13
    Quote Originally Posted by feckless View Post
    You have called scanf() twice. First is the line right after the first printf (prompting user for shower length) and then right after again using check variable. As far as the program is concerned, you have asked for user input twice (for the same question and same variable). You can eliminate one. Actually...


    Very basic user input verification would utilize something like the while loop.

    For instance, instead of having a variable to check the truth of the scanf input (that is, to verify whether or not user inputs an integer), you can combine them into something like:

    Code:
    printf("How many minutes does your average shower last? (q to quit)\n");
    while ((scanf("%i", &showerTime)) == 1)
    {
        blah blah blah
    }
    Since you are aware of "the truth" in C, you can see that the while loop will continue to execute so long as that particular scanf evaluates to 1 (that is, user inputs an integer). At this point, if the user printed anything other than an integer value, the program would cease. If the user inputs an integer, prompt the user after a run through to enter another value.

    A method like this is crude, of course, simply because you don't give the user another chance to input something correctly. But it's an example. Try looking at other forms of input and input verification.

    Thanks! So I fixed the first part, with your help. However, I'm still having issues with the input verification part. It's not so much an issue with getting separate feedback if the user puts in incorrect information rather than correct information, but rather getting the function to loop back to the start if it is an incorrect input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiplication is not right.
    By tmac619619 in forum C Programming
    Replies: 4
    Last Post: 10-11-2012, 03:05 PM
  2. multiplication without * & +
    By gourabbanik05 in forum C Programming
    Replies: 10
    Last Post: 08-31-2010, 12:17 AM
  3. Zero multiplication
    By Kempelen in forum C Programming
    Replies: 13
    Last Post: 05-09-2009, 08:44 AM
  4. Replies: 9
    Last Post: 04-20-2009, 12:34 PM
  5. Multiplication in GCC 2.953
    By Durafei in forum C Programming
    Replies: 2
    Last Post: 05-01-2003, 04:48 PM