hi ppl
im back here with a kinda major problem

thing is i made this program for some company that sends letters or packages, and has 3 types of services: to send it the next day with priority, the next day but whithout the priority, or to send it in 2 or more.
the letters must weight 30 gr as max, if its more, then is a package.
packages are also divided: if it weights less than 1000 gr it has a fixed cost, that depends on the day i send it, and if it weights more, i have to add some extra money, also depending on the day.

the problem is when the program just began: i validate if the user wants to start or not. it doesnt take the 0 to go out. if i put the '0', doesnt do anything, and gives an error, if i dont, the program keeps going as if the while wasnt there.

in the word attached, is the error that appear.

hope the explanation is helpfull :S

dont know what it is :S

// cartas_y_paquetes.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

int main(int argc, char* argv[])
{
	using namespace std;

	char empezar;
	char a_enviar, dia;
	float peso;

	int total_dsp_c = 0, costo_dsp_c = 0;
	int total_dsn_c = 0, costo_dsn_c = 0;

	int total_dsp_p = 0, costo_dsp_p = 0;
	int total_dsn_p = 0, costo_dsn_p = 0;
	int total_dos_d_p = 0, costo_dos_d_p = 0;

	int total_dsp_p1000 = 0, costo_dsp_p1000 = 0;
	int total_dsn_p1000 = 0, costo_dsn_p1000 = 0;
	int total_dos_d_p1000 = 0, costo_dos_d_p1000 = 0;

	int totales_dsp, totales_dsn, totales_dos_d;
	int recaudado_dsp, recaudado_dsn, recaudado_dos_d;
	
	int entregas_totales, costo_total;

	int porc_dsp, porc_dsn, porc_dos_d;

	cout << " Empezar?? " << "\n";         // TO START
	cout << "    1 - SI" << "\n";
	cout << "    0 - NO" << "\n";
	cin >> empezar;
	cout << "\n" << "\n";

	while (empezar != '0')      // IF I PUT THIS WITH '' DOESNT WORK. IF DONT, DOESNT TAKE IT
	{
		cout << " Ingrese lo que va a enviar: " << "\n";
		cout << "    3 - Carta" << "\n";
		cout << "    4 - Paquete " << "\n";
		cin >> a_enviar;
		cout << "\n" << "\n";
		
		cout << " Ingrese dia a enviar: " << "\n";
		cout << "    6 - Dia Siguiente prioritario" << "\n";
		cout << "    7 - Dia siguiente normal" << "\n";
		cout << "    8 - Dos dias o mas" << "\n";
		cin >> dia;
		cout << "\n" << "\n";

		cout << " Ingrese peso del paquete: " << "\n";
		cin >> peso;
		cout << "\n" << "\n";


		if (a_enviar == '3')     
		{
			if(dia == '7')
			{
				costo_dsp_c += 8;
				total_dsp_c += 1;
			}
			else if (dia == '8')
			{
				costo_dsn_c += 5;
				total_dsn_c += 1;
			}
			else if (dia == '9')
				cout << " NO DISPONIBLE" << "\n";
		}
		if ((a_enviar == '4') && (peso <= 1000))
		{
			if (dia == '6')
			{
				costo_dsp_p += 25;
				total_dsp_p +=1;
			}
			else if (dia == '7')
			{
				costo_dsn_p += 20;
				total_dsn_p +=1;
			}
			else if (dia == '8')
			{
				costo_dos_d_p += 15;
				total_dos_d_p +=1;
			}
		}
		if ((a_enviar == '4') && (peso > 1000))
		{
			if (dia == '6')
			{
				costo_dsp_p1000 += 25 + (((peso-1000)/500)+2);
				total_dsp_p1000 +=1;
			}
			else if (dia == '7')
			{
				costo_dsn_p1000 += 20 + (((peso-1000)/500)+1.5);
				total_dsn_p1000 +=1;
			}
			else if (dia == '8')
			{
				costo_dos_d_p1000 += 15 + (((peso-1000)/500)+1);
				total_dos_d_p1000 +=1;
			}
		}

		cout << " Seguir??" << "\n";
		cout << "    1 - SI" << "\n";
		cout << "    0 - NO" << "\n";
		cin >> empezar;
		cout << "\n" << "\n";
	}


// CALCULATE TOTALS

	totales_dsp = total_dsp_c + total_dsp_p + total_dsp_p1000;
	totales_dsn = total_dsn_c + total_dsn_p + total_dsn_p1000;
	totales_dos_d = total_dos_d_p + total_dos_d_p1000;

	entregas_totales = totales_dsp + totales_dsn + totales_dos_d;

	recaudado_dsp = costo_dsp_c + costo_dsp_p + costo_dsp_p1000;
	recaudado_dsn = costo_dsn_c + costo_dsn_p + costo_dsn_p1000;
	recaudado_dos_d = costo_dos_d_p + costo_dos_d_p1000;

	costo_total = recaudado_dsp + recaudado_dsn + recaudado_dos_d;

	porc_dsp = (totales_dsp * 100) / entregas_totales;
	porc_dsn = (totales_dsn * 100) / entregas_totales;
	porc_dos_d = (totales_dos_d * 100) / entregas_totales;



// SHOW ALL TOTALS

	cout << "         ---         " << "\n";
	cout << "         ---         " << "\n";
	cout << "                     " << "\n";
	cout << " Las entregas totales hechas al dia siguiente prioritario fueron: " << totales_dsp << "\n";
	cout << " Las entregas hechas al dia siguiente normal fueron:" << recaudado_dsn << "\n";
	cout << " Las entregas realizadas a los dos dias o mas son: " << totales_dos_d << "\n";
	cout << "                     " << "\n";
	cout << "         ---         " << "\n";
	cout << "                     " << "\n";
	cout << " Lo recaudado en el dia siguiente prioritario es de " << recaudado_dsp << "\n";
	cout << " Lo que se recaudo por enviar en el dia siguiente normal fue de " << recaudado_dsn << "\n";
	cout << " Lo recaudado a los dos dias o mas fue de " << recaudado_dos_d << "\n";
	cout << "                     " << "\n";
	cout << "         ---         " << "\n";
	cout << "                     " << "\n";
	cout << " El porcentaje de las ventas del dia siguiente prioritario es de " << porc_dsp << "\n";
	cout << " El porcentaje de las ventas del dia siguiente normal es de " << porc_dsn << "\n";
	cout << " El porcentaje de las ventas de  dos dias o mas es de " << porc_dos_d << "\n";
	cout << "                     " << "\n";
	cout << "         ---         " << "\n";
	cout << "         ---         " << "\n";



	system ("PAUSE");
	return 0;
}

