PDA

View Full Version : If anybody knows C++ I need help here



Bandito
03-22-2014, 09:43 PM
// Bono marzo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
const int array_size = 10;
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{

int car[array_size], client, index, menu, number, decision;
/*
car[] = 1 para Toyota, 2 para Jeep, 3 para BMW
client = cantidad de clientes
index= numero unico para saber quien es el cliente
menu = es las opciones del menu
number = despues de darle search y encontrar al usuario se usa ests variable para almacenar el index correcto
*/
float rent[array_size], miles[array_size], deuda;
/*
deuda = la deuda actual del cliente
miles[] = millas recorrida por el cliente
rent[] = dias que el cliente tiene el carro rentado
*/
char flag;//es para controlar y salir del loop
string Fname[array_size], Lname[array_size], se_Fname[array_size], se_Lname[array_size];
/*
Fname[] = nombre del cliente
Lname[] = apellido del cliente
se_Fname[] = el nombre a buscar
se_Lname[] = el apellido a buscar
*/
flag = 'y';
menu = 0;
while (flag == 'y' || flag == 'Y')
{
cout<<"*******************************************"<<endl<<"Enter a number to choose an option:\n"<<"1: Create an user\n"<<"2:Search and display an user data\n";
cout<<"3:Edit an already created user.\n*******************************************";
cin>>menu;//El menu del programa
switch(menu)
{//start of switch
case 1://Creates an user
{
cout<<"How many clients do you want to enter? ";
cin>>client;//number of clients to add in the array
cout<<endl;
for (index =1; index <= array_size; index++)
if (client > 0)
{
if (car[index] < 0)
{//Los datos a crearse de la tabla si el cliente no existe
cout<<"Enter the first name of the client ";
cin>> Fname[index];//El usuario entra el primer nombre del cliente
cout<<"\nEnter the last name of the client ";
cin>>Lname[index];//EL usuario entra el apellido
cout<<"\n**********\nEnter the car model to be rented; Enter a number to choose an option.\n1: 1: Toyota\n2: Jeep\n3:BMW\n**********\n";
cin>>car[index];//el usuario entra el modelo de carro a escoger
miles [index] = 0;//Las millas recorridas; es cero porque el cliente no ha recorrido millas
rent [index] = 0;//Es los dias que el cliente ha tenido el carro; es cero porque el cliente abrio la cuenta hoy
deuda = 0;//Es la deuda existente; es cero porque el cliente no tiene deuda
cout<<endl;
}
else
{
Fname [index] = "0";
Lname [index] = "0";
car [index] = 0;
miles [index] = 0;
rent [index] = 0;
deuda = 0;
}
client--;}
break;
}//end of case 1
case 2://Search and display an user
{
cout<<"Enter the first name of the client: ";
cin>>se_Fname[0];
cout<<endl;
cout<<"Enter the last name of the client: ";
cin>>se_Lname[0];
cout<<endl;
for (index = 1; index<= array_size; index++)
{
if (se_Fname[0] == Fname[index] && se_Lname[0] == Lname[index])
number = index;
}
cout<<"first name of the client: "<<Fname[number]<<"\nLast name of the client: "<<Lname[number];
cout<<"\nCar rented: ";
if (car[number] == 1)
{
cout<<"Toyota";
}
else
{
if (car[number] == 2)
{
cout<<"Jeep";
}
else
{
cout<<"BMW";
}
}
cout<<"\nMiles traveled: "<<miles[number]<<"\nDays rented: "<<rent[number];
cout<<"\nThe current debt is: $"<<deuda<<endl;
break;
}//end of case 2
case 3://Edit an user
{//start of case 3
cout<<"Enter the first name of the client: ";
cin>>se_Fname[0];
cout<<endl;
cout<<"Enter the last name of the client: ";
cin>>se_Lname[0];
cout<<endl;
for (index = 1; index<= array_size; index++)
{
if (se_Fname[0] == Fname[index] && se_Lname[0] == Lname[index])
number = index;
}
cout<<"Edit the days rented for "<<Fname[number]<<" "<<Lname[number]<<": ";
cin>>rent[number];
cout<<"Edit the miles traveled for "<<Fname[number]<<" "<<Lname[number]<<": ";
cin>>miles[number];
//Calculations
if (car[number] == 1)//Si es toyota
{
if (miles[number] < 100)
deuda = rent[number] * 26;
else
deuda = rent[number] * 26 + .18 * miles[number];
}
if (car[number] == 2)//Si es Jeep
{
if (miles[number] < 100)
deuda = rent[number]* 32;
else
deuda = rent[number] * 32 + .22 * miles[number];
}
if (car[number] == 3)//Si es BMW
{
if (miles[number] < 100)
deuda = rent[number] * 46;
else
deuda = rent[number] * 46 + .28 * miles[number];
}
break;
}//end of case 3
}//end of switch
cout<<"Continue running the program? Y/N: ";
cin>>flag;
}//end of while
system("pause");
return 0;
}


