Wrox Programmer Forums
|
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 November 2nd, 2006, 09:18 PM
Authorized User
 
Join Date: Oct 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mastrgamr
Default help with coding

can som1 tell me wats wrong with this code plz (im still learning and need to lear my mistakes)
here:
// mastrgamr's simple calculator!
#include <iostream.h>
void main()
{
 cout<<"to use my calculator follow the first command
 then u must hold shift and either A to add, S to subtract,
 M to multiply, or D to divide. insert another number and your
 answer should appear!"<< endl;
 float num2;
 float num1;
 char op;
 char (op==65 && op==83 && op==77 && op==68)
 float ans;
 cout << "insert number: ";
 cin >> num1;
 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;
 cin >> num2>> endl;
 cout << "=" << ans << endl;
}
i dont understand wat my CMD is talking about wen it anounces my errors

__________________
-Stuart Smith
Reply With Quote
  #2 (permalink)  
Old November 3rd, 2006, 05:35 AM
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

It'd be useful to see the error that is returned.

Without that my guess is that you problem is this line:

<code>
char (op==65 && op==83 && op==77 && op==68)
</code>

It doesn't end in a semi colon, so the syntax is wrong, and it doesn't do anything AFAICS, maybe just delete it?

--
Don't Stand on your head - you'll get footprints in your hair
                                           http://charlieharvey.org.uk
                                              http://charlieharvey.com
Reply With Quote
  #3 (permalink)  
Old November 3rd, 2006, 09:19 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 103
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Geo121
Default

Alright . . .

I took some time and your problem was primarily two things

char (op==65 && op==83 && op==77 && op==68)

and

you were trying to do the math before you accepted the input

~ Geo

 ~ You are unique, just like everyone else
Reply With Quote
  #4 (permalink)  
Old November 3rd, 2006, 09:22 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 103
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Geo121
Default

ciderpunx was right about deleting

char (op==65 && op==83 && op==77 && op==68)

because it doesn't really do anything

ya know?

I would keep it comment code just so you know what variables are supposed to be accepted when this code gets huge

so if you delete that line and you accept the second number before you do the math your code will be correct

~ Geo

 ~ You are unique, just like everyone else
Reply With Quote
  #5 (permalink)  
Old November 3rd, 2006, 09:24 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 103
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Geo121
Default

There is also the problem of declaring your main function as a void return value

You should always declare main as int because of the way the program returns

it should return a 0 or a 1

0 for complete or 1 for errors

Your code should look like this :

// mastrgamr's simple calculator!
#include <iostream.h>

int main()
{
     cout<<"to use my calculator follow the first command then u must hold shift and either A to add, S to subtract, M to multiply, or D to divide. insert another number and your answer should appear!"<< endl;
     float num2;
     float num1;
     char op;
     //char (op==65 && op==83 && op==77 && op==68) just keep this documentation it isn't required

     float ans;
     cout << "insert number: ";
     cin >> num1;
     cin >> op;
     cin >> num2;
     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 << "=" << ans << endl;

     return 0;
}


~ Geo

 ~ You are unique, just like everyone else
Reply With Quote
  #6 (permalink)  
Old November 3rd, 2006, 09:34 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 103
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Geo121
Default

Hopefully though you weren't going to be finished with this right?

A little cleaning and possibly more user friendly controls would help a lot

here are some suggestions

1) All of the ifs should be a switch (case) structure

2) Put the data accept inside of a while statement to prevent incorrect data entry
   (such as they enter P for 'plus' we have to ASSUME people are dumber than rocks)

3) Organize your instructions in the begining to create a clean appearance

4) Make your instructions verified ex : you said hold shift and enter a letter in caps but what if they have caps lock on and they enter a value while holding shift?

5) Regarding the caps only you can make the input data accept upper and lower case

6) I would probably use symbols for the operation identifier and present it to them

7) Create a more explanative output
   ex :
   =4
   that works but you should restate everything
   2 * 2 = 4
   see . . . thats what the user wants to see

8) Prompt for another math function
   ex:
   2 * 2 = 4
   would you like to perform another math operation?

9) possibly add more than those four math operations

Follow these and your program will be awesome beyond belief =P

Sorry if I seemed a little harsh

I am just trying to give you a hand in making the best program you can

~ Geo

 ~ You are unique, just like everyone else
Reply With Quote
  #7 (permalink)  
Old November 3rd, 2006, 10:03 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 103
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Geo121
Default

This is how I would do it only there would be more operations and a try catch exception handler

// geo121's simple calculator!
#include <iostream.h>

int main()
{
     cout << "\n\n\n\n\n\nWelcome to Geo121's Calculator\n\nThis Calculator is used to calculate various math operations\n\nWhen Prompted enter a value for the first number\n\nThen the symbol for the operation\n\nThen another number for the second value\n\nThe symbols for the operations are as followed : \n\n+ is used for addition\n\n- is used for subtraction\n\n* is used for multiplication\n\n/ is used for division\n\n\nMORE OPERATIONS TO COME\n\n";
     float num2;
     float num1;
     char op;
     // this documentation it isn't required
     // char op in ASCII
     // + = 43
     // - = 45
     // * = 42
     // / = 47

     float ans;
     cout << "Please Enter the First Number: ";
     cin >> num1;
     cout << "Please Enter the Operation Symbol: ";
     cin >> op;
     cout << "Please Enter the Second Number: ";
     cin >> num2;
     switch (op)
     {
          case 43: // operation symbol was + in ASCII is 43
               ans=num1+num2;
               break;
          case 45: // operation symbol was - in ASCII is 45
               ans=num1-num2;
               break;
          case 42: // operation symbol was * in ASCII is 42
               ans=num1*num2;
               break;
          case 47: // operation symbol was / in ASCII is 47
               ans=num1/num2;
               break;
          default: // operation symbol was invalid
               cout << "\n\n\nIncorrect Operation\n\n\n";
               break;
     }

     cout << num1 << " " << op << " " << num2 << " = " << ans << endl;

     return 0;
}





If you are new to programming in C++ I would suggest reading

learn C++ in 21 days

It actually takes less if you try and it also teaches some key computer programming concepts

such as code organization and memory management

~ Geo

 ~ You are unique, just like everyone else
Reply With Quote
  #8 (permalink)  
Old November 3rd, 2006, 10:04 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 103
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Geo121
Default

go ahead and copy the code above and compile it and see how it looks and how it is more user friendly

Don't worry You'll get better

=P

~ Geo

 ~ You are unique, just like everyone else
Reply With Quote
  #9 (permalink)  
Old November 3rd, 2006, 10:06 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 103
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Geo121
Default

P.S. Are you still interested in programming windowed applications?

If so have you looked up :

The Forger's Win32 API Tutorial??

~ Geo

 ~ You are unique, just like everyone else
Reply With Quote
  #10 (permalink)  
Old November 3rd, 2006, 08:14 PM
Authorized User
 
Join Date: Oct 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mastrgamr
Default

thanks for the help alot. yes i am a new programmer. and yes im looking forwaord to making windowed apps.

Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with coding Gini Visual Studio 2008 2 March 6th, 2009 04:14 PM
Coding Help sugunakar Classic ASP Professional 2 March 30th, 2008 04:42 PM
Help coding here Scoob PHP How-To 3 January 26th, 2006 11:18 AM
coding mdlan PHP Databases 1 May 28th, 2005 07:40 PM
can someone help me in coding this? somanchivasu Access 1 April 5th, 2004 02:41 PM





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