User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,593 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,705 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 1772 | Replies: 15
Reply
Join Date: Sep 2005
Posts: 102
Reputation: Niklas is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
Niklas's Avatar
Niklas Niklas is offline Offline
Junior Poster

Combining multiple programs...activating under anb ift statement??

  #1  
Sep 22nd, 2005
Okay Ive made a bunch of single use calculator programs (one for addition, subtraction, mutiplication, and division).

Addition Code
#include <iostream>
using namespace std;

int main()
{
     cout << "Please enter 2 numbers to be added "; 
     int a;
     int b;
     cin >> a;
     cout << "\n num1: " << a << endl;
     cin >> b;
     cout << " \n num2: " << b << endl;
     cout << " \n a + b = " << a + b << endl;
     
     cout << "Press the enter key to exit";
     cin.ignore(numeric_limits<streamsize>::max(), '\n');
     cin.get();
     return 0;
}

Subtraction
// Division Calculator
#include <iostream>
using namespace std;

int main()
{
    cout << "Please enter two numbers to be divided: ";
    int a;
    int b;
    
    cin >> a;
    cout << "\n num1: " << a << endl;
    cin >> b;
    cout << "\n num2: " << b << endl;
    
    cout << "\n a / b = " << a / b << endl;
    
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cin.get();
    
    return 0;
}

Multiplication
//Multiplication Calculator
#include <iostream>
using namespace std;

int main()
{
    cout << "Please enter two numbers to be multiplied: ";
    int a;
    int b;
    
    cin >> a;
    cout << "\n num1: " << a << endl;
    cin >> b;
    cout << "\n num2: " << b << endl;
    
    cout << "\n a x b = " << a * b << endl;
    
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cin.get();
    
    return 0;
}

Division
// Subtraction Calculator
#include <iostream>
using namespace std;

int main()
{
    cout << "Please enter 2 numbers to be subtractred";
    int a;
    int b;
    
    cin >> a;
    cout << "\n num1: " << a << endl;
    
    cin >> b;
    cout << "\n num2: " << b << endl;
    
    cout << "\n a - b = " << a - b << endl;
    cout << "Press the enter key to exit";
    
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cin.get();
    
    return 0;
}

My question is how do I combine these all into one program. Id ask please enter + - * or /. And then make an if statement ....if "symbol" ==x multiply. Is that the right logic or is there a much easier way to do this. And how would I right that if statement. Thanks youve helped me a lot lately guys. -Nick
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,017
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 414
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Combining multiple programs...activating under anb ift statement??

  #2  
Sep 22nd, 2005
The only difference is asking for an operator too:
#include <iostream>

using namespace std;

int main()
{
  int a, b;
  char op;

  cin>> a >> ws >> op >> b;

  if ( op == '+' )
    cout<< a + b <<'\n';
  else if ( op == '-' )
    cout<< a - b <<'\n';
  else if ( op == '*' )
    cout<< a * b <<'\n';
  else if ( op == '/' )
    cout<< a / b <<'\n';
  else
    cerr<<"Invalid operation\n";
}
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: Sep 2005
Posts: 102
Reputation: Niklas is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
Niklas's Avatar
Niklas Niklas is offline Offline
Junior Poster

Re: Combining multiple programs...activating under anb ift statement??

  #3  
Sep 22nd, 2005
Thanks a lot Narue.
Reply With Quote  
Join Date: Sep 2005
Posts: 102
Reputation: Niklas is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
Niklas's Avatar
Niklas Niklas is offline Offline
Junior Poster

Re: Combining multiple programs...activating under anb ift statement??

  #4  
Sep 22nd, 2005
#include <iostream>
using namespace std;