I made this program in visual c++. It supposed to be a program where the user choose an option and he creates some clients in order to see and edit a car rental place. Well for some reason the program keeps crashing and I don't even know what to do. I rewrote the program a couple of times but I don't know what I am doing wrong.

This is the error I get in Visual Studio 2010


http://i61.tinypic.com/23rtqae.png

I am using c++

Bandito
03-22-2014, 09:56 PM
If anybody needs more explanation I will do so.

embersyc
03-23-2014, 09:11 AM
My first guess is that you are trying to access an out of bounds index in one of your arrays...

Can you post what output your program gives prior to crashing?

ArbitraryWater
03-23-2014, 09:18 AM
*Waiting for GiftedMind to solve the problem*

Bandito
03-23-2014, 11:09 AM
My first guess is that you are trying to access an out of bounds index in one of your arrays...

Can you post what output your program gives prior to crashing?
You thought wrong:D

I fixed it when I moved from visual studio to dev c++ for some reason but I don't really see how it just doesn't work there.

D-FENS
03-23-2014, 11:10 AM
Erm, one of your "/" is in the wrong place. Look at them all carefully again

Bandito
03-23-2014, 11:18 AM
Erm, one of your "/" is in the wrong place. Look at them all carefully again
If it is for comments it could be when I copy pasta it. They syntax' right, the program runs. The problem is logic related.

Even then it runs on another compiler for some reason, it just doens't run on visual studio.

shaq2000
03-23-2014, 12:42 PM
Just glancing at it, it looks like you're initializing the "number" int inside of conditionals but using it as an index whether the conditional fired or not.

Bandito
03-23-2014, 12:46 PM
Just glancing at it, it looks like you're initializing the "number" int inside of conditionals but using it as an index whether the conditional fired or not.
Yeah number and index is what I used when I want to compare the arrays for fname [] and se_fname[], lname [] and se_lname [].
They are just the index numbers for the arrays.

shaq2000
03-23-2014, 12:49 PM
Yeah number and index is what I used when I want to compare the arrays for fname [] and se_fname[], lname [] and se_lname [].
They are just the index numbers for the arrays.

But it's possible that "number" won't be initialized when you try to use it as an array index and the compiler knows it.

Bandito
03-23-2014, 12:53 PM
But it's possible that "number" won't be initialized when you try to use it as an array index and the compiler knows it.
It is actually. The problem is when I compare the two arrays the first time it works fine. Let's say I choose option 3, I will then choose option 2 and the program will crash. It happens viceversa too.


But if I choose option 2 (or 3) and choose it again for a second time it runs fine. But like I said before the problem is in Visual Studio as I downloaded Dev c++ and it runs fine there, but I don't know why though:confusedshrug:

shaq2000
03-23-2014, 01:21 PM
You first assign a value to "number" here:

if (se_Fname[0] == Fname[index] && se_Lname[0] == Lname[index])
number = index;
If it's not true, no value gets assigned and yet you still try to use it as an index. It might not be the cause of your memory error, but it can definitely be an issue.

You can't run this without error, for example:


#include <iostream>
using namespace std;

int main()
{
int uninitializedVar, initializedVar = 0;
char cArr[3] = {'a', 'b', 'c'};

cout << cArr[initializedVar] << endl; // fine

if(0) uninitializedVar = 1;

cout << cArr[uninitializedVar] << endl; // not fine
return 0;
}

Bandito
03-23-2014, 01:25 PM
You first assign a value to "number" here:

if (se_Fname[0] == Fname[index] && se_Lname[0] == Lname[index])
number = index;
If it's not true, no value gets assigned and yet you still try to use it as an index. It might not be the cause of your memory error, but it can definitely be an issue.

You can't compile this, for example:


#include <iostream>
using namespace std;

int main()
{
int uninitializedVar, initializedVar = 0;
char cArr[3] = {'a', 'b', 'c'};

cout << cArr[initializedVar] << endl; // fine

if(0) uninitializedVar = 1;

cout << cArr[uninitializedVar] << endl; // not fine
return 0;
}

