Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,968 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,378 people online right now. Registration is fast and FREE... Join Now!




Little help with my code

 
Reply to this topicStart new topic

Little help with my code

Sergiux
4 Dec, 2007 - 10:24 PM
Post #1

New D.I.C Head
*

Joined: 14 Nov, 2007
Posts: 3


My Contributions
Hi, I'm new to programming and also new to this site.

This is my code:
CODE
/* Programa que halla el área de la figura geométrica */
#include <stdio.h>
float cuatro_lados (float figura); /*Prototipo función de la figura de cuatro lados*/
float triangulo (float baseT, float alturaT); /*Prototipo función del triángulo*/
int lados;
main ()
{
printf ("Escriba el número de lados de la figura: ");
scanf ("%d",&lados);
while (lados != 3 && lados != 4)
{
printf ("Intente de nuevo: ");
scanf ("%d",&lados);
}
if (lados == 3)
{
float base, altura, area;
area = triangulo (base, altura);
printf ("El área es: %f\n",area);
}
else
{
float figura4;
float area4 = cuatro_lados (figura4);;
printf ("El área es: %f\n",area4);
}
return 0;
}
float triangulo (float baseT, float alturaT) /*Función del triángulo*/
{
float base, altura;
printf ("Escribe la base del triángulo: ");
scanf ("%f",&base);
printf ("Escribe la altura del triángulo: ");
scanf ("%f",&altura);
return (base * altura /2);
}
float cuatro_lados (float figura) /*Función de los cuatro lados */
{
int figura4;
printf ("Especifica de qué figura se trata: \n1. Cuadrado\n2. Rectángulo\nEscribe el número correspondiente: ");
scanf ("%d",&figura4);
while (figura4 != 1 && figura4 != 2)
{
printf ("Escriba el número correspondiente: ");
scanf ("%d",&figura4);
}
if (figura4 == 1)
{
float lado;
printf ("Escriba el lado del cuadrado: ");
scanf ("%f",&lado);
return (lado * lado);
}
if (figura4 == 2)
{
float base;
float altura;
printf ("Escriba la base del rectángulo: ");
scanf ("%f",&base);
printf ("Escriba la altura del rectángulo: ");
scanf ("%f",&altura);
return (base * altura);
}
}


Well, as you can probably see, it's like a first-time code... and it works smile.gif
What I want to know is the following: You see at the main function that if integer 'lados' is different than 3 and different from 4, keep printing out 'Intente de nuevo' (try again). This works when printing in a different integer than 3 and 4, but when typing a letter or a float number, it fills my console saying 'Intente de nuevo' and doesn't scan again 'lados'.

I hope my problem has been proposed clearly enoght... Thanks for your help.
User is offlineProfile CardPM
+Quote Post

Pontus
RE: Little Help With My Code
5 Dec, 2007 - 10:06 AM
Post #2

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 544



Thanked: 4 times
Dream Kudos: 275
My Contributions
Alot of new people ask that 1. Well, this is my solution: instead of amking lados an integer, make it a char.
But becarefull, when u are checking the value of the character dont use == 3 but =='3'


code:
CODE

* Programa que halla el área de la figura geométrica */
#include <stdio.h>
float cuatro_lados (float figura); /*Prototipo función de la figura de cuatro lados*/
float triangulo (float baseT, float alturaT); /*Prototipo función del triángulo*/
char lados;
main ()
{
printf ("Escriba el número de lados de la figura: ");
lados=getchar();
while (lados != '3' && lados != '4')
{
printf ("Intente de nuevo: ");
lados=getchar();
}
if (lados == '3')
{
float base, altura, area;
area = triangulo (base, altura);
printf ("El área es: %f\n",area);
}
else
{
float figura4;
float area4 = cuatro_lados (figura4);;
printf ("El área es: %f\n",area4);
}
return 0;
}
float triangulo (float baseT, float alturaT) /*Función del triángulo*/
{
float base, altura;
printf ("Escribe la base del triángulo: ");
scanf ("%f",&base);
printf ("Escribe la altura del triángulo: ");
scanf ("%f",&altura);
return (base * altura /2);
}
float cuatro_lados (float figura) /*Función de los cuatro lados */
{
int figura4;
printf ("Especifica de qué figura se trata: \n1. Cuadrado\n2. Rectángulo\nEscribe el número correspondiente: ");
scanf ("%d",&figura4);
while (figura4 != 1 && figura4 != 2)
{
printf ("Escriba el número correspondiente: ");
scanf ("%d",&figura4);
}
if (figura4 == 1)
{
float lado;
printf ("Escriba el lado del cuadrado: ");
scanf ("%f",&lado);
return (lado * lado);
}
if (figura4 == 2)
{
float base;
float altura;
printf ("Escriba la base del rectángulo: ");
scanf ("%f",&base);
printf ("Escriba la altura del rectángulo: ");
scanf ("%f",&altura);
return (base * altura);
}
}

User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Little Help With My Code
5 Dec, 2007 - 10:09 AM
Post #3

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,143



Thanked: 77 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(manhaeve5 @ 5 Dec, 2007 - 11:06 AM) *

Alot of new people ask that 1. Well, this is my solution: instead of amking lados an integer, make it a char.
But becarefull, when u are checking the value of the character dont use == 3 but =='3'


I would suggest using isdigit or isalpha to verify the values data type.

Both are declared in ctype.h.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Little Help With My Code
5 Dec, 2007 - 11:14 AM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
This is a very common question, but one where we really don't have a standard answer. The problem is that scanf is trying to read in a number, if it does not find a number if leaves the input alone and moves on, but your program does not really take that into account and the next time it asks for input it sees that old input still in the buffer and tries to use it... but it still isn't a number... loop.

So how do we get arround this little problem? One way is to read in a char, or text since scanf really does not look for formatting there.

Another way is to place the input into a buffer, and then use sscanf to read the buffer. This is the path I usually take.

Here is an example menu program. The thing to look at here is the two functions getLine() and get_users_input().

getLine() will get characters from the input stream, but only so many, until the user presses enter. This is helpful just as an input routine in general.

the get_users_input() will use getLine() to actually get what the user types, then it will use sscanf() to convert that to a number. It will ensure that the user's input is between 1 and max. It also has some logic to limit the number of attempts that the routine allows before it exits.

Its not the best input routine I have ever written but it should give you some ideas.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 06:32PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month