Welcome to Dream.In.Code
Click Here
Getting C++ Help is Easy!

Join 117,607 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 2,460 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Converter

 
Reply to this topicStart new topic

Converter, using Looping and Conditional

angel0lz
post 19 Jul, 2008 - 08:02 AM
Post #1


New D.I.C Head

*
Joined: 19 Jul, 2008
Posts: 8

we were given a project by our teacher, she told us we can make any program at our interest but it must have conditional statements and Looping.. i was planning to make a converter(e.g. convert meters to km, lbs to kg, sec to hr.) i made this draft.. but thinking how many formulas and menus i have to make i dont know how to simplify my code. i need to get this done by a week.. any help about for project will be much appreciated..

ill keep posting my codes to show you guys my process of work..


CODE
#include<stdio.h>
#include<conio.h>

main()
{
    char ans,try,y,n,a,b,c,d,e;

    clrscr();
    gotoxy(37,12);
    printf("CONVERTER 2008\n");

    try_again:

    printf("What do you want to convert?");
    printf("\nA:AREA\nB:MASS\nC:LENGTH\nD:TEMPERATURE\nE:TIME");
    ans=getche();
    clrscr();
    switch(ans)
    {
        case 'a':
        case 'A':
            printf("What unit of area do you want to convert?");
/* i still need to put menus for different types of areas */
            break;

        case 'b':
        case 'B':
            printf("What unit of mass do you want to convert?");
            break;
/* i still need to put menus for different types of mass conversions*/
        case 'c':
        case 'C':
            printf("What unit of Length do you want to convert?");
            break;
/* i still need to put menus for different types of length conversions */
        case 'd':
        case 'D':
            printf("What temperature do you want to convert?");
            break;
/* i still need to put menus for different types of temp conversions */
        case 'e':
        case 'E':
            printf("What unit of time do you want to solve?");
            break;

        default:
        printf("\n%c is not in the choices.", ans);
        printf("\nWant to try again<Y/N>?    ");
        try=getche();
            switch(try)
            {
                case 'y':
                case 'Y':
                    clrscr();
                    goto try_again;
                case 'n':
                case 'N':
                    break;
                default:
                    break;
             }


          }

    getch();
}


god bless guys

any suggestions will be very helpful to me!!

This post has been edited by angel0lz: 19 Jul, 2008 - 08:04 AM
User is offlineProfile CardPM

Go to the top of the page


Cerolobo
post 19 Jul, 2008 - 08:13 AM
Post #2


D.I.C Regular

Group Icon
Joined: 5 Apr, 2008
Posts: 440



Thanked 30 times
My Contributions


There are two things I would recommend

1: Create separate functions for your conversion code. Putting everything into main() is generally a bad idea. Working with smaller functions is typically easier to work with and maintain.

2: Use a loop instead of a goto. Looking at your code, a do while loop will work great. IE

CODE
/* try_again: was here */
do
{
  /* ... */
  switch(ans)
  {
    /* ... */

    default:
      printf("\n%c is not in the choices.", ans);
      printf("\nWant to try again<Y/N>?    ");
      try=getche();
  } /* end of switch(ans) */
} /* end of do */
while(try == 'y' || try == 'Y');
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 19 Jul, 2008 - 09:40 AM
Post #3


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,763



Thanked 36 times

Dream Kudos: 525
My Contributions


Well my first comment is that you are using conio.h. You have to remember that although these functions are neat, they are not standard and so your code may not work with different compilers (such as the one I am using).

Another note would be that you are using a variable named "try" -- this is fine in C but once you migrate to C++ then "try" becomes a reserved word.

You really should not use goto -- in a simple program like this it is not a terrible sin since you only have 1, but you should really learn to use the control structures because using many goto's is very difficult to maintain and forms what we call spaghetti code.

As for putting everything in main() -- when you first begin to program then putting everything into the main() function is Ok. However, as you move on you will find that your main() function becomes cluttered and difficult to maintain. You will find that modular programming (using functions) simplifies things and allows you to concentrate upon smaller chunks of code rather than having to keep the whole thing in your head at once.

Here is an example that may help:
cpp
#include <stdio.h>
#include <stdlib.h>

#define YES ('Y'-'A')
int doMenu(const char *menu);

int main() {
int ans;
do {
ans = doMenu("A:FIRST\nB:SECOND\nC:THIRD\nD:FOURTH");
switch(ans) {
case 0:
printf("User entered A\n");
break;
case 1:
printf("User entered B\n");
break;
case 2:
printf("User entered C\n");
break;
case 3:
printf("User entered D\n");
break;
default:
printf("**User entered: %c\n", 'A'+ans);
}
ans = doMenu("Continue (y/n):");
} while (ans == YES);
return 0;
}

int doMenu(const char *menu) {
char input;
do {
printf("\n%s\nPlease enter your choice: ", menu);
input = getche(); //Get an input.
input = toupper(input); //convert to upper case.
//Make sure the input is a letter...
} while (input < 'A' || input > 'Z');
putchar('\n');
return input - 'A';
}


This post has been edited by NickDMax: 19 Jul, 2008 - 09:40 AM
User is offlineProfile CardPM

Go to the top of the page

angel0lz
post 19 Jul, 2008 - 03:51 PM
Post #4


New D.I.C Head

*
Joined: 19 Jul, 2008
Posts: 8

thanks for the suggestions guys.. that's what our teacher said, creating functions... but im trying to figure it out since she still didnt discussed it for us.. ill try to browse from this site about making functions.. anyways, i really am glad that someone helped me.. ill post my code again.. thanksss guyss!! biggrin.gif
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 20 Jul, 2008 - 03:15 AM
Post #5


T3H R0XX0R!

Group Icon
Joined: 6 Feb, 2008
Posts: 3,382



Thanked 69 times

Dream Kudos: 2175

Expert In: (X)HTML, CSS, Batch Scripting, C, C++

My Contributions


An example of a function

Functions can return different variables, specified in the declaration:
Syntax: return_type name (parameters)
Example: int add (int x, int y);
Now if we add a function into our code, it is done like so:

cpp
#include <stdio.h>

int add (int x, int y); // function declaration
int main () {
printf ("%d",add(3,2)); // output the RETURN value of the function
return 0;
}
// function definition
// this is where we define the function
int add (int x, int y)
{ // x and y are the parameters. This means that
// they will take the values of whatever is passed to the function
// (see the function call in main for an example)
return x + y; // return the total of x and y
}

Hope this helps smile.gif
User is offlineProfile CardPM

Go to the top of the page

angel0lz
post 21 Jul, 2008 - 06:32 AM
Post #6


New D.I.C Head

*
Joined: 19 Jul, 2008
Posts: 8

i was like thinking this program is more on conditional statements and not on looping.. wat can you suggest for this program guys for it to have more loops other than do while statements? our teacher suggested that we should like move/animate the texts.. any suggestions guys on how to implement looping here?? because we really need to apply on conditional statements and looping, not only in conditional statements..
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 10/7/08 11:29PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month