Thread: Change/Keep Values (C beginner)

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    2

    Change/Keep Values (C beginner)

    Hey guys i'm new to C and i've got a little problem.

    I would like to write a program that gives you a few default values. Now you can keep these values by pressing the enter key or you can change them by just typing in new ones and confirm the new values by pressing the enter key again.

    Thats as far as i got on my own but it doesn't really work. I would appreciate it if one of you guys could tell what i can do to fix this.

    Code:
    #include<stdio.h>
    #include <conio.h>
    
    int getInt(int min, int max);
    
    int main ()
    {
        int a[3] = {3,4};
        int b;
        int code;
        int loop;
    
        for (b = 0 ; b < 2 ; b++){
            printf ("a[%d] = ",b);
        
            do{
                code = _getch();
    
                switch (code){
                case 13:
                    printf("%d\n", a[b]);
                    loop = 1;
                    break;
    
                default:
                    a[b] = getInt(0,6);
                    loop = 1;
                    break;
                }
    
            }while (loop != 1);
        }
    }
    
    
    int getInt(int min, int max)
    {
        int num;        
        char ch;        
        int error = 1;     
    
        do    {
            
            fflush(stdin);
    
            //Error if you enter a character
            if (scanf("%d%c", &num, &ch) != 2)
            {
                printf("Error you entered a character\n");
            }
    
            //Error if you enter an incorect value
            else if ((ch != 10) || (num > max) || (num < min))    //
            {
                printf("Error, inncorect range of values\n");
            }
    
            //No Error, exit loop
            else
            {
                error = 0;
            }
    
        } while (error == 1);
    
        return num;
    }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Erazz View Post
    Now you can keep these values by pressing the enter key or you can change them by just typing in new ones and confirm the new values by pressing the enter key again.

    Thats as far as i got on my own but it doesn't really work.
    If you want to allow user to press ENTER alone, then you need line-based input. I.e. use fgets to read a line into a buffer, and then use sscanf on that buffer just as you would use scanf. The way you're doing it now with scanf on its own, you can never catch the "user pressed ENTER" event.

    If user presses enter then you could probably assign the default value within your getInt function.

    Also note fflush(stdin) is not correct.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You should tell us exactly what you mean by "doesn't really work". What input are you giving it? What output do you expect? What output are you actually getting?

    Some additional comments:

    Don't flush stdin: FAQ > Why fflush(stdin) is wrong - Cprogramming.com
    Some alternatives here: FAQ > Flush the input buffer - Cprogramming.com

    conio.h is old and non-standard; you can use "getchar()" instead

    Avoid constants when checking for characters:

    Code:
    // instead of this:
    case 13:
    
    // use this:
    case '\n':
    
    // note that 13 is actually '\r', but you should check for '\n' as indicated
    Code:
    // instead of this:
    (ch != 10)
    
    // use this:
    (ch != '\n')
    Your use of arrays is a bit strange. You declare the array to have 3 elements, but you're only using two. And instead of magic numbers, you should #define a constant and use this instead. Not only does this increase readability, but if you want to expand the size of the array (and resulting code), you only have to change one value in your code, instead of hunting down all instances and changing each and every one (possibly missing some, leading to potential bugs).

    Code:
    #define MAX_SIZE 3
    
    // ...
    
    int array[MAX_SIZE] = ...
    
    // ...
    
    for(i=0; i<MAX_SIZE; i++)
        // ...
    Also, use prompts in your program so the user knows what they're supposed to be entering.

    And maybe you should print the array after the loop ends to see the final results.
    Last edited by Matticus; 10-11-2014 at 02:48 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I change #define values without recompiling?
    By maryfsan in forum C Programming
    Replies: 5
    Last Post: 07-31-2014, 03:06 AM
  2. Problems when trying to change values in an array
    By kdun in forum C++ Programming
    Replies: 8
    Last Post: 03-30-2013, 04:24 AM
  3. Beginner Needing Help making change
    By jp1978 in forum C++ Programming
    Replies: 8
    Last Post: 03-22-2012, 04:17 PM
  4. rand() seeded with time but values don't change
    By mmfuller in forum C++ Programming
    Replies: 7
    Last Post: 09-21-2009, 12:41 AM
  5. Unexpected change of input values
    By imrahaseen in forum C Programming
    Replies: 6
    Last Post: 06-22-2007, 02:25 AM