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

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




ERROR IN PROGRAME

 
Reply to this topicStart new topic

ERROR IN PROGRAME, CALCULATER

azeem
17 Dec, 2007 - 09:47 AM
Post #1

New D.I.C Head
*

Joined: 17 Dec, 2007
Posts: 1


My Contributions
please help me this how i can use do while condition on this programe when the programe ask (do you want to continue Y\N) when i prees y the programe is going in main menue and how i can add (sientific function on this coding)

CODE

main()
{
unsigned int a,b,c,total;
int d,e;
float tot;
char ch='y';
clrscr();
while(ch=='y')
{
printf("\n\n\n\t\t                FEDERAL URDU UNIVERSTY");
printf("\n\t\t/*/*/*/*/*/*/*/*/* MAIN MENUE/*/*/*/*/*/*/*/*/*/*/");
printf("\n\n\t PRESS 1 FOR ADDITION",a);
printf("\n\n\t PRESS 2 FOR SUBTRACTION",a);
printf("\n\n\t PRESS 3 FOR MULTIPLICATION",a);
printf("\n\n\t PRESS 4 FOR DIVISION",a);
printf("\n\n\t PRESS 5 FOR MAIN MENUE",a);
printf("\n\n\n\t ENTER UR CHOICE\t>>>>",a);
scanf("%d",&a);
if(a==1)
      {printf("\n\n\t!**********U CHOSE THE ADDITION*********!");
       printf("\n\n ENTER 1ST NUMBER\t>>>>");
       scanf("\n %d",&b);
       printf("\n\n ENTER 2ND NUMBER\t>>>>");
       scanf("\n%d",&c);
       total=b+c;
       printf("\n\nREAULT=>\t%d+%d=%d",b,c,total);
       printf("\n ----------");
       printf("\n\n\n\t\t DO U WANT TO CONTINUE(Y/N)");
       ch=getche();
       clrscr();}
else
{
  if(a==3)
       {printf("\n\n\t!***************** U CHOSE THE MULTIPLICATION*****************");
    printf("\n\n ENTER 1ST NUMBER>>>>\t");
    scanf("\n %d",&b);
    printf("\n\n ENTER 2ND NUMBER>>>>\t");
    scanf("\n%d",&c);
    total=b*c;
      printf("\n\n RESULT=>\t%d*%d=%d",b,c,total);
      printf("\n-------------");
        printf("\n\n\t\t DO U WANT TO CONTINUE(Y/N)");
        printf("\n\t\t ??????????????????????????");ch=getche();
        clrscr();}
else
{
   if(a==2)
     {printf("\n\n\t!**************** U CHOSE THE SUBTRACTION*****************!");
      printf("\n\n ENTER 1ST NUMBER>>>>\t");
      scanf("\n %d",&b);
      printf("\n\n ENTER 2ND NUMBER>>>>\t");
      scanf("\n%d",&c);
      total=b-c;
        printf("\n\n RESULT=>\t%d-%d=%d",b,c,total);
        printf("\n------------- ");
        printf("\n\n\t\t DO U WANT TO CONTINUE(Y/N)");
        printf("\n\t\t ???????????????????????????");ch=getche();
        clrscr();}
else
{
  if(a==4)
    { printf("\n\n\t!**************U CHOSE THE DIVISION****************!");
      printf("\n\n ENTER 1ST NUMBER>>>\t");
      scanf(" %d",&d);
      printf("\n\n ENTER 2ND NUMBER>>>>\t");
      scanf("%d",&e);
      tot=(float) d/e;
        printf("\n\n RESULT=>\t%d/%d=%f",d,e,tot);
        printf("\n---------");
        printf("\n\n\t\t DO U WANT TO CONTINUE(Y/N)");
        }
else
printf("\n\n\t\tPRESS 'N FOR EXIT'");
ch=getche(); clrscr();
}
}
}
}
}

User is offlineProfile CardPM
+Quote Post

Pontus
RE: ERROR IN PROGRAME
17 Dec, 2007 - 12:00 PM
Post #2

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 544



Thanked: 4 times
Dream Kudos: 275
My Contributions
easy
CODE

if(ch=='N')
break;

Break is the command to terminate the loop where it is placed in.

This post has been edited by manhaeve5: 17 Dec, 2007 - 12:00 PM
User is offlineProfile CardPM
+Quote Post

dacoder
RE: ERROR IN PROGRAME
6 Jan, 2008 - 12:24 AM
Post #3

New D.I.C Head
*

Joined: 6 Nov, 2007
Posts: 5


