#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    11
    Rep Power
    0

    Talking Correct my program Please


    Well the only part of the program that isn't working is the part where I want it to "continue" when the character Y or y is typed in by the user asking them "Do you want to do anything else?"

    Please Help. Thanks.


    #include <stdio.h>

    int main()
    {
    char customer_name[30], address[30];
    int choice1, choice2, tel_num, counter = 1;
    float purchase_amt, total_sales = 0, credit_amt, total_credit = 0, paid, remaining_cred = 0;

    for(;;){
    printf("To choose option, type the appropriate number.\n");
    printf("Upfront Payments(1), Crediting Payments(2), Payoff Credits(3)\n");
    scanf("%d", &choice1);
    switch(choice1){
    case 1: printf("Enter Customer name and Purchase Amount\n");
    scanf("%s %f", customer_name, &purchase_amt);
    total_sales = total_sales + purchase_amt;
    break;

    case 2: printf("Enter Customer name, Credit Amount, Telephone Number and Address\n");
    scanf("%s %f %d %s", customer_name, &credit_amt, &tel_num, address);
    total_credit = total_credit + credit_amt;
    break;

    case 3: printf("Enter amount of money\n");
    scanf("%f", &paid);

    remaining_cred = total_credit - paid;
    total_sales = total_sales + paid;
    break;

    default: printf("Wrong number chosen\n");
    break;

    }
    getchar();

    printf("Do you want to do anything else? Y or N\n");
    scanf("%c", &choice2);
    if(choice2 == 'Y'|| choice2 == 'y'){
    continue;
    }
    else
    {
    break;
    }
    }

    printf("Total Sales %f \n", total_sales);
    printf("Total_credit %f\n", total_credit);
    printf("Remaining Credit\n%f", remaining_cred);

    getchar();
    getchar();
    return 0;
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,087
    Rep Power
    2222
    Use code tags to preserve your code's indenting. Without code tags, your code is an unreadable mess.

    Kill smilies!!! Look at your message. What's that smilie doing in there? The forum software is misinterpreting a character sequence in your code as being a smilie. In the advanced editor, there's a checkbox for "Disable smilies" or something similar. Always check that. We really do need an option in our user profiles to always disable smilies.

    Code:
                scanf("%c", &choice2);
    You're telling scanf to read in the next character, no matter what it is, including the newline that was left in the input buffer by the previous scanf. That is not what you want, obviously. Instead, you need to tell scanf to ignore any whitespace that may appear before the next printable character. You need to add a space before the percent sign, namely: " %c"
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    11
    Rep Power
    0
    Dwise, it didn't work. I tried what you said and it still just went on.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,087
    Rep Power
    2222
    Go back and edit your first message with code tags so we can see what that continue statement is supposed to be associated with. And disable smilies while you're at it.

    Or better, post your current formatted code with code tags so we can see what you actually did do. You need to show us that kind of stuff, you know.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    23
    Rep Power
    0
    Originally Posted by legendaryz
    Dwise, it didn't work. I tried what you said and it still just went on.
    Let me give you a head up. Check the code below and see how what you intended was done.
    Code:
    #include <stdio.h>
    
    int main(void){
    
      int i;
      char ans;
    
       for ( i = 1; i < 101; i++ ){
         printf("%d ", i);
         if( i % 10 == 0 ){
           printf("\nDo you want to continue?: ");
           scanf(" %c", &ans);
           if( ans == 'Y' || ans == 'y' )       
             continue;
           else
    	 break;
         } 
       }
       
      return 0;
    }
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    11
    Rep Power
    0

    With closing tags


    Code:
     
    
    #include <stdio.h>
    
    int main()
    {
    char customer_name[30], address[30];
    int choice1, choice2, tel_num, counter = 1;
    float purchase_amt, total_sales = 0, credit_amt, total_credit = 0, paid, remaining_cred = 0;
    
    for(;;){
    printf("To choose option, type the appropriate number.\n");
    printf("Upfront Payments(1), Crediting Payments(2), Payoff Credits(3)\n");
    scanf("%d", &choice1);
    	switch(choice1){
    		case 1: printf("Enter Customer name and Purchase Amount\n");
    			scanf("%s %f", customer_name, &purchase_amt);
    			total_sales = total_sales + purchase_amt;
    			break;
    
    		case 2: printf("Enter Customer name, Credit Amount, Telephone Number and Address\n");
    			scanf("%s %f %d %s", customer_name, &credit_amt, &tel_num, address);
    			total_credit = total_credit + credit_amt;
    			break;
    
    		case 3: printf("Enter amount of money\n");
    			scanf("%f", &paid);
    
    			remaining_cred = total_credit - paid;
    			total_sales = total_sales + paid;
    			break;
    
    			default: printf("Wrong number chosen\n");
    			break;
    
    			}
    			getchar();
    
    		printf("Do you want to do anything else? Y or N\n");
    		scanf(" %c", &choice2);
    			if(choice2 == 'Y'|| choice2 == 'y'){
    			continue;
    			}
    			else
    			{
    			break;
    			}
    		}
    
    	printf("Total Sales %f \n", total_sales);
    	printf("Total_credit %f\n", total_credit);
    	printf("Remaining Credit\n%f", remaining_cred);
    
    	getchar();
    	getchar();
    	return 0;
    }
    Oh 2teez. Your code looks similar but I want my code to continue from the for loop near to the beginning of the code.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    23
    Rep Power
    0
    Hi,
    Please check the correction I made to your code and comments.
    The code before works as intended.
    Code:
     
    
    #include <stdio.h>
    
    int main(){
    char customer_name[30], address[30]; 
    char choice2; /* make this char data type*/
    int choice1, tel_num; /* counter = 1; not used so remove */
    /* make all the float type equals to 0.0 not 0 
     But I would rather use double instead of float
     */
    float purchase_amt, total_sales = 0.0, credit_amt, total_credit = 0.0, paid, remaining_cred = 0.0;
    
    for(;;){ /* I would rather use while(1){..} here instead of for(;;){..}*/
    printf("To choose option, type the appropriate number.\n");
    printf("Upfront Payments(1), Crediting Payments(2), Payoff Credits(3)\n");
    scanf("%d", &choice1);
    	switch(choice1){
    		case 1: printf("Enter Customer name and Purchase Amount\n");
    			scanf("%s %f", customer_name, &purchase_amt);
    			total_sales = total_sales + purchase_amt;
    			break;
    
    		case 2: printf("Enter Customer name, Credit Amount, Telephone Number and Address\n");
    			scanf("%s %f %d %s", customer_name, &credit_amt, &tel_num, address);
    			total_credit = total_credit + credit_amt;
    			break;
    
    		case 3: printf("Enter amount of money\n");
    			scanf("%f", &paid);
    
    			remaining_cred = total_credit - paid;
    			total_sales = total_sales + paid;
    			break;
    
    			default: printf("Wrong number chosen\n");
    			break;
    
    	}
    			getchar();
    
    		printf("Do you want to do anything else? Y or N\n");
    		scanf(" %c", &choice2);
    			if(choice2 == 'Y'|| choice2 == 'y'){
    			continue;
    			}
    			else
    			{
    			break;
    			}
    }
    
    	printf("Total Sales %f \n", total_sales);
    	printf("Total_credit %f\n", total_credit);
    	printf("Remaining Credit\n%f", remaining_cred);
    
    	puts("\n");
    	return 0;
    }
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,087
    Rep Power
    2222
    Horrible formatting! The purpose of formatting is to make your code readable by revealing its structure. We cannot see your code's structure through the misapplied and completely inconsistent indenting. In addition to that, you are using the horrible indent style, K&R, which hides open braces at the ends and even in the middle of lines! K&R depends absolutely on consistent and correct indenting, which you are not providing!

    This is especially important, because the acceptable solution to your problem would require you to structure your program appropriately. But obviously you cannot do that if you cannot even see the structure of your program.

    Here is what your code looks like when properly formatted:
    Code:
    #include <stdio.h>
    
    int main()
    {
        char customer_name[30], address[30];
        int choice1, choice2, tel_num, counter = 1;
        float purchase_amt, total_sales = 0, credit_amt, total_credit = 0, paid, remaining_cred = 0;
    
        for(;;)
        {
            printf("To choose option, type the appropriate number.\n");
            printf("Upfront Payments(1), Crediting Payments(2), Payoff Credits(3)\n");
            scanf("%d", &choice1);
            switch(choice1)
            {
                case 1: 
                    printf("Enter Customer name and Purchase Amount\n");
                    scanf("%s %f", customer_name, &purchase_amt);
                    total_sales = total_sales + purchase_amt;
                    break;
    
                case 2: 
                    printf("Enter Customer name, Credit Amount, Telephone Number and Address\n");
                    scanf("%s %f %d %s", customer_name, &credit_amt, &tel_num, address);
                    total_credit = total_credit + credit_amt;
                    break;
    
                case 3: 
                    printf("Enter amount of money\n");
                    scanf("%f", &paid);
    
                    remaining_cred = total_credit - paid;
                    total_sales = total_sales + paid;
                    break;
    
                default: 
                    printf("Wrong number chosen\n");
                    break;
            }
    
            getchar();
    
            printf("Do you want to do anything else? Y or N\n");
            scanf(" %c", &choice2);
            if(choice2 == 'Y'|| choice2 == 'y')
            {
                continue;
            }
            else
            {
                break;
            }
        }
    
        printf("Total Sales %f \n", total_sales);
        printf("Total_credit %f\n", total_credit);
        printf("Remaining Credit\n%f", remaining_cred);
    
        getchar();
        getchar();
        return 0;
    }
    As far as I can see, both the continue and the break are already associated with the only loop that you have in your program. I have no idea where you think that it's taking you.

    For that matter, you don't even need that continue. There's no more code at the bottom of the loop, so the flow of control would naturally proceed to the top of the loop. The only action that if-statement needs to take is to detect when you want to break out of the loop. Lose the continue, keep the break, and reverse the polarity of the neutron flow ... er, I mean of the if statement's condition (ie, to if it's not 'Y' and it's also not 'y'; refer to De Morgan's Theorem if you need to).

    My question to you is: Why are you ignoring the warnings that your compiler is throwing at you? Don't you know that warnings are much more important than error message? Don't you realize that you must not even begin to contemplate thinking about running your program until you have cleaned up or at least accounted for all warnings? Don't they teach you kids anything anymore?

    Here is what I get when I compile your program (MinGW gcc on WinXP):
    C:TEST>gcc -Wall legend01.c
    legend01.c: In function `main':
    legend01.c:44: warning: char format, different type arg (arg 2)
    legend01.c:6: warning: unused variable `counter'

    C:TEST>
    Line 44 is: scanf(" %c", &choice2);
    choice2 is declared as an int, and yet you are treating it as a char. An int is 4 bytes long on a 32-bit system, whereas a char is only one byte long. That means that that scanf only writes one byte of data to choice2 while leaving the other three bytes of garbage unchanged. I added a printf after that scanf to echo out the value of choice2 and when I entered 'y' I got this:
    choice2 is 4072569
    There is no possible way that 4072569 could possibly be equal to 'y', which is 121.

    Declare choice2 to be char.

    And please note that the warning took us straight to the problem and told us what was wrong. This should serve as an object lesson for you: Never ignore warnings!
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    11
    Rep Power
    0

    Thanks


    Oh my my problem was indeed using choice2 as and integer rather than a character. And no Dwise1. They don't teach us this stuff like they are suppose to :mad: . I thank you for your harsh yet helpful advice. You too 2teez.

IMN logo majestic logo threadwatch logo seochat tools logo