Foros del Web » Programación para mayores de 30 ;) » C/C++ »

Help with "if"

Estas en el tema de Help with "if" en el foro de C/C++ en Foros del Web. I'm trying to create a friendly aplication that finds areas and perimeters and this is my code: #include <iostream> using namespace std; int main (void) ...
  #1 (permalink)  
Antiguo 02/09/2006, 18:35
 
Fecha de Ingreso: septiembre-2006
Mensajes: 328
Antigüedad: 17 años, 8 meses
Puntos: 3
Help with "if"

I'm trying to create a friendly aplication that finds areas and perimeters and this is my code:

#include <iostream>

using namespace std;

int main (void) {
string name;

cout<<"Hi, What's your name? ";
cin>> name;

cout<<"Oh, hi "<<name<<", my name is Pedro!\n";

int a;
int b;

cout<<"How old are you? ";
cin>> a;

if (a<17) {

b = 17-a;
cout<<"Wow! I'm "<<b<<" years older than you!\n";

} else if (a==17) {

cout<<"Cool! We're the same age!\n";

}else if (a ==18) {

cout<<"You are 1 year older than me!\n";

} else if (a>18) {

b = a-17;
cout<<"Oh, I see, you are "<<b<<" years older than me!\n";
}

int shape;
int command;

cout<<"..........\n";
cout<<"\n";
cout<<" Although I'm relatively young I can accomplish many mathematical tasks, since \nyou're my BUDDY now, you don't mind me showing you, do you?\n";
inicio:
cout<<"\n";
cout<<"What shape would you like me to calculate something of?\n";
cout<<"1.- Square 2.-Rectangle 3.-Triangle 4.-Circle\n";
cin>> shape;
cout<<"\n";
cout<<"What do you want me to find?\n";
cout<<"1.- Area 2.-Perimeter\n";
cin>> command;
cout<<"\n";

int sizes;
int sizesp;
int rsize;
int rsizeh;
int rsizep;
int rbase;
int rhgt;
float x, x1, x2;


if (shape ==1, command ==1) {
cout<<"Enter the measure of one side: ";
cin>>sizes;

cout<<"The Result is: "<<sizes*sizes;
cout<<"\n";
cout<<"\nPress 1 if you wanna start over\nPress 2 if you wanna quit\n";
cin>> x;
if (x ==1) {
goto inicio;
}
if (x ==2) {
goto end;
}
}

if (shape ==1, command ==2) {
cout<<"Enter the measure of one side: ";
cin>>sizesp;

cout<<"The Result is: "<<sizesp*4<<"\n";
cout<<"\nPress 1 if you wanna start over\nPress 2 if you wanna quit\n";
cin>> x1;
if (x1 ==1) {
goto inicio;
}
if (x1 ==2) {
goto end;
}
}

if (shape ==2, command ==1) {
cout<<"Enter the size of the base: ";
cin>>rsize;

cout<<"Enter The Height: ";
cin>>rsizeh;

cout<<"The Result is: "<<rsize*rsizeh;
cout<<"\n";
cout<<"\nPress 1 if you wanna start over\nPress 2 if you wanna quit\n";
cin>> x;
if (x ==1) {
goto inicio;
}
if (x ==2) {
goto end;
}
}
end:;
}


My problem is the following: If I choose rectangle the code takes me to square's code anyways.

What do I have to do for the code to take me to rectancle's code when I type 2 for shape.
  #2 (permalink)  
Antiguo 02/09/2006, 19:15
Avatar de sam
sam
 
Fecha de Ingreso: agosto-2001
Ubicación: Aguascalientes, México.
Mensajes: 572
Antigüedad: 22 años, 8 meses
Puntos: 2
This is a spanish forum my friend.

Lo de mas va en español xD

Puedes usar switch en este caso, si quieres usar if, verifica tu sintaxis, por ejemplo:

if (shape ==1, command ==2)

deberia ir:

if ((shape ==1)&&(command ==2))

Saludos xD.
__________________
El que tiene voluntad de aprender, encontrará siempre un maestro...
Proverbio Aleman
  #3 (permalink)  
Antiguo 02/09/2006, 19:18
 
Fecha de Ingreso: septiembre-2006
Mensajes: 328
Antigüedad: 17 años, 8 meses
Puntos: 3
Estoy tratando de crear una aplicacion amigable k halla areas y perimetros para figuras, este es mi codigo:

