Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old January 19th, 2014, 04:43 AM
Registered User
 
Join Date: Jan 2014
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Simple C++ Calculator | Cant find the error! Where am i wrong?

SIMPLE C++ Calculator from the book "Beginning Programming" ...


Code:
// C++ Calculator
#include <iostream.h>
void main()
    {
      float num1;
      float num2;
      char op;
      float ans;
      cout << "Please enter a number: ";
      cin >> num1;
      cout << "Please enter another number: ";
      cin >> num2;
      cout << "Press A to add the two numbers." ;
           << endl
           << "Press B to subtract the two numbers"
           << endl
           << "Press C to multiply the two numbers"
           << endl
           << "Press D to divide the two numbers"
           << endl
      cin >> op;
      if (op == 65)
      	ans = num1 + num2;
      if (op == 83)
      	ans = num1 - num2;
      if (op == 77)
      	ans = num1 * num2;
      if (op == 68)
      	ans = num1 / num2;
      cout << "The answer is " << ans << endl;
     }
It dosent compile! Cant find the error! Please help me!
Reply With Quote
  #2 (permalink)  
Old January 23rd, 2014, 09:37 AM
bclark2137
Guest
 
Posts: n/a
Default

first, you should change:
#include <iostream.h>
to:
#include <iostream>
using namespace std;

also, this:
cout << "Press A to add the two numbers." ;
<< endl
<< "Press B to subtract the two numbers"
<< endl
<< "Press C to multiply the two numbers"
<< endl
<< "Press D to divide the two numbers"
<< endl

should read:
cout << "Press A to add the two numbers."
<< endl
<< "Press B to subtract the two numbers"
<< endl
<< "Press C to multiply the two numbers"
<< endl
<< "Press D to divide the two numbers"
<< endl;
if you'll notice, the semicolon must be added at the end of each statement, whereas you have one at the end of the first line.
You have one at the end of "Press A to add the two numbers.";
This would be correct if you then added:
cout <<
to the beginning of the following lines of code.

:-) hope that helps

Last edited by bclark2137; January 23rd, 2014 at 09:50 AM..
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Something went wrong and i am getting a error message. 800a03f3 joeIT34 VBScript 1 October 7th, 2011 02:48 AM
This simle simple xsl is not working...what's wrong with this? spring2009 XSLT 2 September 7th, 2010 02:15 PM
Error in script but can't find what's wrong!! BramuS ASP.NET 1.0 and 1.1 Basics 1 May 10th, 2005 04:32 AM
Error message is wrong Mitch SQL Server 2000 3 March 17th, 2004 07:11 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.