using an if inside a for loop

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2008
Posts: 60
Reputation: gispe is an unknown quantity at this point 
Solved Threads: 0
gispe gispe is offline Offline
Junior Poster in Training

using an if inside a for loop

 
0
  #1
Jul 23rd, 2008
hi ppl!
im here again with another problem, which, is more a question about if what im doing is ok.

the thing is i have to do a problem that counts the total of letters sent, the total of packages sent, total of letters and packages sent on the next day priority, on the next day normal, and the total if its sent over 2 days.
also i have to calculate a big total of letters and packages sent (i mean letters + packages in each day) and the percentage of the total of things sent on the next day priority, of the next day normal, and over 2 days.

i already have a code made, my question if its ok to put a for loop in the middle of a chain of ifs:

  1. //A GENERAL FOR LOOP WOULD BE HERE
  2. if ((peso < 30) && (dia = 7))
  3. cout << " Se le cobraran 8$ " << "\n"; // these 3 are letters,
  4. else if ((peso < 30) && (dia = 8))
  5. cout << " Se le cobraran 5$ " << "\n";
  6. else if ((peso < 30) && (dia = 9))
  7. cout << " No esta disponible$ " << "\n";
  8. //ANOTHER PUT A FOR LOOP HERE
  9. else if ((peso > 30) && (peso < 1000) && (dia = 7)) // from here to the end, are packages
  10. cout << " Se le cobraran 25$ " << "\n";
  11. else if ((peso > 30) && (peso < 1000) && (dia = 8))
  12. cout << " Se le cobraran 20$ " << "\n";
  13. else if ((peso > 30) && (peso < 1000) && (dia = 9))
  14. cout << " Se le cobraran 15$ " << "\n";
  15. //AND ANOTHER ONE HERE
  16. else if((peso > 1000) && (dia = 7))
  17. {
  18. cout << " Se le cobraran 25$ " << "\n";
  19. cout << " Y 2$ adicionales por cada 500 gr de màs" << "\n";
  20. diferencia = ((peso-1000) / 500);
  21. total_a_pagar = (25 + ((diferencia *2) + 2));
  22. cout << " Se le cobraran" << total_a_pagar << " $ en total" << "\n";
  23. }
  24. else if ((dia = 8) && (peso > 1000))
  25. {
  26. cout << " Se le cobraran 20$ " << "\n";
  27. cout << " Y 1.5$ adicionales por cada 500 gr de màs" << "\n";
  28. diferencia = ((peso-1000) / 500);
  29. total_a_pagar = (25 + ((diferencia *2)+2));
  30. cout << " Se le cobraran" << total_a_pagar << " $ en total" << "\n";
  31. }
  32. else if ((dia = 9)&& (peso > 1000))
  33. {
  34. cout << " Se le cobraran 15$ " << "\n";
  35. cout << " Y 1$ adicionales por cada 500 gr de màs" << "\n";
  36. diferencia = ((peso-1000) / 500);
  37. total_a_pagar = (25 + ((diferencia *2)+2));
  38. cout << " Se le cobraran" << total_a_pagar << " $ en total" << "\n";
  39. }

what i want to do is this: the program must consider the hours in which the people work, so the first for loop would control that [for (hour = hour_begining; (hour > hour_begining) && (hour < hour_final); hour++) ]. now, when it comes to the other two for loops, i dont know if they will stop in any way the ifs, or do i have to write the code differently.

in the code:
peso = weight
dia = day
total_a_pagar = is what i have to pay if i send just one (its calculated only for the packages, if its a letter doesnt matter the weight, its a fixed price)



thanks for the help!!
gispe!!

ps: if anything isnt clear, ask me! and if whole code were necessary ill paste it!!
Last edited by Tekmaven; Jul 24th, 2008 at 3:05 am. Reason: Code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: using an if inside a for loop

 
0
  #2
Jul 23rd, 2008
If we asked for more code, would you still post it without [code][/code] tags?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 60
Reputation: gispe is an unknown quantity at this point 
Solved Threads: 0
gispe gispe is offline Offline
Junior Poster in Training

