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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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
    }

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    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
    }

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