Go Back   Forums > Community Chatterbox > Tech Corner > Programming
Memberlist Forum Rules Today's Posts
Search Forums:
Click here to use Advanced Search

Reply
 
Thread Tools Display Modes
Old 12-10-2010, 04:38 PM   #1
Fubb
GreatCanadianMan
 
Fubb's Avatar


 
Join Date: May 2008
Location: Swan River, Canada
Posts: 842
Default C++ Calculator Problem

Now im not quite sure WHY this isn't working, but on the bolded line, my code finds and error and doesn't want to build

Quote:
#include <iostream>
using namespace std;

int main(void)
{

system("TITLE Calculator");
system("COLOR 2");
char cChar; // char is a character, its how you get plus and minus signs, etc //
double dfirstnumber;
double dsecondnumber;
char cDoagain;

do
{
system ("CLS");
cout << "please enter the first number that you would like to use"
<< endl;
cin >> dfirstnumber;
cout << "please enter the operation that you want to complete"
<< " (+,-,*, or /)" << endl;
cin >> cChar;

}







{

switch (cChar)
}

{
case '+':
cout << "The answer is: " << dfirstnumber << " + " <<
dsecondnumber << " = " << (dfirstnumber + dsecondnumber)
<< endl;
break;
case '-':
cout << "The answer is: " << dfirstnumber << " - " <<
dsecondnumber << " = " << (dfirstnumber - dsecondnumber)
} << endl;
break;
case '*':
} cout << "The answer is: " << dfirstnumber << " * " <<
dsecondnumber << " = " << (dfirstnumber * dsecondnumber)
<< endl;
break;
case 'x':
cout << "The answer is: " << dfirstnumber << " x " <<
dsecondnumber << " = " << (dfirstnumber * dsecondnumber)
<< endl;
break;
case 'X':
cout << "The answer is: " << dfirstnumber << " X " <<
dsecondnumber << " = " << (dfirstnumber * dsecondnumber)
<< endl;
break;
case '/':
if(dsecondnumber == 0) {
cout << "that is an invalid operation" << endl;
}else{
cout << "The answer is: " << dfirstnumber << " / " <<
dsecondnumber << " = " << (dfirstnumber / dsecondnumber)
<< endl;
}
break;
default:
cout << "That is an invalid operation" << endl;
break;
}
cout << "would you like to start again? (y or n)" << endl;
cin >> cDoagain;
while (cDoagain == 'Y' || cDoagain == 'y');
system("PAUSE");




/*system("pause") causes the program to stay open, but isnt necesary */
/* in programs like visual basic, */
/*int main(void) initialises main coding section*/
/*Two types of number variables, one is an integer or int, and one is a double*/
/*The integer deals with full numbers while the double deals with decimals */
/* d needs to be in front of dnumber because the d is a variable for double */
/* you must have 0.0 as your opening numbers or else it will not work properly */
Ignore the comments at the end. at the function opening bracket on the line before switch (cChar) is keeps giving me an error, any ideas?
__________________
Kugarfang: o hai guiz im trying to find this techno song from the radio and it goes like this:

DUN duuuunnnn dudududududun SPLOOSH duuunnnnn


We ate the horse.
Fubb is offline                         Send a private message to Fubb
Reply With Quote
Old 12-10-2010, 05:21 PM   #2
The Fifth Horseman
FUTURE SCIENCE BASTARD
 
The Fifth Horseman's Avatar


 
Join Date: Oct 2004
Location: Opole, Poland
Posts: 14,276
Default

Your code has a lot of the { } brackets where not neccessary. They're messing up the whole thing.
Further, you're never inputting the value for "dsecondnumber";
Further, whoever you have those comments from... don't trust them on anything relating to programming. EVER. Again.
int main(int argc, char* argv[]) is the proper form if you want arguments. int main() if you don't.
Do yourself a favor, and replace that ridiculous case with some else if 's.

Quote:
/*Two types of number variables, one is an integer or int, and one is a double*/
No.
Int comes in no less than four subtypes: __int8, __int16, __int32, __int64, and each of those can also be unsigned, for a total of eight types. the usual int is the same as __int32, BTW.
There's also the bool type, which can also be used in the same fashion as an integer.
There's two types of floating-point values: float and double.
Quote:
// char is a character, its how you get plus and minus signs, etc //
Char is stored in memory as a hexadecimal representation of the character, with a value corresponding to a given ASCII code. See: http://www.asciitable.com/
Unicode chars are stored in two bytes, IIRC, while regular Ascii takes up just one byte (same as __int8)
Try incrementing or decrementing a char and see what happens.
Char can be unsigned too, BTW.
Quote:
/* d needs to be in front of dnumber because the d is a variable for double */
Incorrect. It would work just the same if you named it lulz .
__________________

"God. Can't you people see I'm trying to commit a crime against science and nature here?"
-- Reed Richards

Last edited by The Fifth Horseman; 12-10-2010 at 05:52 PM.
The Fifth Horseman is offline                         Send a private message to The Fifth Horseman
Reply With Quote
Old 12-10-2010, 05:52 PM   #3
The Fifth Horseman
FUTURE SCIENCE BASTARD
 
The Fifth Horseman's Avatar


 
Join Date: Oct 2004
Location: Opole, Poland
Posts: 14,276
Default

Quick and dirty fix just to get it working:
Code:
#include <iostream>
using namespace std;

int main()
{
system("TITLE Calculator");
system("COLOR 2");
char cChar, cDoagain; // char is a character, its how you get plus and minus signs, etc //
double dfirstnumber, dsecondnumber;

do { system ("CLS");
     cout << "please enter the first number that you would like to use\n";
     cin >> firstnumber >> secondnumber;
     cout << "please enter the operation that you want to complete: (+,-,*, or /)\n";
     cin >> cChar;
     switch (cChar){case '+':{ cout << "The answer is: " << dfirstnumber << " + " << dsecondnumber << " = " << (dfirstnumber + dsecondnumber) << endl;
                               break;}
                    case '-':{ cout << "The answer is: " << dfirstnumber << " - " << dsecondnumber << " = " << (dfirstnumber - dsecondnumber) << endl;
                               break;}
                    case '*':{ cout << "The answer is: " << dfirstnumber << " * " << dsecondnumber << " = " << (dfirstnumber * dsecondnumber) << endl;
                               break;}
                    case 'x':{ cout << "The answer is: " << dfirstnumber << " x " << dsecondnumber << " = " << (dfirstnumber * dsecondnumber) << endl;
                               break;}
                    case 'X':{ cout << "The answer is: " << dfirstnumber << " X " << dsecondnumber << " = " << (dfirstnumber * dsecondnumber) << endl;
                               break;}
                    case '/':{ if(dsecondnumber == 0) cout << "that is an invalid operation" << endl;
                               else cout << "The answer is: " << dfirstnumber << " / " << dsecondnumber << " = " << (dfirstnumber / dsecondnumber) << endl;
                               break;}
                    default:{ cout << "That is an invalid operation" << endl; break; } }
     cout << "would you like to start again? (y or n)" << endl;
     cin >> cDoagain; } while (cDoagain == 'Y' || cDoagain == 'y');
     system("PAUSE"); }
__________________

"God. Can't you people see I'm trying to commit a crime against science and nature here?"
-- Reed Richards

Last edited by The Fifth Horseman; 12-10-2010 at 06:17 PM.
The Fifth Horseman is offline                         Send a private message to The Fifth Horseman
Reply With Quote
Old 12-10-2010, 06:28 PM   #4
Japo
Autonomous human
 
Japo's Avatar


 
Join Date: Mar 2006
Location: ,
Posts: 4,613
Default

Yes, I wonder how you decided to put them. You need to review C syntax.

http://msdn.microsoft.com/en-us/library/66k51h7a.aspx

Also, why do you use system() so much? Instead of system("pause") you should use getchar() or similar, so the same thing is done by the program itself, instead of having to call an OS command. And what is the purpose of

Code:
system("TITLE Calculator");
system("COLOR 2");
? Are those actual executable files you have? Or are you trying to print those strings?