Re: using an if inside a for loop

 
-1
  #3
Jul 23rd, 2008
here is the code



  1. // caja_paquete_2.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6.  
  7.  
  8. int main(int argc, char* argv[])
  9. {
  10. using namespace std;
  11.  
  12. char tipo_servicio, dia;
  13.  
  14. float peso, total_a_pagar = 0, diferencia; // CALCULA PRECIO UNITARIO DE CADA COSA
  15. int total_paquete = 0, total_carta = 0; // TOTALES POR TIPO ENTREGA, incializadas
  16. int total_dia_sig_prioritario = 0, total_dia_sig_normal = 0, total_2_dias_o_mas = 0; // TOTALES POR DIA, ya inicializadas
  17. int totales_de_entregas = 0; // PARA QUE CALCULE EL TOTAL DE ENTREGAS, ya inicializadas
  18. float porcentaje_dia_sig_prioritario, porcentaje_dia_siguiente_normal, porcentaje_dos_dias_o_mas; // PORCENTAJES
  19. float dinero_recaudado = 0; // TOTAL DINERO RECAUDADO, ya inicializada
  20. float hora, hora_fin, hora_inicio; // HORA DEL DIA PARA CARGAR LAS COSAS
  21.  
  22.  
  23.  
  24. cout << "Ingrese hora a la que empieza a trabajar (hh.mm): ";
  25. cin >> hora_inicio;
  26. cout << "\n";
  27.  
  28. cout << " Ingrese la hora a la que termina de trabajar (hh.mm): ";
  29. cin >> hora_fin;
  30. cout << "\n";
  31.  
  32. for (hora = hora_inicio; hora < hora_fin; hora++)
  33. {
  34. cout << " Ingrese tipo de servicio:" << "\n"; ////////
  35. cout << " 1 - Carta" << "\n"; ////////
  36. cout << " 2 - Paquete" << "\n"; //
  37. cin >> tipo_servicio; //
  38. //
  39. cout << " Ingrese tipo de entrega: " << "\n"; // primer ingreso
  40. cout << " 7 - Dia siguiente prioritario" << "\n"; // de datos
  41. cout << " 8 - Dia siguiente normal" << "\n"; //
  42. cout << " 9 - Dos dias o mas" << "\n"; //
  43. cin >> dia; //
  44. //
  45. cout << " Ingrese peso EN GRAMOS a enviar: "; ////////
  46. cin >> peso; ////////
  47. cout << "\n";
  48.  
  49.  
  50. if ((peso < 30) && (dia = 7)) //
  51. cout << " Se le cobraran 8$ " << "\n"; //
  52. else if ((peso < 30) && (dia = 8)) //
  53. cout << " Se le cobraran 5$ " << "\n";
  54. else if ((peso < 30) && (dia = 9))
  55. cout << " No esta disponible$ " << "\n";
  56. else if ((peso > 30) && (peso < 1000) && (dia = 7))
  57. cout << " Se le cobraran 25$ " << "\n";
  58. else if ((peso > 30) && (peso < 1000) && (dia = 8))
  59. cout << " Se le cobraran 20$ " << "\n";
  60. else if ((peso > 30) && (peso < 1000) && (dia = 9))
  61. cout << " Se le cobraran 15$ " << "\n";
  62. else if((peso > 1000) && (dia = 7))
  63. {
  64. cout << " Se le cobraran 25$ " << "\n";
  65. cout << " Y 2$ adicionales por cada 500 gr de màs" << "\n";
  66. diferencia = ((peso-1000) / 500);
  67. total_a_pagar = (25 + ((diferencia *2) + 2));
  68. cout << " Se le cobraran" << total_a_pagar << " $ en total" << "\n";
  69. }
  70. else if ((dia = 8) && (peso > 1000))
  71. {
  72. cout << " Se le cobraran 20$ " << "\n";
  73. cout << " Y 1.5$ adicionales por cada 500 gr de màs" << "\n";
  74. diferencia = ((peso-1000) / 500);
  75. total_a_pagar = (25 + ((diferencia *2)+2));
  76. cout << " Se le cobraran" << total_a_pagar << " $ en total" << "\n";
  77. }
  78. else if ((dia = 9)&& (peso > 1000))
  79. {
  80. cout << " Se le cobraran 15$ " << "\n";
  81. cout << " Y 1$ adicionales por cada 500 gr de màs" << "\n";
  82. diferencia = ((peso-1000) / 500);
  83. total_a_pagar = (25 + ((diferencia *2)+2));
  84. cout << " Se le cobraran" << total_a_pagar << " $ en total" << "\n";
  85. }
  86. }
  87.  
  88. system ("PAUSE");
  89.  
  90.  
  91. return 0;
  92. }