Well if that is true, but I am testing it as if everything is supposed to be working. So if I add two names and they are in the same index in the array, and it has the same value as the other arrays (in other words fname[] == se_fname[]) then it is supposed to run no?

I see what you're saying though, but in my case if the values fit it crashes.

But why does it work in another compiler and not Visual studio though? I just tested it right now and it runs fine.:roll: :facepalm

shaq2000
03-23-2014, 01:30 PM
I just tested it on Visual Studio here. I'm not getting a memory read error, but I am getting a crash. It only crashes for me when I enter a first or last name that doesn't match. I.E. the conditional I'm talking about.

You should debug step-by-step and see where your error is coming from.

Bandito
03-23-2014, 01:33 PM
so if it's the same one it runs fine?

WTF!? Why does mine crash then !!!!?!?!?!!? raargh!!!!!

shaq2000
03-23-2014, 01:37 PM
Yeah, mine runs fine as long as I, the user, manually make sure the data is correct and matches.

Run the debugger and let me know what line throws the error.

Bandito
03-23-2014, 02:00 PM
Yeah, mine runs fine as long as I, the user, manually make sure the data is correct and matches.

Run the debugger and let me know what line throws the error.
In visual studio 2010 it doesn't say the line it crashes. The pic that shows the error is in the OP.

Bandito
03-24-2014, 07:53 PM
I fixed it this morning.

I just replaced int _tmain(int argc, _TCHAR* argv[]) with int main ()

CelticBaller
03-24-2014, 07:54 PM
just to let you know im saving all these threads, computer engineering major here :cheers:

Bandito
03-24-2014, 08:00 PM
just to let you know im saving all these threads, computer engineering major here :cheers:

You'll be my boss in the future:cheers:

Bandito
03-27-2014, 11:32 PM
It runs now. I learn how to do functions and I added them because it makes everything so easy. Besides that I made the program make a file so it prints out everything to a text file.

Anyways here it is:

PS: the comments are in Spanish, forgot to translate it.

[CODE]// Bono marzo.cpp : Esteban Morales G00294816
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
const int array_size = 10; //This the size of an array as it is a static array
using namespace std;
void calculation(float& deuda, int numero);//se declara la funcion
void search (int& numero);//Funcion para darle search a los datos de los clientes
void files (void);

ifstream car_infile, rent_infile, miles_infile, Fname_infile, Lname_infile;
/*
car_infile = se usa para llamar el archivo de los valores de car[]
rent_infile = se usa para llamar el archivo de los valores de rent[]
miles_infile = se usa para llamar el archivo de los valores de miles[]
Fname_infile = se usa para llamar el archivo de los valores de Fname[]
Lname_infile = se usa para llamar el archivo de los valores de Lname[]
*/
ofstream car_outfile, rent_outfile, miles_outfile, Fname_outfile, Lname_outfile, array_outfile;
/*
car_outfile = se usa para grabar el archivo de los valores de car[]
rent_outfile = se usa para grabar el archivo de los valores de rent[]
miles_outfile = se usa para grabar el archivo de los valores de Fname
Lname_outfile = se usa para grabar el archivo de los valores de Lname
*/


int car[array_size], index;
/*
car[] = 1 para Toyota, 2 para Jeep, 3 para BMW
index= numero unico para saber quien es el cliente
number = despues de darle search y encontrar al usuario se usa ests variable para almacenar el index correcto
*/
float rent[array_size], miles[array_size], debt[array_size];
/*
deuda = la deuda actual del cliente
miles[] = millas recorrida por el cliente
rent[] = dias que el cliente tiene el carro rentado
*/
string Fname[array_size], Lname[array_size];
/*
Fname[] = nombre del cliente
Lname[] = apellido del cliente
se_Fname[] = el nombre a buscar
se_Lname[] = el apellido a buscar
*/

int main()
{
string car_type;//se usa para igualar el array de car[] a un string
char flag;//es para controlar y salir del loop
int decision, menu, number;
float debt;
/*
number = es el numero del index cuando se busca a un cliente en especifico
menu = es las opciones del menu
number = despues de darle search y encontrar al usuario se usa esta variable para almacenar el index correcto
*/
flag = 'y';
menu = 0;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
array_outfile.open ("INFO.txt");
array_outfile<<"\n\t\t\t\t\tCompa

Draz
03-27-2014, 11:37 PM
Don't know c++ but I can make you snacks

Bandito
03-27-2014, 11:39 PM
Don't know c++ but I can make you snacks
Gimme!!!:cheers: