1. ## Calculating Error

This is my (Yehaa!! My first, more than 20 lines of code containing, more than 90% working) finished program.
It is a calculator, I made it just to practise. BUT!! it doesn't work!
Well, it does, but the outcome is 0!! No matter what I try, the outcome is 0 NULL ZERO!! How is this possible! I coun't find out why. Here is the source code, please tell me what I am doing wrong.
Code:
```#include <iostream>     //For cout and other basic stuff
#include <string.h>     //For char and string
#include <stdlib.h>     //For system pause.(I don't know if including headers uses memory, but it would be a waste of space if they would cuz I don't really need this one)

int main()

{
char confirm[2];

while (strcmpi("q", confirm))

{
cout<<"Druk op Q en dan op enter om het programma te beeindigen, druk op enter om door te gaan.\n"; //Press Q and than enter to quit, press enter to continue.

cin.getline(confirm, 2, '\n');

if (!strcmpi("q", confirm))
{
return 0;
}
else
{
char input[2];

cout<<"Wat wil je doen?? Twee cijfers: \nA: optellen.\nB: aftrekken.\nC: vermenigvuldigen\nD: delen.\n__________\n"; //What do you wish to do? A=Count two number. B=subtract two number C=Multiply two number D=Split two number. (?)

cin.getline(input, 2, '\n');

cout<<"__________\n";

if (!strcmpi("A", input))
{
cout<<"Je hebt ervoor gekozen om twee getallen op te tellen\n"; //You have chosen to count two numbers.
}
else if (!strcmpi("B", input))
{
cout<<"Je hebt ervoor gekozen om twee getallen af the trekken\n"; //You have chosen to subtract two numbers.
}
else if (!strcmpi("C", input))
{
cout<<"Je hebt ervoor gekozen om twee getallen te vermenigvuldigen\n"; //You have chosen to multiply two numbers.
}
else if (!strcmpi("D", input))
{
cout<<"Je hebt ervoor gekozen om twee getallen te delen\n"; //You have chosen to split two numbers.
}
else
{

cin.get();

return 0;
}
system("pause");

int x=0,y=0,z=0;
{
cout<<"Vul het eerste cijfer in: "; //Please enter the first number.

cin>>x;

cin.get();

cout<<"\nVul nu het tweede cijfer in: "; //please enter the second number.

cin>>y;

cin.get();

switch (input, '\n')

{

case 'A':

z=x+y;

break;

case 'B':

z=x-y;

break;

case 'C':

z=x*y;

break;

case 'D':

z=x/y;

break;

}
cout<<"__________\nDe uitkomst: "<<z<<"\n__________\n";     //The outcome: <<z<<; */ this is whre it goes wrong/*

}
}
}
}```
The cout functions are written in dutch, but I translated them to english.

2. Change...
Code:
`        switch (input, '\n')`
... to...
Code:
`        switch (input[0])`

3. ## Not working

That doesn't change anything! The outcome remains 0.

4. The ONLY problem: When the program calculates the sum, the outcome is always 0.

5. >>> That doesn't change anything!

Yes it does. As it stands in your program, none of your case: clauses are being considered, thus it is zero, because you are never setting it to anything else.

switch takes a single value, you are using a comma operator and thus the actual value you are testing is the last operand it sees i.e. '\n'. So your cases are saying effectively does '\n' == A, no, does '\n' == B, no does '\n' == C, no, does '\n' == D, no.

If you don't believe me, try adding...
Code:
```case default:
z = 5;
break;```
... the default case is executed if all the others are FALSE.

6. FromHolland,

adrianX is correct, although this will still give you a zero value for "z" if you input a lowercase menu choice.

#include <ctype.h>

and change your switch statement to:

switch (toupper(input[0]))

and it runs fine. Sorry I can't give a better explaination but im very new to C++.

BH

7. Thank you VERY much !! It finally works!

You say that you can't explain any better, well, maybe someone else can. I need to know HOW and WHY it works, that's how I learn from it.

8. Letters have two cases, uppercase like A and lowercase like a. Your switch cases are saying is the input variable an 'A', i.e. an uppercase A. If you entered an a i.e. a lowercase a, then it will not match as A != a. What the toupper() function does is given a character, in whatever case, it returns the uppercase value, thus toupper('a') returns 'A' thus your switch's case 'A' now will work. There is the opposite function tolower() as well which does the opposite.

9. Instead of using a foreign function you may want to look into the ASCII numerical codes of the different letters of the alphabet and write your own function.

(If at all possible I try to use only my own functions/classes/templates because I can then know all of its potential and limitations. Also good for finding problems.)

10. >>>
If at all possible I try to use only my own functions/classes/templates because I can then know all of its potential and limitations.
<<<

This approach may well be fine whilst learning or tinkering with your own projects but will not work in the professional world.

Reuse is a key concept. You will simply not have the time to develop everything from scratch. It takes to long to do, to test, and to certify - and why would an employer pay you to do this when things like the STL are there, thourougly debugged and used by thousands of developers around the world?

You will need to understand interfaces. Your task will most probably be to produce classes etc., that expose a specified interface and invoke methods of other classes, (not written by you), that also expose a specified interface.

If you do not get used to this approach, you will find joining a professional software house a bit of a jolt.

11. Also, a custom-written touppper using the key codes you know (ASCII probably) is very limited. toupper, belonging to the CRT library, is implemented for every platform where there is a C compiler, in an efficient and working way. For example, if you tried porting your custom toupper to an IBM mainframe you would suddenly face the EBCDIC key table, which would mean that you need to reimplement the function. Using the CRT this has already been done for you.

Or consider suddenly changing to a UNICODE environment, where there are other letters like Chinese ideograms. They don't have a concept of case AFAIK, but even other alphabets might have (Greek letters). You would have to sort out those cases too.
Using the CRT you simply replace toupper with wtoupper which is already complete and working.

Embrace the standard library, use it whereever you can.

12. CornedBee -- I thought the truly C++ way was to define a locale-specific toupper(), based off of the locale-specific charater toupper().

13. Yep, right. In true C++ you shouldn't use <cctype> but rather the things from <locale>.

So you should actually write
std::toupper(c, std::locale());

The advantage is that it takes heed of locale-issues and automatically supports UNICODE (if c is wchar_t, it still works).

14. i don't have much to add here, just nitpicking, but:

0 != "zero" != NULL;

what i meant to say is that 0 and NULL are two different things...

15. Not in C++, or at least not on the compilers I know.