d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Easy C++ Help
Add Reply New Topic New Poll
Member
Posts: 13,899
Joined: Aug 14 2009
Gold: 1,399.00
Nov 19 2013 07:43pm
Is this possible?
To define a predetermined character ??
how do i do this?
i mean something like this:

cout << "Press 1 to choose package N 1" << endl;
cin >> resp;

switch (resp)
{

case 1:

Cost = 1000;
Servicetype [0] = 'Fly to New York2' ;

break;


when i print that, it shows trash data...

Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 19 2013 08:05pm
you might need to cast resp, not sure.

not sure what "shows trash data" since you didnt post the code

and i think strings are " and not '

This post was edited by carteblanche on Nov 19 2013 08:06pm
Member
Posts: 13,899
Joined: Aug 14 2009
Gold: 1,399.00
Nov 19 2013 08:44pm
Code
#ifndef SERVICES_H
#define SERVICES_H
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>


using namespace std;

class Servicio
{
public:

int Codigo, opc, dato, opcion, CodigoS;
string TServicio [20];
float Costo, Comision, goli;
int enc, gol, codservicio;

public:

void Capturar ();
void Mostrar ();
int Buscar (int);
int Modificar (int);
void Salir ();

};


void Servicio::Capturar()
{
do {
cout << " |------------------------------------------------------------------------|" << endl;
cout << " | Código | | | Comisión |" << endl;
cout << " | del | Servicio | Costo | al |" << endl;
cout << " | servicio | | | empleado |" << endl;
cout << " |------------------------------------------------------------------------|" << endl;
cout << " | 100 | Cable básico | 150 | 5 |" << endl;
cout << " |------------------------------------------------------------------------|" << endl;
cout << " | 101 | Cable total | 300 | 7 |" << endl;
cout << " |------------------------------------------------------------------------|" << endl;
cout << " | 102 | Cable total plus | 550 | 10 |" << endl;
cout << " |------------------------------------------------------------------------|" << endl;
cout << " | 103 | Internet + Cable | 469 | 12 |" << endl;
cout << " |------------------------------------------------------------------------|" << endl;
cout << " | 104 | Cable + Teléfono | 500 | 12 |" << endl;
cout << " |------------------------------------------------------------------------|" << endl;
cout << " | 105 | Cable + Teléfono + Internet| 635 | 15 |" << endl;
cout << " |------------------------------------------------------------------------|" << endl << endl;

cout << " Digite el código del servicio que desea contratar:" << endl;
cout << " En caso de contratar un servicio personalizado digite 0" << endl;
cout << endl;
cout << " ";cin >> codservicio;

switch (codservicio)
{

case 100:

CodigoS = 100;
TServicio [0] = "Cable basico"; <<<<<<<<<<<------------------------------------------------------------------------
Costo = 150;
Comision = 5;

cout << endl;
cout << " Número de la venta:" << endl;
cout << " ";cin >> Codigo;
goli = 0;

break;


My homework is in spanish.. if you need some translation just lmk pls

TServicio [0] = "Cable basico";

THIS IS WHAT IM ASKING WHERE AM I WRONG? WHEN I PRINT THAT IT JUST MAKE RANDOM NUMERS AND LETTERS



This post was edited by ClubRocker on Nov 19 2013 08:48pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 19 2013 09:03pm
i'm not a cpp expert, but this doesn't look right to me:

string TServicio [20];
TServicio [0] = "Cable basico";

go google for cpp array of strings example

This post was edited by carteblanche on Nov 19 2013 09:04pm
Member
Posts: 13,899
Joined: Aug 14 2009
Gold: 1,399.00
Nov 22 2013 10:31pm
strcpy worked thanks cartelblanche
Member
Posts: 23,261
Joined: Dec 17 2006
Gold: 18,129.00
Nov 23 2013 09:58am
I'm a little rusty in my C as of lately, but if you want to use strings, use a const char point:

const char *array[20];

Then to place an item in the array I think you could do:

array[0] = "whatever";

OR

You could go:

char array[20][20];
strcpy(array[0], "whatever");


The second method will allow you to change the content of the string. In the const char pointer, it is pointing to an address which has the content "whatever" inside. There is no easy way to manipulate this.

However, in the second method, you're able to change the string however you would like as each character is stored individually in an array.

Basically, when you call the first one, it retrieves "whatever"

Where as, the second one will call each section of the array
array[0] = w
array[1] = h
array[2] = a
array[3] = t
array[4] = e

I think you get the point. The second method is going to take more memory, but you're able to manipulate each section of the array. (this is a very frequent project in C classes at schools)
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll