Thread: need the input value to increment of input not the multiplied value can anyone help?

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    5

    Post need the input value to increment of input not the multiplied value can anyone help?

    Code:
    #include <stdio.h>
    
    
    void getNumber(int *input)
    {
    printf("Enter an integer number:");
    scanf("%d",input);
    }
    
    
    int incValue(int *val)
    {
      if( *val>1 && *val<100 )
      {
        *val+=10;
      }
      else if (*val>101 & *val<1000){
        *val+=20;
        
      }
      else{
        *val+=25;
      }
      return val;
    }
    
    
    void doubleValue(int *val)
    {
    *val *= 2;
    }
    
    
    
    
    void main(void)
    {
    int number;
    getNumber(&number);
    doubleValue(&number);
    printf("That value doubled is %d " ,number);
    
    
    incValue(&number);
    printf("That value incremented is %d " ,number);
    }

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    738
    If I understand you right, you want the original number input passed to both doubleValue() and incValue().

    main() ALWAYS returns an int!!!
    Error checking needed for scanf()
    Indent your code better!
    Set your warning level to the highest level, and don't ignore ANY warnings!!!
    See my notes in the code below.
    Code:
    #include <stdio.h>
    
    void getNumber(int *input)
    {
       printf("Enter an integer number:");
       scanf("%d",input); // Error checking needed!!!
    }
    
    int incValue(int *val)
    {
       if( *val>1 && *val<100 )
       {
          *val+=10;
       }
       else if (*val>101 && *val<1000){ // && Not &
          *val+=20;
    
       }
       else{
          *val+=25;
       }
       return *val; // Return the value, NOT the pointer!
    }
    
    
    void doubleValue(int *val)
    {
       *val *= 2;
    }
    
    // void main(void)
    
    int main(void) //main() ALWAYS returns an int!!!
    {
       int number = 0; // Always initialize all local variables
       int num_copy = 0;
    
       getNumber(&number);
    
       num_copy = number;  // Preserve the original value input
       doubleValue(&number);
       printf("That value doubled is %d \n" ,number);  // Added newline
    
       number = num_copy; // Replace with original value
       incValue(&number);
       printf("That value incremented is %d \n" ,number);   // Added newline
    
       return 0;  // Added
    }

  3. #3
    Registered User
    Join Date
    Jun 2019
    Posts
    5

    Arrow What is error checking that needs to be added i am new in coding

    I there anyway of preserving that value instead of adding any new variable just keeping the int main() same.

    i have changed pointer to val in return and understood what your point here however if is there any other way without changing int main()



    Quote Originally Posted by rstanley View Post
    If I understand you right, you want the original number input passed to both doubleValue() and incValue().

    main() ALWAYS returns an int!!!
    Error checking needed for scanf()
    Indent your code better!
    Set your warning level to the highest level, and don't ignore ANY warnings!!!
    See my notes in the code below.
    Code:
    #include <stdio.h>
    
    void getNumber(int *input)
    {
       printf("Enter an integer number:");
       scanf("%d",input); // Error checking needed!!!
    }
    
    int incValue(int *val)
    {
       if( *val>1 && *val<100 )
       {
          *val+=10;
       }
       else if (*val>101 && *val<1000){ // && Not &
          *val+=20;
    
       }
       else{
          *val+=25;
       }
       return *val; // Return the value, NOT the pointer!
    }
    
    
    void doubleValue(int *val)
    {
       *val *= 2;
    }
    
    // void main(void)
    
    int main(void) //main() ALWAYS returns an int!!!
    {
       int number = 0; // Always initialize all local variables
       int num_copy = 0;
    
       getNumber(&number);
    
       num_copy = number;  // Preserve the original value input
       doubleValue(&number);
       printf("That value doubled is %d \n" ,number);  // Added newline
    
       number = num_copy; // Replace with original value
       incValue(&number);
       printf("That value incremented is %d \n" ,number);   // Added newline
    
       return 0;  // Added
    }

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    738
    Quote Originally Posted by blueeyes View Post
    I there anyway of preserving that value instead of adding any new variable just keeping the int main() same.

    i have changed pointer to val in return and understood what your point here however if is there any other way without changing int main()
    Yes, of course!

    Call doubleValue(), and incValue() by value, not by pointer, and return the calculated value. Then call the functions as arguments to the printf() statements.

    You still need to add error checking code for the scanf() to insure that scanf() work correctly. What if I typed in "abc" as input to the program? ;^)

    Code:
    #include <stdio.h>
    
    void getNumber(int *input)
    {
       printf("Enter an integer number:");
       scanf("%d",input); // Error checking needed!!!
    }
    
    int incValue(int val)
    {
       if( val>1 && val<100 )
       {
          val+=10;
       }
       else if (val>101 && val<1000){ // && Not &
          val+=20;
    
       }
       else{
          val+=25;
       }
    
       return val; // Return the value
    }
    
    
    int doubleValue(int val)
    {
       return val *= 2;
    }
    
    // void main(void)
    
    int main(void) //main() ALWAYS returns an int!!!
    {
       int number = 0; // Always initialize all local variables
    
       getNumber(&number);
    
       printf("That value doubled is %d \n", doubleValue(number));  // Added newline
    
       printf("That value incremented is %d \n", incValue(number));   // Added newline
    
       return 0;  // Added
    }

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,875
    You don't have to save a copy of the value if you don't want to.


    Because of the way you are using incValue, I'm guessing that you don't want to return the value of "val".


    Also, you can use getNumber to initialise your "number" variable.


    You should also consider what you want done for the inputs 1 and 101 - Did you want to use ">=" instead of ">"? I've offered and alternative way of checking the input numbers.


    When looking at your code I've also added a fflush after your printf, because you don't end it with \n.

    Another thing I added is a little error checking on scanf - It returns the amount of successful conversions. This is just in case the user accidently types something else by accident. You could also check things like input range/positive inputs/...

    I've used EXIT_SUCCESS as defined in stdlib.h - It just looks neater.

    10, 20, and 25 are "magic numbers" - So I've made a define that gives it some meaning... You'll need to come up with names that actually say why those numbers are there.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define LT_100_INCVALUE 10
    #define LT_1000_INCVALUE 20
    #define DEFAULT_INCVALUE 25
    
    int getNumber(void)
    {
        int input = 0, iResult = 0;
    
        do
        {
            printf("Enter an integer number:");
    
            // Added because the printf doesn't end with \n
            // See: https://cboard.cprogramming.com/c-programming/98138-fflush-stdout.html#post708567
            fflush(stdout);
    
            iResult = scanf("%d", &input);
    
            if (iResult != 1)
            {
                puts("*** Error: Could not read number ***");
    
                //Try to get all the bad inputs off stdin
                //See https://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044873249&id=1043284392
                int ch;
                while ((ch = getchar())!='\n' && ch!=EOF)
                    continue;
            }
        }
        while (iResult != 1);
    
    
        return input;
    }
    
    
    void incValue(int *val)
    {
        if ( *val < 1)
        {
            // 0 and under...
            // Anything required here?
            // Are you letting the input be 0 or negative?
        }
        else if( *val < 100 )
        {
            *val += LT_100_INCVALUE;
        }
        else if (*val < 1000)
        {
            *val += LT_1000_INCVALUE;
        }
        else
        {
            *val += DEFAULT_INCVALUE;
        }
    }
    
    
    void doubleValue(int *val)
    {
        *val *= 2;
    }
    
    
    
    // FAQ on void/main
    // https://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044841143&id=1043284376
    int main(void)
    {
        int number = getNumber();
    
        doubleValue(&number);
        printf("That value doubled is %d\n", number);
    
        incValue(&number);
        printf("That value incremented is %d\n", number);
    
    
        return EXIT_SUCCESS;
    }
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Jun 2019
    Posts
    5

    Arrow no i need the input number to be incremented not the double value

    no i need the input number to be incremented not the double value
    Quote Originally Posted by Click_here View Post
    You don't have to save a copy of the value if you don't want to.


    Because of the way you are using incValue, I'm guessing that you don't want to return the value of "val".


    Also, you can use getNumber to initialise your "number" variable.


    You should also consider what you want done for the inputs 1 and 101 - Did you want to use ">=" instead of ">"? I've offered and alternative way of checking the input numbers.


    When looking at your code I've also added a fflush after your printf, because you don't end it with \n.

    Another thing I added is a little error checking on scanf - It returns the amount of successful conversions. This is just in case the user accidently types something else by accident. You could also check things like input range/positive inputs/...

    I've used EXIT_SUCCESS as defined in stdlib.h - It just looks neater.

    10, 20, and 25 are "magic numbers" - So I've made a define that gives it some meaning... You'll need to come up with names that actually say why those numbers are there.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define LT_100_INCVALUE 10
    #define LT_1000_INCVALUE 20
    #define DEFAULT_INCVALUE 25
    
    int getNumber(void)
    {
        int input = 0, iResult = 0;
    
        do
        {
            printf("Enter an integer number:");
    
            // Added because the printf doesn't end with \n
            // See: https://cboard.cprogramming.com/c-programming/98138-fflush-stdout.html#post708567
            fflush(stdout);
    
            iResult = scanf("%d", &input);
    
            if (iResult != 1)
            {
                puts("*** Error: Could not read number ***");
    
                //Try to get all the bad inputs off stdin
                //See https://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044873249&id=1043284392
                int ch;
                while ((ch = getchar())!='\n' && ch!=EOF)
                    continue;
            }
        }
        while (iResult != 1);
    
    
        return input;
    }
    
    
    void incValue(int *val)
    {
        if ( *val < 1)
        {
            // 0 and under...
            // Anything required here?
            // Are you letting the input be 0 or negative?
        }
        else if( *val < 100 )
        {
            *val += LT_100_INCVALUE;
        }
        else if (*val < 1000)
        {
            *val += LT_1000_INCVALUE;
        }
        else
        {
            *val += DEFAULT_INCVALUE;
        }
    }
    
    
    void doubleValue(int *val)
    {
        *val *= 2;
    }
    
    
    
    // FAQ on void/main
    // https://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044841143&id=1043284376
    int main(void)
    {
        int number = getNumber();
    
        doubleValue(&number);
        printf("That value doubled is %d\n", number);
    
        incValue(&number);
        printf("That value incremented is %d\n", number);
    
    
        return EXIT_SUCCESS;
    }

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,875
    Quote Originally Posted by blueeyes View Post
    no i need the input number to be incremented not the double value
    No worries - Is rstanley's answer sufficiant? Returning a caculated value is the way that you'd usually do it, rather than passing a pointer into a function. Passing a pointer into a function is for when you need to change the value of a variable in the scope that your in.

    If you were hell-bent on passing pointers, you could do something like this...
    Code:
    int main(void)
    {
        int userInput = getNumber();
    
        int doubleValueResult = userInput;
        int incValueResult = userInput;
    
        doubleValue(&doubleValueResult);
        printf("That value doubled is %d\n", doubleValueResult);
    
        incValue(&incValueResult);
        printf("That value incremented is %d\n", incValueResult);
    
    
        return EXIT_SUCCESS;
    }
    But as you can see, it requires more work for no extra gain in this example.
    Fact - Beethoven wrote his first symphony in C

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    738
    @Click_here & others

    FYI:

    Most of the people here are learning C, through a book, from an (Hopefully qualified) instructor, or against my recommendations, online tutorials, or YouTube videos! I cannot and do not make any assumptions about their learning process, or level.

    When a question is asked, I try to answer their question, and point out other obvious problems with the code, and compilation process. I DO NOT try to rewrite their code, add features they may not be ready for, or make major changes!! Unfortunately, I have seen this done too many times here. Doing so may intimidate posters, and scare them away.

    Yes, most of the time if I was handed the assignment, I would write the code differently. Most of us would. Making a lot of changes, or adding code and features, that they may not be ready for, may confuse the OP more than it helps them.

    I do speak from experience as a C instructor.

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,875
    Quote Originally Posted by rstanley View Post
    I DO NOT try to rewrite their code...

    Unfortunately, I have seen this done too many times here...
    Like in post 2...

    Quote Originally Posted by rstanley View Post
    ...
    See my notes in the code below.
    Code:
    #include <stdio.h>
    
    void getNumber(int *input)
    {
       printf("Enter an integer number:");
       scanf("%d",input); // Error checking needed!!!
    }
    
    int incValue(int *val)
    {
       if( *val>1 && *val<100 )
       {
          *val+=10;
       }
       else if (*val>101 && *val<1000){ // && Not &
          *val+=20;
    
       }
       else{
          *val+=25;
       }
       return *val; // Return the value, NOT the pointer!
    }
    
    
    void doubleValue(int *val)
    {
       *val *= 2;
    }
    
    // void main(void)
    
    int main(void) //main() ALWAYS returns an int!!!
    {
       int number = 0; // Always initialize all local variables
       int num_copy = 0;
    
       getNumber(&number);
    
       num_copy = number;  // Preserve the original value input
       doubleValue(&number);
       printf("That value doubled is %d \n" ,number);  // Added newline
    
       number = num_copy; // Replace with original value
       incValue(&number);
       printf("That value incremented is %d \n" ,number);   // Added newline
    
       return 0;  // Added
    }
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 10-29-2011, 07:02 PM
  2. Replies: 1
    Last Post: 09-24-2009, 01:28 AM
  3. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  4. How can terminate input when input specifed char?
    By Mathsniper in forum C Programming
    Replies: 5
    Last Post: 12-11-2006, 09:59 AM
  5. Help loading a vector with input from input stream
    By Bnchs in forum C++ Programming
    Replies: 9
    Last Post: 11-07-2006, 03:53 PM

Tags for this Thread