Thread: faulty code?

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    14

    faulty code?

    I have been trying to figure out why the assignment operator for my total1-4 variables are not working. can anyone help?


    Code:
    #include <stdio.h>
    
    int main ()
    
    	{
    	
    
    int age, crntAdrs, anlIncome, sameJob;      			// The four factors that will determine credit worthiness
    	
    char letter; 										    //  If applying for credit card, Y or N                       	
    
    int total1, total2, total3, total4;						// Variable for points issued
    
    int pointTotal;											// Total of total1-4
    
    	
    		printf("\n\nHello, you have chosen to apply for a credit card");
    	
    		printf("\nwith ACME Banking & Trust.");
    
    		printf("\n\nIf this is correct, press \"Y\" for Yes or \"N\" for No: ");
    		
    			scanf("%c", &letter);
    		
    		if (letter == 'Y' || letter == 'y') 
    		    
    			printf("\n\nGreat, let's continue!");			      // The consumer chose to apply
    		
    		
    		else {
    			printf("\n\nYou are free to exit the program.");	  // The consumer did not choose to apply
    			
    			return 0; 
    		}
    		
    		
    		
    		//do {
    		
    		printf("\n\nPlease enter your age: ");					   
    		
    			scanf("%d", &age);
    	    
    		if ( age < 18 || age > 105) {                             // Input validation
    		 
    			
    			printf("\n\nYou have entered an age that is out of range.");
    			
    			printf("\n\nPlease enter an age in the range of 18 to 105 years.");
    		}	
    		
    		else {													  // Determine number of points based on age
    			
    			if ( age <= 20 ) 
    				total1 = -10;
    		
    			else if ( age > 20 || age <= 30 ) 
    				total1 = 0;
    		
    			else if ( age > 30 || age <= 50 ) 
    				total1 = 20;
    			
    			else if ( age > 50 ) 
    				total1 = 25;
    		}	
    		
    		// } while ( age < 18 || age > 105);
    		 		
    	
    		printf("%d", &total1);
    		
    		//do {
    		
    		printf("\n\nHow many years have you lived at your current address? ");
    		
    			scanf("%d", &crntAdrs);
    			
    			
    		if ( crntAdrs <= 0) {
    		
    			printf("\n\nThe amount of time you entered is out of range.");
    			
    			printf("\n\nEnter an amount of time greater than or equal to 1 year.");
    		}
    		
    		else {														// Determine number of points based on time at current address
    		
    			if ( crntAdrs < 1 )
    				total2 = -5;
    			
    			else if ( crntAdrs == 1 || crntAdrs <= 3 )
    				total2 = 5;
    		
    			else if ( crntAdrs == 4 || crntAdrs <= 8 )
    				total2 = 12;
    			
    			else if ( crntAdrs == 9 || crntAdrs >= 9 )
    				total2 = 20;
    		}	
    			
    		//} while ( crntAdrs <= 0);
    		
    		
    		
    		//do {
    		
    		printf("\n\nWhat is your annual income? ");
    		
    			scanf("%d", &anlIncome);
    			
    			
    		if ( anlIncome <= 0 ) 
    		
    			printf("\n\nThe income amount you entered is out of range.");
    		
    		
    		else {														    // Determine number of points based on annual income
    		
    			if ( anlIncome < 15000 )
    				total3 = 0;
    			
    			else if ( anlIncome > 15000 || anlIncome <= 25000 )
    				total3 = 12;
    			
    			else if ( anlIncome > 25000 || anlIncome <= 40000 )
    				total3 = 24;
    			
    			else if ( anlIncome > 40000 )
    				total3 = 30;
    		}	
    		
    		//} while ( anlIncome <=0);
    
    		
    		
            //do {
    		
    		printf("\n\nHow long have you been employed? ");
    		
    			scanf("%d", &sameJob);
    			
    			
    		if ( sameJob <= 0 ) 
    		
    			printf("\n\nThe employment time frame you entered is out of range.");
    		
    		
    		else {														// Determine the number of points based on the length of employment
    		
    			if ( sameJob < 2 ) 
    				total4 = -4;
    		
    			else if ( sameJob >= 2 || sameJob <= 4 ) 
    				total4 = 8;
    			
    			else if ( sameJob > 4 )
    				total4 = 15;
    		}
    
    		//} while ( sameJob <= 0 );
    		
    		printf(" total l is %d ", &total1);
    		printf(" total 2 is %d ", &total2);
    		printf(" total 3 is %d ", &total3);
    		printf(" total 4 is %d ", &total4);
    		
    
    		pointTotal = ( total1 + total2 + total3 + total4 );			  // Total number of credibility points
    	
    
    	
    		if ( pointTotal == -19 || pointTotal <= 20 ) { 
    		
    			printf("\n\nYour point total for credit worthiness is %d.", pointTotal);
    			
    			printf("\n\nWe're sorry, at this time we are not able to offer you");
    			
    			printf("\ncredit with us.\n\n");
    		}
    		
    		else {
    		
    			if ( pointTotal == 21 || pointTotal <= 35 ) {
    		
    			printf("\n\nYour point total for credit worthiness is %d.", pointTotal);
    			
    			printf("\n\nCongratulations! You have been granted a $500 credit limit!\n\n");
    		
    		}
    			else if ( pointTotal == 36 || pointTotal <= 60 ) { 
    		
    			printf("\n\nYour point total for credit worthiness is %d.", pointTotal);
    		
    			printf("\n\nCongratulations! You have been granted a $2000 credit limit!\n\n");
    		
    		}
    			else if ( pointTotal == 61 || pointTotal <= 90 ) {
    		
    			printf("\n\nYour point total for credit worthiness is %d.", pointTotal);
    			
    			printf("\n\nCongratulation! You have been granted a $5000 credit limit!\n\n");
    		}	
    		}
    	
    	}
    Last edited by ceddsoboss; 10-06-2010 at 11:37 PM. Reason: convenience

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Why don't you ask us to predict the future while your at it? Or to contact your late uncle Roberts, maybe? Because we're totally psychic here, and can do that.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    14
    If you can help, fine; if not dont worry about it.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by msh View Post
    Why don't you ask us to predict the future while your at it? Or to contact your late uncle Roberts, maybe? Because we're totally psychic here, and can do that.
    In your if statements assigining points to (for example) age, you are using the || or operator.
    Try it again with the && and operator.

    If that doesn't get it... post your code again but this time mark where the problems are so we can take a closer look.

  5. #5
    Novice
    Join Date
    Jul 2009
    Posts
    568
    You could stop printing the address of total1 - 4.

    E.g.
    Code:
    printf( "%d\n", total1 );     // value of _total1_
    
    printf( "%d\n", &total1 );    // address of _total1_

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Printing an address (ie, a pointer) with %d is undefined behavior. Use %p and cast to void*.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by Elysia View Post
    Printing an address (ie, a pointer) with %d is undefined behavior. Use %p and cast to void*.
    He doesn't want the address.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Maybe not. But nevertheless, in your code, you used %d to print the address. I am merely pointing that out.
    Irrelevant, perhaps, but pedantic is good. No good to hand defect code to newbies. Correcting faulty code you see is, IMHO, a good thing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by Elysia View Post
    Maybe not. But nevertheless, in your code, you used %d to print the address. I am merely pointing that out.
    Irrelevant, perhaps, but pedantic is good. No good to hand defect code to newbies. Correcting faulty code you see is, IMHO, a good thing.
    In my code, I was illustrating what he was doing vs. what he should be doing.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You also failed to point out that it's wrong to print addresses with %d, so I added that missing point, in case the OP was not aware of this
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Fair enough.

  12. #12
    Registered User
    Join Date
    Oct 2010
    Posts
    14
    Ok, so the problem was with the redundancy of my relational operators and the ampersand before the total1-4 variables. Here is the revised code...

    Code:
    #include <stdio.h>
    
    int main ()
    
    	{
    	
    
    float age, crntAdrs, anlIncome, sameJob;      			// The four factors that will determine credit worthiness
    	
    char letter; 										    //  If applying for credit card, Y or N                       	
    
    float total1, total2, total3, total4;					// Variable for points issued
    
    int pointTotal;											// Total of total1-4
    
    	
    		printf("\n\nHello, you have chosen to apply for a credit card");
    	
    		printf("\nwith ACME Banking & Trust.");
    
    		printf("\n\nIf this is correct, press \"Y\" for Yes or \"N\" for No: ");
    		
    			scanf("%c", &letter);
    		
    		if (letter == 'Y' || letter == 'y') 
    		    
    			printf("\n\n\nGreat, let's continue!\n\n\n");		  // The consumer chose to apply
    		
    		
    		else {
    			printf("\n\nYou are free to exit the program.");	  // The consumer did not choose to apply
    			
    			return 0; 
    		}
    		
    		
    		
    		do {
    		
    		printf("\n\nPlease enter your age: ");					   
    		
    			scanf("%f", &age);
    	    
    		if ( age < 18.0 || age > 105.0 ) {                        // Input validation
    		 
    			
    			printf("\n\nYou have entered an age that is out of range.");
    			
    			printf("\n\nPlease enter an age in the range of 18 to 105 years.");
    		}	
    		
    		else {													  // Determine number of points based on age
    			
    			if ( age <= 20.0 ) 
    				total1 = -10.0;
    		
    			else if ( age <= 30.0 ) 
    				total1 = 0.0;
    		
    			else if ( age <= 50.0 ) 
    				total1 = 20.0;
    			
    			else if ( age > 50.0 ) 
    				total1 = 25.0;
    		}	
    		
    		} while ( age < 18.0 || age > 105.0);
    		 		
    	
    		
    		
    		do {
    		
    		printf("\n\nHow many years have you lived at your current address? ");
    		
    			scanf("%f", &crntAdrs);
    			
    			
    		if ( crntAdrs <= 0.0) {
    		
    			printf("\n\nThe amount of time you entered is out of range.");
    			
    			printf("\n\nEnter an amount of time greater than or equal to 1 year.");
    		}
    		
    		else {														// Determine number of points based on time at current address
    		
    			if ( crntAdrs < 1.0 )
    				total2 = -5.0;
    			
    			else if ( crntAdrs <= 3.0 )
    				total2 = 5.0;
    		
    			else if ( crntAdrs <= 8.0 )
    				total2 = 12.0;
    			
    			else if ( crntAdrs >= 9.0 )
    				total2 = 20.0;
    		}	
    			
    		} while ( crntAdrs <= 0.0);
    		
    		
    		
    		do {
    		
    		printf("\n\nWhat is your annual income? ");
    		
    			scanf("%f", &anlIncome);
    			
    			
    		if ( anlIncome <= 0.0 ) 
    		
    			printf("\n\nThe income amount you entered is out of range.");
    		
    		
    		else {														    // Determine number of points based on annual income
    		
    			if ( anlIncome < 15000.0 )
    				total3 = 0.0;
    			
    			else if ( anlIncome <= 25000.0 )
    				total3 = 12.0;
    			
    			else if ( anlIncome <= 40000.0 )
    				total3 = 24.0;
    			
    			else if ( anlIncome > 40000.0 )
    				total3 = 30.0;
    		}	
    		
    		} while ( anlIncome <=0.0);
    
    		
    		
            do {
    		
    		printf("\n\nHow long have you been employed? ");
    		
    			scanf("%f", &sameJob);
    			
    			
    		if ( sameJob <= 0.0 ) 
    		
    			printf("\n\nThe employment time frame you entered is out of range.");
    		
    		
    		else {														// Determine the number of points based on the length of employment
    		
    			if ( sameJob < 2.0 ) 
    				total4 = -4.0;
    		
    			else if ( sameJob <= 4.0 ) 
    				total4 = 8.0;
    			
    			else if ( sameJob > 4.0 )
    				total4 = 15.0;
    		}
    
    		} while ( sameJob <= 0.0 );
    		
    		
    		
    
    		pointTotal = ( total1 + total2 + total3 + total4 );			  // Total number of credibility points
    	
    
    	
    		if ( pointTotal == -19 || pointTotal <= 20 ) { 
    		
    			printf("\n\nYour point total for credit worthiness is %d.", pointTotal);
    			
    			printf("\n\nWe're sorry, at this time we are not able to offer you");
    			
    			printf("\ncredit with us.\n\n");
    		}
    		
    		else {
    		
    			if ( pointTotal == 21 || pointTotal <= 35 ) {
    		
    			printf("\n\nYour point total for credit worthiness is %d.", pointTotal);
    			
    			printf("\n\nCongratulations! You have been granted a $500 credit limit!\n\n");
    		
    		}
    			else if ( pointTotal == 36 || pointTotal <= 60 ) { 
    		
    			printf("\n\nYour point total for credit worthiness is %d.", pointTotal);
    		
    			printf("\n\nCongratulations! You have been granted a $2000 credit limit!\n\n");
    		
    		}
    			else if ( pointTotal == 61 || pointTotal <= 90 ) {
    		
    			printf("\n\nYour point total for credit worthiness is %d.", pointTotal);
    			
    			printf("\n\nCongratulations! You have been granted a $5000 credit limit!\n\n");
    		}	
    		}
    	
    	}

  13. #13
    Novice
    Join Date
    Jul 2009
    Posts
    568
    You need to rethink this bit.

    As it stands, you do not accept values equal to or less then 0 due to code marked in red. Consequentially, code marked in blue will never execute.
    Code:
        do {
            printf("\n\nHow many years have you lived at your current address? ");
            scanf("%d", &crntAdrs);
            
            if ( crntAdrs <= 0) {
                printf("\n\nThe amount of time you entered is out of range.");
                printf("\n\nEnter an amount of time greater than or equal to 1 year.");
            } else {
               if ( crntAdrs < 1 )                                   
                    total2 = -5;
                else if ( crntAdrs == 1 || crntAdrs <= 3 )srntAdrs <= 3)...
                    total2 = 5;
                else if ( crntAdrs == 4 || crntAdrs <= 8 )
                    total2 = 12;
                else if ( crntAdrs == 9 || crntAdrs >= 9 )
                    total2 = 20;
            }	
        } while ( crntAdrs <= 0);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Segmentation Fault before faulty code is ever reached?
    By yougene in forum C Programming
    Replies: 21
    Last Post: 01-12-2009, 06:44 PM
  3. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

Tags for this Thread