My Contributions
you could also consider using the switch statement. that way your condition of ch==y won't have to change and you can check to see a user doesnt input a correct option or not easier.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: ERROR IN PROGRAME
6 Jan, 2008 - 06:01 AM
Post #4

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,281



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Your if then else logic may not be what you think it is. It currently looks like this:
CODE

while(ch=='y') {
    if(a==1) {
    } else {
        if(a==3) {
        } else {
            if(a==2) {
            } else {
                if(a==4) {
                } else {
                }
            }
        }
    }
}


You probably want it like this:
CODE

while(ch=='y') {
    if(a==1) {
    } else if(a==3) {
    } else if(a==2) {
    } else if(a==4) {
    } else {
    }
}


While its basically the same, the second convention is easier to maintain. wink2.gif

Now, just for functions sake, you do the following three times:
CODE

printf("\n\n ENTER 1ST NUMBER\t>>>>");
scanf("\n %d",b);
printf("\n\n ENTER 2ND NUMBER\t>>>>");
scanf("\n%d",c);


If you were to make a function for that, your code might be easier to read. Functions can also make code blocks easier to follow for non repetitive tasks. Here's my version of your code:
CODE

#include <stdio.h>
#include <stdlib.h>

int menu() {
    int a;
    printf("\n\n\n\t\t                FEDERAL URDU UNIVERSTY");
    printf("\n\t\t/*/*/*/*/*/*/*/*/* MAIN MENUE/*/*/*/*/*/*/*/*/*/*/");
    printf("\n\n\t PRESS 1 FOR ADDITION",a);
    printf("\n\n\t PRESS 2 FOR SUBTRACTION",a);
    printf("\n\n\t PRESS 3 FOR MULTIPLICATION",a);
    printf("\n\n\t PRESS 4 FOR DIVISION",a);
    printf("\n\n\t PRESS 5 FOR MAIN MENUE",a);
    printf("\n\n\n\t ENTER UR CHOICE\t>>>>",a);
    scanf("%d",&a);
    return a;
}

void getNumbers(unsigned int *b, unsigned int *c) {
    printf("\n\n ENTER 1ST NUMBER\t>>>>");
    scanf("\n %d",b);
    printf("\n\n ENTER 2ND NUMBER\t>>>>");
    scanf("\n%d",c);
}

main() {
    int a;
    unsigned int b,c,total;
    int d,e;
    float tot;
    char ch='y';
    while(ch=='y') {
        a = menu();
        if(a==1) {
            printf("\n\n\t!**********U CHOSE THE ADDITION*********!");
            getNumbers(&b, &c);
            total=b+c;
            printf("\n\nREAULT=>\t%d+%d=%d",b,c,total);
        } else if(a==3) {
            printf("\n\n\t!***************** U CHOSE THE MULTIPLICATION*****************");
            getNumbers(&b, &c);
            total=b*c;
            printf("\n\n RESULT=>\t%d*%d=%d",b,c,total);
        } else if(a==2) {
            printf("\n\n\t!**************** U CHOSE THE SUBTRACTION*****************!");
            getNumbers(&b, &c);
            total=b-c;
            printf("\n\n RESULT=>\t%d-%d=%d",b,c,total);
        } else if(a==4) {
            printf("\n\n\t!**************U CHOSE THE DIVISION****************!");
            printf("\n\n ENTER 1ST NUMBER>>>\t");
            scanf(" %d",&d);
            printf("\n\n ENTER 2ND NUMBER>>>>\t");
            scanf("%d",&e);
            tot=(float)d / (float)e;
            printf("\n\n RESULT=>\t%d/%d=%f",d,e,tot);
        }
        printf("\n ----------");
        printf("\n\n\n\t\t DO U WANT TO CONTINUE(Y/N)");
        scanf(" %c", &ch );
    }
    return 0;
}


Hope this helps.

User is online!Profile CardPM
+Quote Post

Amadeus
RE: ERROR IN PROGRAME
6 Jan, 2008 - 07:36 AM
Post #5

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,355



Thanked: 51 times
Dream Kudos: 25
My Contributions
One final word of advice - when asking for numbers for division, I'd check to ensure your denominator is not 0. If it is, unpleasantness will follow.
User is online!Profile CardPM
+Quote Post

Tom9729
RE: ERROR IN PROGRAME
6 Jan, 2008 - 09:04 AM
Post #6

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,591



Thanked: 12 times
Dream Kudos: 325
My Contributions
Your main method should specify an int return type. smile.gif

CODE

int main()
{
    return 0;
}

User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 05:54PM

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