Pregunta

Make and run a program that will compute the total cost charges of a telephone call rendered from a particular network using the given table. The user will input call destination code (1 to 5), time code (1=off-peak, 2=peak), and the duration of call.

  Off-peak Hours                Rate/min
  1 Calls to the same network     P 8
  2 Call to other network         P 8
  3 Call to a landline            P 8
  4 National direct dial          P 11
  5 International direct dial     P 20

  Peak hours                     Rate/min
  1 Calls to the same network     P 3
  2 Call to other network         P 4
  3 Call to a landline            P 4
  4 National direct dial          P 7
  5 International direct dial     P 20

*the program runs but all conditions follow eq1 even if i told it to print eq2,eq3,eq4,eq5 or eq6... meaning every number i input just multiply the duc by 8.

---------------------------------------...

#include<stdio.h>
#include<conio.h>

main()
{
int dc, tc, duc, eq1, eq2, eq3, eq4, eq5, eq6;

printf("Time Code:");
scanf("%d", &tc);
printf("Destination Code:");
scanf("%d", &dc);
printf("Duration Call:");
scanf("%d", &duc);

eq1=duc*8;
eq2=duc*11;
eq3=duc*3;
eq4=duc*4;
eq5=duc*7;
eq6=duc*20;

{
if((tc=1) && (dc=1))
printf("%d", eq1);
else if((tc=1) && (dc=2))
printf("%d", eq1);
else if((tc=1) && (dc=3))
printf("%d", eq1);
else if((tc=1) && (dc=4))
printf("%d", eq2);
else if((tc=1) && (dc=5))
printf("%d", eq6);
else if((tc=2) && (dc=1))
printf("%d", eq3);
else if((tc=2) && (dc=2))
printf("%d", eq4);
else if((tc=2) && (dc=3))
printf("%d", eq4);
else if((tc=2) && (dc=4))
printf("%d", eq5);
else if((tc=2) && (dc=5))
printf("%d", eq6);
}

getch();
}
¿Fue útil?

Solución 2

= is the assignment operator
== is the equality operator

if(tc=1) is the same as if(1), so will always be true regardless of the tc value (even if it is not equal to 1).

Make it your habbit to speak the = operator as assigned to variable (like tc gets assiged the value of 1). While the == could be read as equals to, or equal equal. It will also help when you explain code to someone else.

Otros consejos

if((tc=1) && (dc=1))

All the conditions in the if and else if uses the assignment operator =, while you should use == to test equality.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top