Thread: need assistance

  1. #1
    Unregistered
    Guest

    need assistance

    i know that it is not good to use goto but i just want to learn every single command that c programming has so please try not to flame a newbie.. thanx..

    anywayz i am tring to make this code work with the goto command but it is not working can anyone tell me why... thanx

    Code:
    #include <stdio.h>
    
    
    int main()
    {
     int x,y;
     char ok;
     char ch;
     
     startover:
      
        printf("What do you want to do?\n");
        printf("ADD, SUBTRACT, MULTIPLY OR DEVIDE\n");
        printf("Enter the first letter\n");
        ch = getchar();
        
        printf("Please enter your value\n");
        scanf("%d",&x);
         printf("Please enter your second value\n");
         scanf("%d",&y);
         
         if (ch == 'a') printf("The value is %d\n",x+y);
         if (ch == 'd') printf("The value is %d\n",x/y);
         if (ch == 'm') printf("The value is %d\n",x*y);
         if (ch == 's') printf("The value is %d\n",x-y);
        
              printf("Would you like to go again?\n");
              printf("Type in ok to go again or no to exit\n");
              scanf("c",&ok);
              
              if (ch == ok) goto startover;
              
              
                return 0;
               }
    please dont change my code cause i really dont know alot about c yet then i will get confused...

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It doesn't work because you're using goto.

    ...

    No, seriously, it doesn't work because:

    if (ch == ok) goto startover;

    "if ch == ok" ?? What?

    ch will equal whatever value you enter. Then later, you scan a new character into ok. That's like doing:

    Enter a letter:
    A

    Enter another letter:
    B

    if( letter1 == letter2) start over again

    See the problem? I think your problem is your if check is wrong.

    In your example, you say "Enter the first letter", that fills out 'ch'.

    Then you say "type OK to try again".

    This fills out the variable "ok".

    However, "ok" is just a single character.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Unregistered
    Guest
    thank you for your reply but how would i fix this i mean if ok for me to ask.. again thank you for you reply

  4. #4
    Unregistered
    Guest
    This
    scanf("c",&ok);
    should be
    scanf("%c",&ok);

    your 'if' should check the value of 'ok' with a value that will give a true or false result.

    eg
    if(ok == '\r')
    goto startover;

    if you press the enter key then TRUE and goto is executed, any other key and FALSE, it wont be.

    Just another way of doing your ifs

    instead of
    if (ch == 'a') printf("The value is %d\n",x+y);
    if (ch == 'd') printf("The value is %d\n",x/y);
    if (ch == 'm') printf("The value is %d\n",x*y);
    if (ch == 's') printf("The value is %d\n",x-y);
    you could do
    Code:
    switch(ch)
    {
       case 'a'  : printf("The value is %d\n",x+y);
                       break;
       case 'd'  : printf("The value is %d\n",x/y);
                       break;
       case 'm' : printf("The value is %d\n",x*y);
                       break;
       case 's'  : printf("The value is %d\n",x-y);
                       break;
       default   : printf("Invalid arithmetic function\n");
    }
    Hope this helps.
    Dav

  5. #5
    Unregistered
    Guest
    ok i have tried it that way but it just exits look this is how i have it ...btw i know i can use the swith statement but i am just tring to do it other ways.. thank you again

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
         int x,y;
         char o,n;
         char ch;
     
      startover:
      
                        printf("What do you want to do?\n");
                        printf("ADD, SUBTRACT, MULTIPLY OR DEVIDE\n");
                        printf("Enter the first letter\n");
             ch = getchar();
        
                        printf("Please enter your value\n");
             scanf("%d",&x);
                        printf("Please enter your second value\n");
             scanf("%d",&y);
         
         if (ch == 'a') printf("The value is %d\n",x+y);
         if (ch == 'd') printf("The value is %d\n",x/y);
         if (ch == 'm') printf("The value is %d\n",x*y);
         if (ch == 's') printf("The value is %d\n",x-y);
             
                        printf("Would you like to go again?\n");
                        printf("Type in o for ok to go again\n");
                        printf("\n");
                        
              o = getche();
              scanf("%c",&o);
              
              if (o == '\r') goto startover;
              
        }

  6. #6
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
         int x,y;
         char o,n;
         char ch;
     
      startover:
      
                        printf("What do you want to do?\n");
                        printf("ADD, SUBTRACT, MULTIPLY OR DEVIDE\n");
                        printf("Enter the first letter\n");
             ch = getchar();
        
                        printf("Please enter your value\n");
             scanf("%d",&x);
                        printf("Please enter your second value\n");
             scanf("%d",&y);
         
         if (ch == 'a') printf("The value is %d\n",x+y);
         if (ch == 'd') printf("The value is %d\n",x/y);
         if (ch == 'm') printf("The value is %d\n",x*y);
         if (ch == 's') printf("The value is %d\n",x-y);
             
                        printf("Would you like to go again?\n");
                        printf("Type in o for ok to go again\n");
                        printf("\n");
                        
              o = getche();
              /*scanf("%c",&o);*/
              
              if (o == 'o' || o == 'O') goto startover;
              
        }

  7. #7
    Unregistered
    Guest

    Angry

    damn.. its still not starting over..

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
         int x,y;
         char o,n;
         char ch;
     
      startover:
      
                        printf("What do you want to do?\n");
                        printf("ADD, SUBTRACT, MULTIPLY OR DEVIDE\n");
                        printf("Enter the first letter\n");
             ch = getchar();
        
                        printf("Please enter your value\n");
             scanf("%d",&x);
                        printf("Please enter your second value\n");
             scanf("%d",&y);
         
         if (ch == 'a') printf("The value is %d\n",x+y);
         if (ch == 'd') printf("The value is %d\n",x/y);
         if (ch == 'm') printf("The value is %d\n",x*y);
         if (ch == 's') printf("The value is %d\n",x-y);
             
                        printf("Would you like to go again?\n");
                        printf("Type in o for ok to go again\n");
                        printf("\n");
                        
              o = getche();
             
               if (o == 'o' || o == 'O') goto startover;
              
        }

  8. #8
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Advice:

    Throw away the goto's.

  9. #9
    Unregistered
    Guest
    ok if i was to take out the goto then how would i make it start over... ?

  10. #10
    Unregistered
    Guest
    There are many ways, this is just one.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
         int x,y;
         char o,n;
         char ch;
     
         do{
                   printf("What do you want to do?\n");
                   printf("ADD, SUBTRACT, MULTIPLY OR DEVIDE\n");
                   printf("Enter the first letter\n");
                   ch = getchar();
        
                   printf("Please enter your value\n");
                   scanf("%d",&x);
                   printf("Please enter your second value\n");
                   scanf("%d",&y);
         
                   if (ch == 'a') printf("The value is %d\n",x+y);
                   if (ch == 'd') printf("The value is %d\n",x/y);
                   if (ch == 'm') printf("The value is %d\n",x*y);
                   if (ch == 's') printf("The value is %d\n",x-y);
             
                   printf("Would you like to go again?\n");
                   printf("Type in o for ok to go again\n");
                   printf("\n");
                        
                   o = getche();
             }while(o=='o'||o=='O');
    
             return 0;
    }
    Now the program will only loop if o is valid ie. 'o' or 'O'

  11. #11
    Unregistered
    Guest
    ok i did everything you said but still it wont start over to give it another calculation.. see for your self..

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
         int x,y;
         char o,n;
         char ch;
     
      
                        printf("What do you want to do?\n");
                        printf("ADD, SUBTRACT, MULTIPLY OR DEVIDE\n");
                        printf("Enter the first letter\n");
             ch = getchar();
        
                        printf("Please enter your value\n");
             scanf("%d",&x);
                        printf("Please enter your second value\n");
             scanf("%d",&y);
         
         if (ch == 'a') printf("The value is %d\n",x+y);
         if (ch == 'd') printf("The value is %d\n",x/y);
         if (ch == 'm') printf("The value is %d\n",x*y);
         if (ch == 's') printf("The value is %d\n",x-y);
             
                        printf("Would you like to go again?\n");
                        printf("Type in o for ok to go again\n");
                        printf("\n");
                        
               o = getche();
               while(o == 'o' || o == 'O')
               return 0;
     
        }
    see everything is the same but it still wont start i wonder what is wrong... ?

  12. #12
    Unregistered
    Guest
    Cut and paste my code properly:

    I see no "do{" in your code.

    To create a loop, start with:

    [CODE]
    do{ //Anything that follows from here will be executed
    //Add some code
    }while(condition == TRUE);
    [CODE]

    In your case, the program loops until the condition is not valid
    ie. not equal 'o' or 'O'

  13. #13
    Unregistered
    Guest
    sorry i didnt add the do before but here it is.. btw thank you for your help i know this is becoming a pain

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
         int x,y;
         char o,n;
         char ch;
     
            do
            {
      
                        printf("What do you want to do?\n");
                        printf("ADD, SUBTRACT, MULTIPLY OR DEVIDE\n");
                        printf("Enter the first letter\n");
             ch = getchar();
        
                        printf("Please enter your value\n");
             scanf("%d",&x);
                        printf("Please enter your second value\n");
             scanf("%d",&y);
         
         if (ch == 'a') printf("The value is %d\n",x+y);
         if (ch == 'd') printf("The value is %d\n",x/y);
         if (ch == 'm') printf("The value is %d\n",x*y);
         if (ch == 's') printf("The value is %d\n",x-y);
             
                        printf("Would you like to go again?\n");
                        printf("Type in o for ok to go again\n");
                        printf("\n");
                        
               o = getche();
               }
               
               while(o == 'o' || o == 'O')
               return 0;
     
        }

  14. #14
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    The following code works for me. I added <conio.h>

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
         int x,y;
         char o;
         char ch;
     
            do
            {
      
                        printf("What do you want to do?\n");
                        printf("ADD, SUBTRACT, MULTIPLY OR DEVIDE\n");
                        printf("Enter the first letter\n");
             ch = getchar();
        
                        printf("Please enter your value\n");
             scanf("%d",&x);
                        printf("Please enter your second value\n");
             scanf("%d",&y);
         
         if (ch == 'a') printf("The value is %d\n",x+y);
         if (ch == 'd') printf("The value is %d\n",x/y);
         if (ch == 'm') printf("The value is %d\n",x*y);
         if (ch == 's') printf("The value is %d\n",x-y);
             
                        printf("Would you like to go again?\n");
                        printf("Type in o for ok to go again\n");
                        printf("\n");
                        
               o = getche();
               }
               while(o == 'o' || o == 'O');
               
    		   return 0;
     
        }

  15. #15
    Unregistered
    Guest
    GREAT! it works but how would i have know to add the conio.h file..?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hello,i need assistance in completing the code
    By toader in forum C++ Programming
    Replies: 1
    Last Post: 06-22-2009, 03:32 AM
  2. Variable Timer assistance
    By rich2852 in forum C Programming
    Replies: 2
    Last Post: 01-21-2009, 05:43 AM
  3. Any assistance would be greatly appreciated
    By iiwhitexb0iii in forum C Programming
    Replies: 18
    Last Post: 02-26-2006, 12:06 PM
  4. Need some more assistance
    By Thantos in forum Windows Programming
    Replies: 6
    Last Post: 08-14-2003, 12:13 PM
  5. Need assistance for major text base Arena RPG project
    By Ruflano in forum C++ Programming
    Replies: 0
    Last Post: 04-04-2002, 11:11 AM