jeudi 5 février 2015

Issues with using if/else statements correctly in C language

I am still very new to programming, so I am not sure the correct action to take. I can't seem to get the program to display a different option when selected by the user. Either the first option displays, or it shows the 'Invalid Entry' text. I am only going to include the problem code, since I've tested the rest without the if/else statements and it calculates and displays correctly.



printf("Select interest type: S, Y, M \n\n");
scanf("%ch", &type); /*program has finished calculating and is waiting on user input. Variable 'type' has already been initialized as a char*/

printf("\n\nIn %i years, at %.2f percent interest rate, \n", n, R);

/*this is where the problem starts*/
if (type == 'S')
printf("Your investment becomes %.2f dollars, with simple interest.\n", futureVal_simp);
else
{
if (type == 'Y')
printf("Your investment becomes %.2f dollars, with annual compounding interest.\n", futureVal_yr);
else
{
if (type == 'M')
printf("Your investment becomes %.2f dollars, with monthly compounding interest.\n\n\n", futureVal_mnth);
else printf("Invalid entry.\n\n\n"); /*these are supposed to display based on char entered*/
}
}


return 0;
}


I checked other questions on the site, but am still not sure. Should I be using != and && instead of multiple if/else?


Aucun commentaire:

Enregistrer un commentaire