And you should import the header files with the prototypes for the functions you're using.
__________________
Life starts every day anew. Prospects not so good...
Japo is offline                         Send a private message to Japo
Reply With Quote
Old 12-10-2010, 07:23 PM   #5
Japo
Autonomous human
 
Japo's Avatar


 
Join Date: Mar 2006
Location: ,
Posts: 4,613
Default

Quote:
Originally Posted by The Fifth Horseman View Post
Do yourself a favor, and replace that ridiculous case with some else if 's.
I disagree, the use of a switch statement is precisely this. Its implementation is supposed to be optimized for comparing an expression to more than one constant expressions. On the other hand nested if/else if/else... may need many evaluations and processor instructions until they hit the target. Both switch and if have their uses.

Moreover, the reason why you have to put break statements, is because otherwise execution would fall through. BUT this can work in your favour, see this:

Code:
   switch(cChar)
   {
      case '+':
         cout << "The answer is: " << dfirstnumber << " + " << dsecondnumber << " = " << (dfirstnumber + dsecondnumber) << endl;
         break;
      case '-':
         cout << "The answer is: " << dfirstnumber << " - " << dsecondnumber << " = " << (dfirstnumber - dsecondnumber) << endl;
         break;
/*************/
      case '*':
      case 'x':
      case 'X':
         cout << "The answer is: " << dfirstnumber << " X " << dsecondnumber << " = " << (dfirstnumber * dsecondnumber) << endl;
         break;
/*************/
      case '/':
         if(dsecondnumber == 0)
            cout << "that is an invalid operation" << endl;
         else
            cout << "The answer is: " << dfirstnumber << " / " << dsecondnumber << " = " << (dfirstnumber / dsecondnumber) << endl;
         break;
      default:
         cout << "That is an invalid operation" << endl;
         break;
   }
This way you have one single piece of code for doing one single thing, as it should be. If you have to change that code in the future, and it's repeated in several places, you may introduce errors or discrepancies if you fail to update all of the instances in the same exact way.

By the way, you don't need to enclose each case group into brackets. (That defines a scope, which you can do anywhere, if you want variables declared within not to be accessible without.) switch() causes a jump to the matching case, and then execution continues, falling through any intermediate case and default labels, until a break (or return etc.) statement is found.

This is special about the switch statement. if (and for, while...) statements do need brackets, if you want more than one statement (line) into them. Above I disposed of the brackets in the if/else, but only because there's only one line for if and another for else. And again a pair of brackets defines a scope, so if you declare a variable within the brackets of an if (or for...) statement, it won't be defined once the program exits the closing bracket.

The C/C++ types are not the same across platforms, which is undesirable, but changing it now would break the standard. Depending on the platform, some types may have the same size, for example in 32-bit x86 int and long, or double and long double. Some non-standard extensions (Microsoft etc.) define custom types that are platform-invariant, such as those Horseman mentioned. But there are no subtypes, for example in 32-bit x86 __int32 is equivalent to both int and long; __int16 to short; and __int8 to char. The basic standard types are: char, short, int, long, double, long double. The integers (char, short, int and long) can be unsigned (which doubles their range in absolute value, since the bit usually reserved to keep the sign is saved for another significant binary digit).

I don't quite understand the final comments either, let alone the mention of Visual Basic. The name of a variable isn't restricted by its type--simply use self-documenting names that are immediate and intuitive to understand to you and anyone else. Some Visual Basic 6 and earlier coders did like to tag variable names with type prefixes, but that isn't required by the language, its purpose was only making the code more readable.
__________________
Life starts every day anew. Prospects not so good...

Last edited by Japo; 13-10-2010 at 04:16 PM.
Japo is offline                         Send a private message to Japo
Reply With Quote
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with XP SkiFree Troubleshooting 4 16-03-2010 02:42 PM
I Have A Problem! kevinitzac General compatibility fixes 1 22-04-2007 03:49 PM
Problem Dark Piedone Tech Corner 12 02-01-2006 07:22 PM
Dos Box Problem lost guy Troubleshooting 1 10-12-2004 09:14 AM
Dos Problem Anonymous Troubleshooting 13 25-09-2004 02:08 AM


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump
 


The current time is 11:29 AM (GMT)

 
Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.