0

When i run this code for selection 2 in the menu, after entering the trn number the program displays the investment type without the 1st letter and doesn't do the calculation assigned to it. Also for selection 3 the program runs continuously after selecting option 3 in the menu

    switch(option)
    {
    case 1:
        system("cls");
        printf(" How many clients are to be signed up ? \n");
            scanf(" %d",&num);
            system("cls");
            for(i=0;i<num;i++)
            {
                printf("\n Investment Type Available");
                printf("\n 1. Bonds 2. Stocks   3. MutualFund");
                printf("\n ");
                printf(" Enter the clients data \n");
                printf(" TRN              : ");
                scanf(" %s",&trn[i]);
                printf("\n Investment Type  : ");
                scanf(" %s",&p_type[i]);
                printf("\n Amount Investing : ");
                scanf(" %lf",&i_amount[i]);
                system("cls");
                t_amount=t_amount+i_amount[i];
            }
            getch();
            goto Menu;
        break;
    case 2:
        printf("\n Enter clients TRN : ");
        scanf("%s",&trn[i]);
        if(strcmp(trn[i],trn[i])==0)
        {
            printf("\n Investment type for this client : %s",&p_type[i]);

        }
        else
        {
            printf("No record found for this client");
            getch();
            return 0;
        }
        printf("\n TRN                     : %s",&trn[i]);
        printf("\n Investment Type         : %s",&p_type[i]);
        printf("\n Interest to be received  : $%0.2lf",s_interest);
        getch();
        goto Menu;
        break;
    case 3:
        printf("Each clients TRN & Investment Type :");
        for(i=0;i<num;i++)
        {
            if(i=num)
            {
                break;
            }
            else
            {
               printf("\n TRN : %s   Investment Type : %s",&trn[i],&p_type[i]);
            }
            if(lowest<i_amount[i])
            {
                lowest=i_amount[i];
            }
        }
        a_amount=t_amount/num;
        printf("The average amount that could be invested per person is : $%0.2lf",&a_amount);
        printf("The client with the least amount to invest ");
        break;
    case 4:
        printf("Exiting system");
        getch();
        return 0;
        break;
    default :
        printf("Invalid selection");
        getch();
        return 0;
    }
}

return 0;
getch();

}

7
  • Reduce your code to the minimum required to demonstrate your problem. Also note that doing getch() or break after return has no effect. Commented Dec 11, 2015 at 2:28
  • maybe char trn[10] --> char trn[10][max string length + 1] Commented Dec 11, 2015 at 2:49
  • 3
    Your code is littered with problems. 1. scanf(" %s",&trn[i]); the second parameter is not a valid buffer for storing a string. It's a pointer to some part of the array but pretty sure that's not what you want. Same for almost all the other places you have &trn[i]. 2. strcmp(trn[i],trn[i]) why comparing something to itself? And in any case trn[i] is a single char not a string buffer. Commented Dec 11, 2015 at 2:50
  • bare with me i am still new to this @kaylum Commented Dec 11, 2015 at 2:56
  • I am bearing with you. That's why I'm pointing out your mistakes so that you can fix them. But why did you remove some of the code so that it is no longer complete? As for option 3 this if(i=num) should be if(i==num). But in fact, there is no need for that conditional block at all. The for loop already has that built into it. At the start of each for iteration the check i<num is performed and at the end of each for loop iteration i++ is run. Commented Dec 11, 2015 at 3:02

1 Answer 1

1

The problem is you are trying to use scanf() for reading input from the keyboard. That way lies madness. Change all scanf() calls to fgets() and correct the necessary.

Especially the horror of mixing scanf() with getch(). That's undefined due to how buffering works.

Sign up to request clarification or add additional context in comments.

1 Comment

He is right. Although using getch() is a common beginner technique to control program flow execution, many don't realize it is actually reading some characters and may cause some undefined behaviors. Since you are missing a character, I would definitely consider this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.