Last edited by Tekmaven; Jul 24th, 2008 at 3:07 am. Reason: Code tags
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

Re: using an if inside a for loop

 
0
  #4
Jul 23rd, 2008
Salem meant you should wrap your code in code tags (the # button on the top, or just type [ code] YOUR CODE HERE [/code], w/o the spaces between '[' and "code").

Do you want the for loop to be part of this conditional:
  1. else if ((peso > 30) && (peso < 1000) && (dia = 9))
  2. cout << " Se le cobraran 15$ " << "\n";
  3. //for loop only executed if peso is between 30 and 1000 and dia is 9

If so, just wrap the code in curly brackets:
  1. else if ((peso > 30) && (peso < 1000) && (dia = 9))
  2. {
  3. cout << " Se le cobraran 15$ " << "\n";
  4. for(;;)
  5. //your loop here
  6. }
  7. //next else if condition

If you meant that the loop is executed regardless of the above conditional, you can also do that, though the else if after it would need to become a regular if:
  1. /*else*/if((peso > 1000) && (dia = 7))
Last edited by CoolGamer48; Jul 23rd, 2008 at 6:39 pm.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 60
Reputation: gispe is an unknown quantity at this point 
Solved Threads: 0
gispe gispe is offline Offline
Junior Poster in Training

Re: using an if inside a for loop

 
0
  #5
Jul 23rd, 2008
so, u mean that i should put the for inside the if, n wont modify the ifs instructions?

cuz i have to count the letters sent every day, and then sum it up
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 19
Reputation: Epic Tissue is an unknown quantity at this point 
Solved Threads: 1
Epic Tissue's Avatar
Epic Tissue Epic Tissue is offline Offline
Newbie Poster

Re: using an if inside a for loop

 
0
  #6
Jul 23rd, 2008
> my question if its ok to put a for loop in the middle of a chain of ifs

Yes it's ok. Try put it in and see what happens. If it messes up it's the logic that's wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: using an if inside a for loop

 
2
  #7
Jul 23rd, 2008
Here's the code in code-tags.

  1. // caja_paquete_2.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6.  
  7.  
  8. int main(int argc, char* argv[])
  9. {
  10. using namespace std;
  11.  
  12. char tipo_servicio, dia;
  13.  
  14. float peso, total_a_pagar = 0, diferencia; // CALCULA PRECIO UNITARIO DE CADA COSA
  15. int total_paquete = 0, total_carta = 0; // TOTALES POR TIPO ENTREGA, incializadas
  16. int total_dia_sig_prioritario = 0, total_dia_sig_normal = 0, total_2_dias_o_mas = 0; // TOTALES POR DIA, ya inicializadas
  17. int totales_de_entregas = 0; // PARA QUE CALCULE EL TOTAL DE ENTREGAS, ya inicializadas
  18. float porcentaje_dia_sig_prioritario, porcentaje_dia_siguiente_normal, porcentaje_dos_dias_o_mas; // PORCENTAJES
  19. float dinero_recaudado = 0; // TOTAL DINERO RECAUDADO, ya inicializada
  20. float hora, hora_fin, hora_inicio; // HORA DEL DIA PARA CARGAR LAS COSAS
  21.  
  22. cout << "Ingrese hora a la que empieza a trabajar (hh.mm): ";
  23. cin >> hora_inicio;
  24. cout << "\n";
  25.  
  26. cout << " Ingrese la hora a la que termina de trabajar (hh.mm): ";
  27. cin >> hora_fin;
  28. cout << "\n";
  29.  
  30. for (hora = hora_inicio; hora < hora_fin; hora++)
  31. {
  32. cout << " Ingrese tipo de servicio:" << "\n"; ////////
  33. cout << " 1 - Carta" << "\n"; ////////
  34. cout << " 2 - Paquete" << "\n"; //
  35. cin >> tipo_servicio; //
  36. //
  37. cout << " Ingrese tipo de entrega: " << "\n"; // primer ingreso
  38. cout << " 7 - Dia siguiente prioritario" << "\n"; // de datos
  39. cout << " 8 - Dia siguiente normal" << "\n"; //
  40. cout << " 9 - Dos dias o mas" << "\n"; //
  41. cin >> dia; //
  42. //
  43. cout << " Ingrese peso EN GRAMOS a enviar: "; ////////
  44. cin >> peso; ////////
  45. cout << "\n";
  46.  
  47. if ((peso < 30) && (dia = 7)) //
  48. cout << " Se le cobraran 8$ " << "\n"; //
  49. else if ((peso < 30) && (dia = 8)) //
  50. cout << " Se le cobraran 5$ " << "\n";
  51. else if ((peso < 30) && (dia = 9))
  52. cout << " No esta disponible$ " << "\n";
  53. else if ((peso > 30) && (peso < 1000) && (dia = 7))
  54. cout << " Se le cobraran 25$ " << "\n";
  55. else if ((peso > 30) && (peso < 1000) && (dia = 8))
  56. cout << " Se le cobraran 20$ " << "\n";
  57. else if ((peso > 30) && (peso < 1000) && (dia = 9))
  58. cout << " Se le cobraran 15$ " << "\n";
  59. else if((peso > 1000) && (dia = 7))
  60. {
  61. cout << " Se le cobraran 25$ " << "\n";
  62. cout << " Y 2$ adicionales por cada 500 gr de màs" << "\n";
  63. diferencia = ((peso-1000) / 500);
  64. total_a_pagar = (25 + ((diferencia *2) + 2));
  65. cout << " Se le cobraran" << total_a_pagar << " $ en total" << "\n";
  66. }
  67. else if ((dia = 8) && (peso > 1000))
  68. {
  69. cout << " Se le cobraran 20$ " << "\n";
  70. cout << " Y 1.5$ adicionales por cada 500 gr de màs" << "\n";
  71. diferencia = ((peso-1000) / 500);
  72. total_a_pagar = (25 + ((diferencia *2)+2));
  73. cout << " Se le cobraran" << total_a_pagar << " $ en total" << "\n";
  74. }
  75. else if ((dia = 9)&& (peso > 1000))
  76. {
  77. cout << " Se le cobraran 15$ " << "\n";
  78. cout << " Y 1$ adicionales por cada 500 gr de màs" << "\n";
  79. diferencia = ((peso-1000) / 500);
  80. total_a_pagar = (25 + ((diferencia *2)+2));
  81. cout << " Se le cobraran" << total_a_pagar << " $ en total" << "\n";
  82. }
  83. }
  84.  
  85. system ("PAUSE");
  86.  
  87.  
  88. return 0;
  89. }

Sorry for "bumping" this thread, but it really gave me a headache to see the code like this and I'm sure it's giving other people a headache too.

Use code tags please! People will be more inclined to help when you do.
Last edited by Alex Edwards; Jul 23rd, 2008 at 8:07 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,832
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: using an if inside a for loop

 
0
  #8
Jul 23rd, 2008
Originally Posted by gispe View Post
so, u mean that i should put the for inside the if, n wont modify the ifs instructions?

cuz i have to count the letters sent every day, and then sum it up

Please don't use leet-speak. He's asking whether the for-loop should always be executed, regardless of whether the if-condition is true. If it should always be executed, then don't put the for-loop inside the if-statement. If the for-loop should only be executed if the the if-condition is true, then put the for-loop inside the if-statement's brackets.

I don't understand what you mean by this sentence, the red part in particular.

so, u mean that i should put the for inside the if, n wont modify the ifs instructions?
Do you mean "execute" rather than "modify"? The machine code instructions can't be modified when you run the program.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 60
Reputation: gispe is an unknown quantity at this point 
Solved Threads: 0
gispe gispe is offline Offline
Junior Poster in Training

Re: using an if inside a for loop

 
0
  #9
Jul 23rd, 2008
well, im back here: same program.
i made some modifications n now runs, but the thing is that it puts on screen same message indefinitely.

it reads the hours it worked, what i want to send (package or letter), and the day i want to send it. n then it returns the cost (indefinitely).

there is the code attached, if u want to try it


#
  1.  
  2. // cartas_paquetes_v3.cpp : Defines the entry point for the console application.
  3. //
  4.  
  5. #include "stdafx.h"
  6. #include <iostream>
  7.  
  8.  
  9. int _tmain(int argc, _TCHAR* argv[])
  10. {
  11. using namespace std;
  12.  
  13. char dia, tipo_servicio, horario_trabajo;
  14.  
  15. float peso, total_a_pagar = 0, diferencia; // CALCULA PRECIO UNITARIO DE CADA COSA
  16.  
  17. int total_paquete = 0, total_carta = 0; // TOTALES POR TIPO ENTREGA, incializadas
  18.  
  19. int total_dia_sig_prioritario = 0, total_dia_sig_normal = 0, total_2_dias_o_mas = 0; // TOTALES POR DIA, ya inicializadas
  20. int total_dia_siguiente_prioritario_p = 0, total_dia_siguiente_prioritario_c = 0; // TOTALES SIG. PRIO C Y P , inicializadas
  21. int total_dia_siguiente_prioritario_p1 = 0, total_dia_siguiente_normal_p1 = 0, total_dos_dias_o_mas_p1 = 0; // para paquetes mayores a 1000, inicializadas
  22. int total_dia_siguiente_normal_p = 0, total_dia_siguiente_normal_c = 0; // TOTAL DIA SIG NORMAL, inicializadas
  23. int total_dos_dias_o_mas_p = 0; // TOTAL DOS DIAS O MAS, inicializadas
  24.  
  25. int totales_de_entregas = 0; // PARA QUE CALCULE EL TOTAL DE ENTREGAS, ya inicializadas
  26.  
  27. float porcentaje_dia_sig_prioritario, porcentaje_dia_siguiente_normal, porcentaje_dos_dias_o_mas; // PORCENTAJES
  28. float dinero_recaudado = 0; // TOTAL DINERO RECAUDADO, ya inicializada
  29. float hora = 0, hora_fin, hora_inicio; // HORA DEL DIA PARA CARGAR LAS COSAS
  30.  
  31. int tp, tc; // CONTADORES tp, para paquete tc, para carta
  32.  
  33. cout << "Ingrese hora a la que empieza a trabajar (hh.mm): ";
  34. cin >> hora_inicio; cout << "\n";
  35. cout << " Ingrese la hora a la que termina de trabajar (hh.mm): ";
  36. cin >> hora_fin; cout << "\n";
  37.  
  38. for (hora = hora_inicio; hora < hora_fin; hora++)
  39. {
  40. cout << " Ingrese tipo de servicio:" << "\n";
  41. cout << " 1 - Carta" << "\n";
  42. cout << " 2 - Paquete" << "\n";
  43. cin >> tipo_servicio;
  44. cout << " Ingrese tipo de entrega: " << "\n";
  45. cout << " 7 - Dia siguiente prioritario" << "\n";
  46. cout << " 8 - Dia siguiente normal" << "\n";
  47. cout << " 9 - Dos dias o mas" << "\n";
  48. cin >> dia;
  49. cout << " Ingrese peso EN GRAMOS a enviar: ";
  50. cin >> peso;
  51. cout << "\n";
  52.  
  53. for (tc = 1; tipo_servicio = 1; tc++)
  54. {
  55. if (dia = 7)
  56. {
  57. cout << " Se le cobraran 8$ " << "\n";
  58. total_dia_siguiente_prioritario_c += 1;
  59. }
  60. else if (dia = 8)
  61. {
  62. cout << " Se le cobraran 5$ " << "\n";
  63. total_dia_siguiente_normal_c += 1;
  64. }
  65. else if (dia = 9)
  66. cout << " No esta disponible$ " << "\n";
  67. }
  68. for (tp = 1; tipo_servicio = 2; tp++)
  69. {
  70. if ((peso > 30) && (peso < 1000) && (dia = 7))
  71. {
  72. cout << " Se le cobraran 25$ " << "\n";
  73. total_dia_siguiente_prioritario_p += 1;
  74. }
  75. else if ((peso > 30) && (peso < 1000) && (dia = 8))
  76. {
  77. cout << " Se le cobraran 20$ " << "\n";
  78. total_dia_siguiente_normal_p += 1;
  79. }
  80. else if ((peso > 30) && (peso < 1000) && (dia = 9))
  81. {
  82. cout << " Se le cobraran 15$ " << "\n";
  83. total_dos_dias_o_mas_p += 1;
  84. }
  85. else if((peso > 1000) && (dia = 7))
  86. {
  87. cout << " Se le cobraran 25$ " << "\n";
  88. cout << " Y 2$ adicionales por cada 500 gr de màs" << "\n";
  89. diferencia = ((peso-1000) / 500);
  90. total_a_pagar = (25 + ((diferencia *2) + 2));
  91. cout << " Se le cobraran" << total_a_pagar << " $ en total" << "\n";
  92. total_dia_siguiente_prioritario_p1 += 1;
  93. }
  94. else if ((dia = 8) && (peso > 1000))
  95. {
  96. cout << " Se le cobraran 20$ " << "\n";
  97. cout << " Y 1.5$ adicionales por cada 500 gr de màs" << "\n";
  98. diferencia = ((peso-1000) / 500);
  99. total_a_pagar = (25 + ((diferencia *2)+2));
  100. cout << " Se le cobraran" << total_a_pagar << " $ en total" << "\n";
  101. total_dia_siguiente_normal_p1 += 1;
  102. }
  103. else if ((dia = 9)&& (peso > 1000))
  104. {
  105. cout << " Se le cobraran 15$ " << "\n";
  106. cout << " Y 1$ adicionales por cada 500 gr de màs" << "\n";
  107. diferencia = ((peso-1000) / 500);
  108. total_a_pagar = (25 + ((diferencia *2)+2));
  109. cout << " Se le cobraran" << total_a_pagar << " $ en total" << "\n";
  110. total_dos_dias_o_mas_p1 += 1;
  111. }
  112. }
  113. }
  114.  
  115.  
  116. total_paquete = total_dia_siguiente_normal_p + total_dos_dias_o_mas_p + total_dia_siguiente_prioritario_p; // TOTAL PAQUETE
  117. total_carta = total_dia_siguiente_prioritario_c + total_dia_siguiente_normal_c; //TOTAL CARTAS
  118.  
  119. total_dia_sig_prioritario = total_dia_siguiente_prioritario_p + total_dia_siguiente_prioritario_c; //TOTAL DIA SIGUIENTE PRIORITARIO
  120. total_dia_sig_normal = total_dia_siguiente_normal_p + total_dia_siguiente_normal_c; // TOTAL DIA SIGUIENTE NORMAL
  121. total_2_dias_o_mas = total_dos_dias_o_mas_p; // TOTAL DOS DIAS O MAS
  122.  
  123. totales_de_entregas = total_paquete + total_carta; //TOTAL DINERO RECAUDADO
  124. // dinero_recaudado = ;
  125.  
  126. system ("PAUSE");
  127. return 0;
  128.  
  129. }
Last edited by Tekmaven; Jul 24th, 2008 at 3:08 am. Reason: Code tags
Attached Files
File Type: cpp cartas_paquetes_v3.cpp (5.3 KB, 1 views)
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 60
Reputation: gispe is an unknown quantity at this point 
Solved Threads: 0
gispe gispe is offline Offline
Junior Poster in Training

Re: using an if inside a for loop

 
0
  #10
Jul 23rd, 2008
  1.  
  2. // cartas_paquetes_v3.cpp : Defines the entry point for the console application.
  3. //
  4.  
  5. #include "stdafx.h"
  6. #include <iostream>
  7.  
  8.  
  9. int _tmain(int argc, _TCHAR* argv[])
  10. {
  11. using namespace std;
  12.  
  13. char dia, tipo_servicio, horario_trabajo;
  14.  
  15. float peso, total_a_pagar = 0, diferencia; // CALCULA PRECIO UNITARIO DE CADA COSA
  16.  
  17. int total_paquete = 0, total_carta = 0; // TOTALES POR TIPO ENTREGA, incializadas
  18.  
  19. int total_dia_sig_prioritario = 0, total_dia_sig_normal = 0, total_2_dias_o_mas = 0; // TOTALES POR DIA, ya inicializadas
  20. int total_dia_siguiente_prioritario_p = 0, total_dia_siguiente_prioritario_c = 0; // TOTALES SIG. PRIO C Y P , inicializadas
  21. int total_dia_siguiente_prioritario_p1 = 0, total_dia_siguiente_normal_p1 = 0, total_dos_dias_o_mas_p1 = 0; // para paquetes mayores a 1000, inicializadas
  22. int total_dia_siguiente_normal_p = 0, total_dia_siguiente_normal_c = 0; // TOTAL DIA SIG NORMAL, inicializadas
  23. int total_dos_dias_o_mas_p = 0; // TOTAL DOS DIAS O MAS, inicializadas
  24.  
  25. int totales_de_entregas = 0; // PARA QUE CALCULE EL TOTAL DE ENTREGAS, ya inicializadas
  26.  
  27. float porcentaje_dia_sig_prioritario, porcentaje_dia_siguiente_normal, porcentaje_dos_dias_o_mas; // PORCENTAJES
  28. float dinero_recaudado = 0; // TOTAL DINERO RECAUDADO, ya inicializada
  29. float hora = 0, hora_fin, hora_inicio; // HORA DEL DIA PARA CARGAR LAS COSAS
  30.  
  31. int tp, tc; // CONTADORES tp, para paquete tc, para carta
  32.  
  33. cout << "Ingrese hora a la que empieza a trabajar (hh.mm): ";
  34. cin >> hora_inicio; cout << "\n";
  35. cout << " Ingrese la hora a la que termina de trabajar (hh.mm): ";
  36. cin >> hora_fin; cout << "\n";
  37.  
  38. for (hora = hora_inicio; hora < hora_fin; hora++)
  39. {
  40. cout << " Ingrese tipo de servicio:" << "\n";
  41. cout << " 1 - Carta" << "\n";
  42. cout << " 2 - Paquete" << "\n";
  43. cin >> tipo_servicio;
  44. cout << " Ingrese tipo de entrega: " << "\n";
  45. cout << " 7 - Dia siguiente prioritario" << "\n";
  46. cout << " 8 - Dia siguiente normal" << "\n";
  47. cout << " 9 - Dos dias o mas" << "\n";
  48. cin >> dia;
  49. cout << " Ingrese peso EN GRAMOS a enviar: ";
  50. cin >> peso;
  51. cout << "\n";
  52.  
  53. for (tc = 1; tipo_servicio = 1; tc++)
  54. {
  55. if (dia = 7)
  56. {
  57. cout << " Se le cobraran 8$ " << "\n";
  58. total_dia_siguiente_prioritario_c += 1;
  59. }
  60. else if (dia = 8)
  61. {
  62. cout << " Se le cobraran 5$ " << "\n";
  63. total_dia_siguiente_normal_c += 1;
  64. }
  65. else if (dia = 9)
  66. cout << " No esta disponible$ " << "\n";
  67. }
  68. for (tp = 1; tipo_servicio = 2; tp++)
  69. {
  70. if ((peso > 30) && (peso < 1000) && (dia = 7))
  71. {
  72. cout << " Se le cobraran 25$ " << "\n";
  73. total_dia_siguiente_prioritario_p += 1;
  74. }
  75. else if ((peso > 30) && (peso < 1000) && (dia = 8))
  76. {
  77. cout << " Se le cobraran 20$ " << "\n";
  78. total_dia_siguiente_normal_p += 1;
  79. }
  80. else if ((peso > 30) && (peso < 1000) && (dia = 9))
  81. {
  82. cout << " Se le cobraran 15$ " << "\n";
  83. total_dos_dias_o_mas_p += 1;
  84. }
  85. else if((peso > 1000) && (dia = 7))
  86. {
  87. cout << " Se le cobraran 25$ " << "\n";
  88. cout << " Y 2$ adicionales por cada 500 gr de màs" << "\n";
  89. diferencia = ((peso-1000) / 500);
  90. total_a_pagar = (25 + ((diferencia *2) + 2));
  91. cout << " Se le cobraran" << total_a_pagar << " $ en total" << "\n";
  92. total_dia_siguiente_prioritario_p1 += 1;
  93. }
  94. else if ((dia = 8) && (peso > 1000))
  95. {
  96. cout << " Se le cobraran 20$ " << "\n";
  97. cout << " Y 1.5$ adicionales por cada 500 gr de màs" << "\n";
  98. diferencia = ((peso-1000) / 500);
  99. total_a_pagar = (25 + ((diferencia *2)+2));
  100. cout << " Se le cobraran" << total_a_pagar << " $ en total" << "\n";
  101. total_dia_siguiente_normal_p1 += 1;
  102. }
  103. else if ((dia = 9)&& (peso > 1000))
  104. {
  105. cout << " Se le cobraran 15$ " << "\n";
  106. cout << " Y 1$ adicionales por cada 500 gr de màs" << "\n";
  107. diferencia = ((peso-1000) / 500);
  108. total_a_pagar = (25 + ((diferencia *2)+2));
  109. cout << " Se le cobraran" << total_a_pagar << " $ en total" << "\n";
  110. total_dos_dias_o_mas_p1 += 1;
  111. }
  112. }
  113. }
  114.  
  115.  
  116. total_paquete = total_dia_siguiente_normal_p + total_dos_dias_o_mas_p + total_dia_siguiente_prioritario_p; // TOTAL PAQUETE
  117. total_carta = total_dia_siguiente_prioritario_c + total_dia_siguiente_normal_c; //TOTAL CARTAS
  118.  
  119. total_dia_sig_prioritario = total_dia_siguiente_prioritario_p + total_dia_siguiente_prioritario_c; //TOTAL DIA SIGUIENTE PRIORITARIO
  120. total_dia_sig_normal = total_dia_siguiente_normal_p + total_dia_siguiente_normal_c; // TOTAL DIA SIGUIENTE NORMAL
  121. total_2_dias_o_mas = total_dos_dias_o_mas_p; // TOTAL DOS DIAS O MAS
  122.  
  123. totales_de_entregas = total_paquete + total_carta; //TOTAL DINERO RECAUDADO
  124. // dinero_recaudado = ;
  125.  
  126. system ("PAUSE");
  127. return 0;
  128.  
  129. }
Last edited by Tekmaven; Jul 24th, 2008 at 3:09 am. Reason: Code tags
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC