Results 1 to 5 of 5
  1. #1

    Default i need your help in c++


    Guys need nku inyo tabang unsaun di pg input ug arithmethic operator in a running c++ program ? dba if number or something ang e input mugmit man ta ug "cin >> " pero what if arithmetic operator na ang dpat e butang labi na and modulos % ng libog ko
    heres an example na dpat mu gwas sa program:
    Enter n1: 5
    Enter n2: 4
    Enter opt: +
    -----------------
    The sum is: 9

    Enter n1: 5
    Enter n2: 2
    Enter opt: %
    ----------------
    The remainder is: 1

    note: i am using an switch statements (case) and i am using visual basic 2010 c++

  2. #2

  3. #3
    Quote Originally Posted by fritzhahaha View Post
    i am using visual basic 2010 c++
    this is..., zero divide ^_^





    'cin' can be versatile bro, all you need is the variable unsa klase. the problem of C/C++ is its very explicit in terms of its input but..., you can mitigate this with other tricks if you know.

    anyways, here is a bogo/crude way of doing what you wanted ^_^
    it is advised unahon nimo ang 'operator' kay mulabad imo ulo if ma-ulahi na siya ^_^
    note: wala ni siya security/bounds/etc checking...

    Code:
    char opt = '0';
        
        cout << "Enter opt: ";
        cin >> opt;
       
       switch(opt)
       {
           case '+':
           {   
               int n1 = 0, n2 = 0;
                cout << "Enter n1: ";
                cin >> n1;
       
                cout << "Enter n2: ";
                cin >> n2;
                   
                cout << "-----------------" << endl; 
                cout << "The sum is: " << n1 + n2 << endl;
           }; break;
           
           case '-':
           {     
                 int n1 = 0, n2 = 0;
                cout << "Enter n1: ";
                cin >> n1;
       
                cout << "Enter n2: ";
                cin >> n2;
                   
                cout << "-----------------" << endl;       
                cout << "The diference is: " << n1 - n2 << endl;
           }; break;
           
           case '*':
           {   
               int n1 = 0, n2 = 0;
                cout << "Enter n1: ";
                cin >> n1;
       
                cout << "Enter n2: ";
                cin >> n2;
                   
                cout << "-----------------" << endl;         
                cout << "The product is: " << n1 * n2 << endl;
           }; break;
           
           case '/':
           {    
                float n1 = 0, n2 = 0;
                cout << "Enter n1: ";
                cin >> n1;
       
                cout << "Enter n2: ";
                cin >> n2;
                   
                cout << "-----------------" << endl;        
                cout << "The quotient is: " << n1 / n2 << endl;
           }; break;
           
           case '%':
           {   
               int n1 = 0, n2 = 0;
                cout << "Enter n1: ";
                cin >> n1;
       
                cout << "Enter n2: ";
                cin >> n2;
                   
                cout << "-----------------" << endl;         
                cout << "The modulo is: " << n1 % n2 << endl;
           }; break;
           
           default:
           {
               cout << "unknown" << endl;
           }       
       }

  4. #4
    C.I.A. Sarevok's Avatar
    Join Date
    Apr 2007
    Gender
    Male
    Posts
    2,802
    Blog Entries
    4
    Quote Originally Posted by dab_s_bad View Post
    this is..., zero divide ^_^





    'cin' can be versatile bro, all you need is the variable unsa klase. the problem of C/C++ is its very explicit in terms of its input but..., you can mitigate this with other tricks if you know.

    anyways, here is a bogo/crude way of doing what you wanted ^_^
    it is advised unahon nimo ang 'operator' kay mulabad imo ulo if ma-ulahi na siya ^_^
    note: wala ni siya security/bounds/etc checking...
    ato limpyohan gamay ang code


    Code:
       char opt = '0';
       double n1 = 0;
       double n2 = 0;
        
       cout << "Enter n1: ";
       cin >> n1;
       
       cout << "Enter n2: ";
       cin >> n2;
        
       cout << "Enter operator: ";
       cin >> opt;
       
       cout << "-----------------" << endl; 
    
       switch(opt)
       {
           case '+':            
               cout << "The sum is: " << (n1 + n2) << endl;
               break;
           
           case '-':
               cout << "The diference is: " << (n1 - n2) << endl;
               break;
           
           case '*': 
               cout << "The product is: " << (n1 * n2) << endl;
               break;
           
           case '/':
               cout << "The quotient is: " << (n1 / n2) << endl;
               break;
           
           case '%': 
               cout << "The modulo is: " << (n1 % n2) << endl;
               break;
           
           default:
               cout << "unknown" << endl;
               break;      
       }

  5. #5
    salamat sa idea bro nasolve nman pud nku nuon sa kdugay nku hinuktok hahaha, instructor nku gud way uso discussion lol
    salamat sa ni post dri lol

  6.    Advertisement

Similar Threads

 
  1. Replies: 12
    Last Post: 12-13-2013, 04:59 PM
  2. Need your Help in buying brand new motorcycle
    By malaya^ in forum General Discussions
    Replies: 16
    Last Post: 06-20-2013, 07:35 PM
  3. Replies: 12
    Last Post: 03-21-2012, 07:54 PM
  4. Is there any lawyers in the house? I need your help!
    By peewee_toot in forum General Discussions
    Replies: 0
    Last Post: 08-27-2010, 11:25 AM
  5. mga bro's i need your help..pls come in..
    By nirpz in forum Networking & Internet
    Replies: 14
    Last Post: 03-15-2008, 12:45 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top