#include <iostream>

using namespace std;

int main (void) {
string name;

cout<<"Hi, What's your name? ";
cin>> name;

cout<<"Oh, hi "<<name<<", my name is Pedro!\n";

int a;
int b;

cout<<"How old are you? ";
cin>> a;

if (a<17) {

b = 17-a;
cout<<"Wow! I'm "<<b<<" years older than you!\n";

} else if (a==17) {

cout<<"Cool! We're the same age!\n";

}else if (a ==18) {

cout<<"You are 1 year older than me!\n";

} else if (a>18) {

b = a-17;
cout<<"Oh, I see, you are "<<b<<" years older than me!\n";
}

int shape;
int command;

cout<<"..........\n";
cout<<"\n";
cout<<" Although I'm relatively young I can accomplish many mathematical tasks, since \nyou're my BUDDY now, you don't mind me showing you, do you?\n";
inicio:
cout<<"\n";
cout<<"What shape would you like me to calculate something of?\n";
cout<<"1.- Square 2.-Rectangle 3.-Triangle 4.-Circle\n";
cin>> shape;
cout<<"\n";
cout<<"What do you want me to find?\n";
cout<<"1.- Area 2.-Perimeter\n";
cin>> command;
cout<<"\n";

int sizes;
int sizesp;
int rsize;
int rsizeh;
int rsizep;
int rbase;
int rhgt;
float x, x1, x2;


if (shape ==1, command ==1) {
cout<<"Enter the measure of one side: ";
cin>>sizes;

cout<<"The Result is: "<<sizes*sizes;
cout<<"\n";
cout<<"\nPress 1 if you wanna start over\nPress 2 if you wanna quit\n";
cin>> x;
if (x ==1) {
goto inicio;
}
if (x ==2) {
goto end;
}
}

if (shape ==1, command ==2) {
cout<<"Enter the measure of one side: ";
cin>>sizesp;

cout<<"The Result is: "<<sizesp*4<<"\n";
cout<<"\nPress 1 if you wanna start over\nPress 2 if you wanna quit\n";
cin>> x1;
if (x1 ==1) {
goto inicio;
}
if (x1 ==2) {
goto end;
}
}

if (shape ==2, command ==1) {
cout<<"Enter the size of the base: ";
cin>>rsize;

cout<<"Enter The Height: ";
cin>>rsizeh;

cout<<"The Result is: "<<rsize*rsizeh;
cout<<"\n";
cout<<"\nPress 1 if you wanna start over\nPress 2 if you wanna quit\n";
cin>> x;
if (x ==1) {
goto inicio;
}
if (x ==2) {
goto end;
}
}
end:;
}


My problema es el siguiente: Si elijo "rectangulo" el codigo me lleva, de todos modos, al codigo del cuadrado.

¿Que tengo k hacer para k el codigo me lleve hacia el rectangulo cuando pulse el numero 2 para "shape"?.
  #4 (permalink)  
Antiguo 02/09/2006, 19:22
 
Fecha de Ingreso: septiembre-2006
Mensajes: 328
Antigüedad: 17 años, 8 meses
Puntos: 3
Gracias, pero eso no resuleve mi problema, ahora en vez de llevarme al codigo del rectangulo, la ventana se cierra.
  #5 (permalink)  
Antiguo 03/09/2006, 03:30
 
Fecha de Ingreso: abril-2006
Ubicación: Acapulco Gro. México
Mensajes: 483
Antigüedad: 18 años
Puntos: 2
si modificas los 3 if :
if (shape ==1, command ==1) {
if (shape ==1, command ==2) {
if (shape ==2, command ==1) {
por:
if (shape ==1 && command ==1) {
if (shape ==1 && command ==2) {
if (shape ==2 && command ==1) {

es probable que tu programa funciones como esperas.

ya que la exprecion if (shape ==1, command ==1) { creo que se evalua a true siempre.

Saludos.
  #6 (permalink)  
Antiguo 03/09/2006, 13:22
 
Fecha de Ingreso: septiembre-2006
Mensajes: 328
Antigüedad: 17 años, 8 meses
Puntos: 3
Muchas, muchas gracias. eso resuelve mi problema, hace mucho k keria dar con ese codigo. Muchas Gracias.
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 01:22.