Thread: Variable not incrementing its value

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    12

    Variable not incrementing its value

    Hello , im new to the forum this is my first post, i subscribed because im really new to programming and i was ask to do a program which gave the user 4 options , to do some random things(to practice what we have learned) but on option 3 i need to ask the user for the age (edad) of the employees and count them until the user don't want to enter another age , then i have to show the count of mature ones (maduros) , seniors (mayores) , and youngsters (jovenes) , pretty much all work right now except that youngsters (variable jovenes in the program) just won't increase the value it always output 0 . i hope some of you can tell me what im i doing wrong .

    Thanks in advance.

    Here is the code:

    Code:
    
    
    Code:
    #include<stdio.h>
    
    
    
    int main(){
    
    
    	int opcion;
    
    
    printf("1.Capturar datos de la Empresa\n");
    printf("2.Mostrar datos de la empresa\n");
    printf("3.Edades de los empleados\n");
    printf("4.Salir\n");
    printf("\n");
    
    
    
    
    	while (opcion!=4){
    
    
    		int edad;
    		int mayores=0;
    		int maduros=0;
    		int jovenes=0;
    
    
    		char siono='y';
    		char salir='n';
    
    
    printf("opcion: ");
    	scanf("%d",&opcion);
    printf("\n");
    
    
    
    
    	char nombreEmpresa[60];
    	char giro[60];
    
    
    	switch (opcion){
    
    
    	case 1:
    printf("Nombre de la empresa:");
    		scanf("%s",nombreEmpresa);
    printf("Giro de la empresa:");
    		scanf("%s",giro);
    printf("\n");
    
    
    break;
    
    
    	case 2:
    printf("Nombre de la empresa:%s", nombreEmpresa);
    printf("\nGiro: %s",giro);
    printf("\n\n");
    
    
    break;
    
    
    	case 3:
    
    
    		while (siono != 'n'){
    
    
    printf("Escriba la edad de los empleados: ");
    		scanf("%d",&edad);
    
    
    		if (edad<0){
    printf("Error");
    		}
    
    
    		else if(edad>65){
    printf("Error\n\n");
    
    
    		}
    		else if(edad>=50){
    			mayores++;
    		}
    		else if(edad>=22){
    			maduros++;
    		}
    		else if(edad>=18){
    			jovenes++;
    		}
    
    
    else{
    printf("Error\n\n");
    		}
    
    
    
    
    printf("Desea ingresar otra edad? y/n ");
    		scanf ("%s",&siono);
    printf("\n");
    
    
    	}
    
    
    printf("Mayores: %d\n",mayores);
    printf("Maduros: %d\n",maduros);
    printf("Jovenes: %d\n",jovenes);
    printf("\n");
    
    
    break;
    
    
    
    
    	case 4:
    
    
    
    
    		while (salir !='y'){
    
    
    printf("Realmente desea salir? y/n ");
    		scanf("%s", &salir);
    		if(salir=='n'){
    			salir='y';
    			opcion=5;
    			printf("\n");
    
    
    		}
    
    
    		}
    
    
    break;
    
    
    default:
    printf("\n");
    
    
    break;
    
    
    	 }
    
    
    
    
    	}
    
    
    return 0;
    }
    
    

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to format your code properly, especially with respect to indentation. It is also better that you post your code as plain text, allowing the forum software to do the syntax highlighting for you. For example:
    Code:
    #include<stdio.h>
    
    int main() {
        int opcion;
    
        printf("1.Capturar datos de la Empresa\n");
        printf("2.Mostrar datos de la empresa\n");
        printf("3.Edades de los empleados\n");
        printf("4.Salir\n");
        printf("\n");
    
        while (opcion != 4) {
            int edad;
            int mayores = 0;
            int maduros = 0;
            int jovenes = 0;
    
            char siono = 'y';
            char salir = 'n';
    
            printf("opcion: ");
            scanf("%d", &opcion);
            printf("\n");
    
            char nombreEmpresa[60];
            char giro[60];
    
            switch (opcion) {
            case 1:
                printf("Nombre de la empresa:");
                scanf("%s",nombreEmpresa);
                printf("Giro de la empresa:");
                scanf("%s",giro);
                printf("\n");
                break;
            case 2:
                printf("Nombre de la empresa:%s", nombreEmpresa);
                printf("\nGiro: %s",giro);
                printf("\n\n");
                break;
            case 3:
                while (siono != 'n') {
                    printf("Escriba la edad de los empleados: ");
                    scanf("%d", &edad);
                    if (edad < 0) {
                        printf("Error");
                    }
                    else if(edad > 65) {
                        printf("Error\n\n");
                    }
                    else if(edad >= 50) {
                        mayores++;
                    }
                    else if(edad >= 22) {
                        maduros++;
                    }
                    else if(edad >= 18) {
                        jovenes++;
                    }
                    else {
                        printf("Error\n\n");
                    }
    
                    printf("Desea ingresar otra edad? y/n ");
                    scanf ("%s", &siono);
                    printf("\n");
                }
                printf("Mayores: %d\n", mayores);
                printf("Maduros: %d\n", maduros);
                printf("Jovenes: %d\n", jovenes);
                printf("\n");
                break;
            case 4:
                while (salir !='y') {
                    printf("Realmente desea salir? y/n ");
                    scanf("%s", &salir);
                    if (salir == 'n') {
                        salir = 'y';
                        opcion = 5;
                        printf("\n");
                    }
                }
                break;
            default:
                printf("\n");
                break;
            }
        }
        return 0;
    }
    Notice that now it is easier to see where the if statements, loops, the switch, etc, start and end. I have also removed the many unnecessary blank lines: a blank line or two (or sometimes even three at file scope) might be used to visually break parts of the code to group parts that logically belong together, but there is definitely no need to use more than three blank lines for this.

    One problem I see is that you use the opcion variable in the outer loop before giving it a value.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    scanf ("%s", &siono);
    siono is a single char variable, so %s is the wrong scanf specifier there. Same with salir:

    Code:
    scanf("%s", &salir);
    Be aware that reading a single character through scanf will leave the newline character in the input buffer, so you need get rid of that.

  4. #4
    Registered User
    Join Date
    Mar 2015
    Posts
    12
    Quote Originally Posted by laserlight View Post
    You need to format your code properly, especially with respect to indentation. It is also better that you post your code as plain text, allowing the forum software to do the syntax highlighting for you. For example:
    Code:
    #include<stdio.h>
    
    int main() {
        int opcion;
    
        printf("1.Capturar datos de la Empresa\n");
        printf("2.Mostrar datos de la empresa\n");
        printf("3.Edades de los empleados\n");
        printf("4.Salir\n");
        printf("\n");
    
        while (opcion != 4) {
            int edad;
            int mayores = 0;
            int maduros = 0;
            int jovenes = 0;
    
            char siono = 'y';
            char salir = 'n';
    
            printf("opcion: ");
            scanf("%d", &opcion);
            printf("\n");
    
            char nombreEmpresa[60];
            char giro[60];
    
            switch (opcion) {
            case 1:
                printf("Nombre de la empresa:");
                scanf("%s",nombreEmpresa);
                printf("Giro de la empresa:");
                scanf("%s",giro);
                printf("\n");
                break;
            case 2:
                printf("Nombre de la empresa:%s", nombreEmpresa);
                printf("\nGiro: %s",giro);
                printf("\n\n");
                break;
            case 3:
                while (siono != 'n') {
                    printf("Escriba la edad de los empleados: ");
                    scanf("%d", &edad);
                    if (edad < 0) {
                        printf("Error");
                    }
                    else if(edad > 65) {
                        printf("Error\n\n");
                    }
                    else if(edad >= 50) {
                        mayores++;
                    }
                    else if(edad >= 22) {
                        maduros++;
                    }
                    else if(edad >= 18) {
                        jovenes++;
                    }
                    else {
                        printf("Error\n\n");
                    }
    
                    printf("Desea ingresar otra edad? y/n ");
                    scanf ("%s", &siono);
                    printf("\n");
                }
                printf("Mayores: %d\n", mayores);
                printf("Maduros: %d\n", maduros);
                printf("Jovenes: %d\n", jovenes);
                printf("\n");
                break;
            case 4:
                while (salir !='y') {
                    printf("Realmente desea salir? y/n ");
                    scanf("%s", &salir);
                    if (salir == 'n') {
                        salir = 'y';
                        opcion = 5;
                        printf("\n");
                    }
                }
                break;
            default:
                printf("\n");
                break;
            }
        }
        return 0;
    }
    Notice that now it is easier to see where the if statements, loops, the switch, etc, start and end. I have also removed the many unnecessary blank lines: a blank line or two (or sometimes even three at file scope) might be used to visually break parts of the code to group parts that logically belong together, but there is definitely no need to use more than three blank lines for this.

    One problem I see is that you use the opcion variable in the outer loop before giving it a value.

    thank you, you are right is its quite easier to read , ill take better care of indentation from now on.
    i also initialized opcion to 0 , but the problem i have with the program is that it just wont display jovenes value it alway shows 0 when printed on the screen.

  5. #5
    Registered User
    Join Date
    Mar 2015
    Posts
    12
    Quote Originally Posted by rags_to_riches View Post
    Code:
    scanf ("%s", &siono);
    siono is a single char variable, so %s is the wrong scanf specifier there. Same with salir:

    Code:
    scanf("%s", &salir);
    Be aware that reading a single character through scanf will leave the newline character in the input buffer, so you need get rid of that.
    Thank you for pointing that out , i think thats the root of the problem i have , i corrected it but now it prints things like "desea salir" twice before asking for the input and it skips getting the input in the option 3 . Could you point out to me how to get rid of the buffer¿ ive tried with fflush(stdin) but i dont get any good result (maybe im placing it wrong)

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Don't use fflush(stdin). Be sure to read the link within that link; it has the answer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. incrementing a variable while using fscanf
    By synhyborex in forum C Programming
    Replies: 28
    Last Post: 03-12-2011, 02:11 AM
  2. Variable not incrementing correctly.
    By DonFord81 in forum C Programming
    Replies: 4
    Last Post: 07-21-2010, 02:10 PM
  3. Incrementing Twice?
    By Kemaratu in forum C Programming
    Replies: 3
    Last Post: 09-27-2009, 11:13 PM
  4. Incrementing by 2 not 1
    By Eckey in forum C++ Programming
    Replies: 6
    Last Post: 10-14-2004, 03:16 PM
  5. Having problems incrementing variable names
    By Legolas in forum C++ Programming
    Replies: 4
    Last Post: 11-11-2001, 10:14 PM