That error message is one of the clearest there is.

The problem is, if the user immediately chooses NO ( '0' ), you skip the while loop and go on to the summary calculations. entregas_totales gets set to value zero, and is used as a divisor a little bit further on. Integer division by zero is mathematically undefined, and causes a hardware abort of your program.

I think the simple solution is to place a transaction counter inside the loop - keep track of how many times you've processed, or just a boolean variable to know that you have entered the loop at least once. Test that that with an if( ) statement before doing the summary calculations.

Second problem is you are forgetting that the RETURN you type in is also a character that gets put into the buffer. Therefore, in this code:

cout << " Empezar?? " << "\n";         // TO START
	cout << "    1 - SI" << "\n";
	cout << "    0 - NO" << "\n";
	cin >> empezar;
	cout << "\n" << "\n";

	while (empezar != '0')      // IF I PUT THIS WITH '' DOESNT WORK. IF DONT, DOESNT TAKE IT
	{
		cout << " Ingrese lo que va a enviar: " << "\n";
		cout << "    3 - Carta" << "\n";
		cout << "    4 - Paquete " << "\n";
		cin >> a_enviar;
		cout << "\n" << "\n";

if you enter 0, empezar gets the value '0'
if you enter 1, empezar gets the value '1' and a_enviar reads the RETURN.
if you enter only RETURN, empezar reads the RETURN and since it's not '0' the loop is started.

Comments
Yep, good old character based input strikes again.

if you enter only RETURN, empezar reads the RETURN and since it's not '0' the loop is started.

On what system? cin >> x; skips whitespace, grabs a character(s) then halts at any whitespace. If it's not gotten a character, the program just sits waiting.

Now, using cin.get( ) is a whole 'nuther story.

Ahhh, yes. Thanks for the correction, vmanes. I was thinking of another input style. Had a brain fart... :icon_redface:

Comments
Stick to 'c' mate.

hi ppl, ive just changed the 0 for a 2, but it brings the exact same error.

and it points to the

porc_dsp = (totales_dsp * 100) / entregas_totales;
	porc_dsn = (totales_dsn * 100) / entregas_totales;
	porc_dos_d = (totales_dos_d * 100) / entregas_totales;

i just thought that those lines "are where they're supoused to be", cuz theyre calculating the percentage of what i entered before. so, they should be outside the while i use to validate if i begin or not.
but, maybe, it just doesnt take it, n should be in the while. but this is illogical, cuz i would be calculating it all day long and has no sens. it should be done just once, after receiving all data.

what would my problem be??

Since it's possible that no transactions occurred, this block must be defended against division by zero, as in:

if( entregas_totales != 0 )  //it's ok to do this
{
	porc_dsp = (totales_dsp * 100) / entregas_totales;
	porc_dsn = (totales_dsn * 100) / entregas_totales;
	porc_dos_d = (totales_dos_d * 100) / entregas_totales;
}
else
{ 
   //handle the error
}

Any time you have division by an integer variable, and you cannot absolutely, positively be certain it can NEVER hold value zero, you should set up such a test.

This article has been dead for over six months. Start a new discussion instead.