int main()
{
    int a;
    int b;
    char op;
    
    cout << "Please enter one number thne press enter then enter the second number";
    cin >> a;
    cout << "\n Num1: " << a << endl;
    cin >> b;
    cout << "\n Num2: " << b << endl;
    cout << "\n Now enter + to add....- to subtract....* to multiply....or / to divide";
    cin >> op;
    
    if ( op == "+" )
       cout << a + b << '\n;
    else if  ( op == "-")
       cout << a - b << '\n;
    else if ( op == "*" )
       cout << a * b << '\n;
    else if ( op == "/" )
       cout << a / b << '\n;
       else cout << "Invalid Operation\n";
    


Well heres my code and Im getting a lot of errors.
1.
IM not sure if theres the quotation marks or the thing on the same key around the operation sign?
if ( op == "+" )
       cout << a + b << '\n;
Reply With Quote  
Join Date: Feb 2005
Posts: 461
Reputation: winbatch is on a distinguished road 
Rep Power: 4
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Combining multiple programs...activating under anb ift statement??

  #5  
Sep 22nd, 2005
If op is a single char, then you need to compare with single quotes '+'
Reply With Quote  
Join Date: Sep 2005
Posts: 102
Reputation: Niklas is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
Niklas's Avatar
Niklas Niklas is offline Offline
Junior Poster

Re: Combining multiple programs...activating under anb ift statement??

  #6  
Sep 23rd, 2005
im really new to C++ and I never used char to declare a variable. I always used int but that wont work now cause int only works for numbers. Now what do you mean by char op; being a single char

BTW does char stand for character
Reply With Quote  
Join Date: Sep 2005
Posts: 102
Reputation: Niklas is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
Niklas's Avatar
Niklas Niklas is offline Offline
Junior Poster

Re: Combining multiple programs...activating under anb ift statement??

  #7  
Sep 23rd, 2005
#include <iostream>
using namespace std;

int main()
{
    int a;
    int b;
    char op;
    
    cout << "Please enter one number thne press enter then enter the second number";
    cin >> a;
    cout << "\n Num1: " << a << endl;
    cin >> b;
    cout << "\n Num2: " << b << endl;
    cout << "\n Now enter + to add....- to subtract....* to multiply....or / to divide";
    cin >> op;
    
    if ( op == '+' )
       cout << a + b << endl;
    else if  ( op == '-')
       cout << a - b << endl;
    else if ( op == '*' )
       cout << a * b << endl;
    else if ( op == '/' )
       cout << a / b << endl;
       else cout << "Invalid Operation\n";
  
     cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cin.get();
    

    return 0;
}

K Thans for all the help guys I dont know what I would do without you. One question though Narue in the code snippet u gave me it was '\n; after the if statment. however I got like 4 compiler errors when trying to compile it so I jsut changed it to endl;......What does the '\n; do and why did my compiler say it was an error?
Reply With Quote  
Join Date: Sep 2004
Posts: 6,017
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 414
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Combining multiple programs...activating under anb ift statement??

  #8  
Sep 23rd, 2005
>BTW does char stand for character
Yes.

>What does the '\n; do and why did my compiler say it was an error?
'\n' is an escape character that means newline. It's basically does the same thing as pressing Return on your keyboard. You shouldn't be getting errors with it though, especially since endl uses it for half of the operation:
template <typename T, typename Traits>
basic_ostream<T, Traits>& endl ( basic_ostream<T, Traits>& out )
{
  out.put ( out.widen ( '\n' ) );
  out.flush();
}
Notice that endl also flushes the stream, which can be expensive if done often. That's why I prefer using '\n' explicitly and let the C++ flush the stream for me except when I really need it. I would imagine that something else was causing the problem, but you can use this test program to see:
#include <iostream>

int main()
{
  std::cout<< 12345 <<'\n';
}
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: Sep 2005
Posts: 102
Reputation: Niklas is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
Niklas's Avatar
Niklas Niklas is offline Offline
Junior Poster

Re: Combining multiple programs...activating under anb ift statement??

  #9  
Sep 23rd, 2005
basic_ostream<T, Traits>& endl ( basic_ostream<T, Traits>& out )

gives me an invalid syntax error.
Reply With Quote  
Join Date: Sep 2004
Posts: 6,017
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 414
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Combining multiple programs...activating under anb ift statement??

  #10  
Sep 23rd, 2005
>gives me an invalid syntax error.
You're not meant to understand what's going on with that function or try to compile it. That code was strictly explanatory for future reference (and yes, it is correct. You're just trying to compile it incorrectly). The second example was for you to compile so that we could figure out what you're doing wrong so that '\n' fails to compile.
Member of: Beautiful Code Club.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:28 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC