Copyright 2004 by M. Uli Kusterer Tue, 30 Nov -1901 08:00:00 GMT Comments on article book3 at Zathras.de http://www.zathras.de/angelweb/book3.htm book3 Comments witness_dot_of_dot_teachtext_at_gmx_dot_net (M. Uli Kusterer) witness_dot_of_dot_teachtext_at_gmx_dot_net (M. Uli Kusterer) en-us Comment 52 by Daniel Bydlowski http://masters-of-the-void.com/book3.htm#comment52 http://masters-of-the-void.com/book3.htm#comment52 Hi Uli, thank you so much for your tutorial. For some reason my + calculator does not work. I try to type in the console 2 int numbers like 1 or 7 (1+7) and add them but nothing happens and the message "finished"
appears.
My code looks like this and I can't see what is wrong. Would you know what is wrong with it?
Thank you so much.
Best,
Daniel


#include <stdio.h>
#include <stdbool.h>

int main()
{
int vFirstArg,
vSecondArg;
char vOperation;
bool vFinished;

vFinished = false;

while (vFinished != true)

{
printf ( "What operation do you want to do?n" );
scanf ("%c", &vOperation);
fpurge(stdin);

if ( vOperation == '+' )

{
printf ("Enter left argument: " );
scanf ("%d", &vFirstArg);
fpurge(stdin);

printf( "nEnter right argument: " );
scanf("%d", &vSecondArg );
fpurge( stdin );

printf ("n%d + %d = %dn",
vFirstArg,
vSecondArg,
vFirstArg + vSecondArg );

}
else

vFinished = true;
}

printf ("Finished.n");

return 0;

}
Comment 51 by Daniel Bydlowski http://masters-of-the-void.com/book3.htm#comment51 http://masters-of-the-void.com/book3.htm#comment51 Hi Uli, thank you so much for your tutorial. For some reason my + calculator does not work. I try to type in the console 2 int numbers like 1 or 7 (1+7) and add them but nothing happens and the message "finished"
appears.
My code looks like this and I can't see what is wrong. Would you know what is wrong with it?
Thank you so much.
Best,
Daniel


#include <stdio.h>
#include <stdbool.h>

int main()
{
int vFirstArg,
vSecondArg;
char vOperation;
bool vFinished;

vFinished = false;

while (vFinished != true)

{
printf ( "What operation do you want to do?n" );
scanf ("%c", &vOperation);
fpurge(stdin);

if ( vOperation == '+' )

{
printf ("Enter left argument: " );
scanf ("%d", &vFirstArg);
fpurge(stdin);

printf( "nEnter right argument: " );
scanf("%d", &vSecondArg );
fpurge( stdin );

printf ("n%d + %d = %dn",
vFirstArg,
vSecondArg,
vFirstArg + vSecondArg );

}
else

vFinished = true;
}

printf ("Finished.n");

return 0;

}
Comment 50 by Uli Kusterer http://masters-of-the-void.com/book3.htm#comment50 http://masters-of-the-void.com/book3.htm#comment50 Uli Kusterer writes:
Thomas,

suer, you can use vOperation instead of the first minus sign, but of course you'd still have to write an if statement for doing the actual calculation.
Comment 49 by Thomas http://masters-of-the-void.com/book3.htm#comment49 http://masters-of-the-void.com/book3.htm#comment49 Hi Uli,
great tutorial. I did do a course on C but this was years back. Good little refresher:-)
I had a question regarding
printf( "n%f - %f = %fn", vFirstArg, vSecondArg, vFirstArg - vSecondArg );

Is it possible to use variable "vOperation" instead of the "-"? Then you would only need to write one "if" statement instead of four.
Comment 48 by Mike Polinske http://masters-of-the-void.com/book3.htm#comment48 http://masters-of-the-void.com/book3.htm#comment48 @ Eli Ron,

I think your problem is with this statement:

if(vOperation == '+', '-', '*', '/')

In C you can't just put all the values together to compare vOperator with all of them, you have to separate them with an "or" as follows:

if(vOperation == '+' || vOperation == '-' || vOperation == '*' || vOperation == '/')

@Uli,

Thanks for a great website and tutorial for learning C on the Mac.
Comment 47 by Johnny B http://masters-of-the-void.com/book3.htm#comment47 http://masters-of-the-void.com/book3.htm#comment47 Thanks for such a wonderful site! I am struggling but know that I will eventually get it. Maybe build a dictionary of terms as I go along. Anyway Pelle's concern may be related to not having

#include <stdio.h>
#include <stdbool.h>

at the top of the program. I ran into the same thing when copying and figured it out.

Thanks Again! Hope to learn much more one step at a time
Comment 46 by Eli Ron http://masters-of-the-void.com/book3.htm#comment46 http://masters-of-the-void.com/book3.htm#comment46 Hi Uli,

Thank you for this great tutorial! I'm learning a lot and I'm having a lot of fun doing it. I have run into a small problem. With my version of the code everything works well except that it doesn't recognize when I enter any other character that it should finish. Here is my code:

#include <stdio.h>
#include <stdbool.h>

int main()
{
int vFirstArg,
vSecondArg;
char vOperation;
bool vFinished;

// Make sure our flag is initialized!
vFinished = false;

// Now loop until user doesn't want anymore:
while(vFinished != true)
{
printf("What operation do you want to do?n");
scanf("%c", &vOperation);
fpurge(stdin);

// User makes a decision which operation to do:
if(vOperation == '+', '-', '*', '/')
{
printf("Enter left argument: ");
scanf("%d", &vFirstArg);
fpurge(stdin);

printf("nEnter right argument: ");
scanf("%d", &vSecondArg);
fpurge(stdin);

if (vOperation == '/' && vSecondArg == 0) // Takes care of 0 in denominator:
{
printf("Error - Can't divide by %d. Please try again: n", vSecondArg);
scanf("%d", &vSecondArg);
fpurge(stdin);
}
else //Computer does selected operation:

{
if (vOperation == '+')
{
printf("n%d %c %d = %dn",
vFirstArg,
vOperation,
vSecondArg,
vFirstArg + vSecondArg);
}

if (vOperation == '-')
{
printf("n%d %c %d = %dn",
vFirstArg,
vOperation,
vSecondArg,
vFirstArg - vSecondArg);
}

if (vOperation == '*')
{
printf("n%d %c %d = %dn",
vFirstArg,
vOperation,
vSecondArg,
vFirstArg * vSecondArg);
}

if (vOperation == '/')
{
printf("n%d %c %d = %dn",
vFirstArg,
vOperation,
vSecondArg,
vFirstArg / vSecondArg);
}

} //End computer's operation
}
else
vFinished = true;
}

printf("Finished.n");

return 0;
}
Comment 45 by P. Jeezy http://masters-of-the-void.com/book3.htm#comment45 http://masters-of-the-void.com/book3.htm#comment45 Here's a calculator that can handle decimals and

#include <stdio.h>
#include <stdbool.h>

int main ()
{
float vFirstArg,
vSecondArg;
char vOperation;
float vFirstDiv;
float vSecondDiv;
bool vFinished;

// Make sure our flag is initialized!
vFinished = false;

// Now loop until user doesn't want anymore:
while( vFinished != true )
{
printf( "What operation do you want to do?\n" );
scanf( "%c", &vOperation );
fpurge( stdin );

if( vOperation=='+')
{
printf( "Enter left argument: " );
scanf( "%f", &vFirstArg );
fpurge( stdin );

printf( "\nEnter right argument: " );
scanf( "%f", &vSecondArg );
fpurge( stdin );

printf( "\n%f + %f = %f\n",
vFirstArg,
vSecondArg,
vFirstArg + vSecondArg );
}

if (vOperation=='-')
{
printf( "Enter left argument: " );
scanf( "%f", &vFirstArg );
fpurge( stdin );

printf( "\nEnter right argument: " );
scanf( "%f", &vSecondArg );
fpurge( stdin );

printf( "\n%f - %f = %f\n",
vFirstArg,
vSecondArg,
vFirstArg - vSecondArg );
}

if (vOperation=='*')
{
printf( "Enter left argument: " );
scanf( "%f", &vFirstArg );
fpurge( stdin );

printf( "\nEnter right argument: " );
scanf( "%f", &vSecondArg );
fpurge( stdin );

printf( "\n%f * %f = %f\n",
vFirstArg,
vSecondArg,
vFirstArg * vSecondArg );
}

if (vOperation=='/')
{
printf( "Enter left argument: " );
scanf( "%f", &vFirstDiv );
fpurge( stdin );

printf( "\nEnter right argument: " );
scanf( "%f", &vSecondDiv );
fpurge( stdin );

{
if (vSecondDiv==0)
{
printf("Can't divde by zero!\n");
vFinished=true;
}

else
{
printf( "\n%f / %f = %f\n",
vFirstDiv,
vSecondDiv,
vFirstDiv / vSecondDiv );
vFinished=true;
}
}
}
else
vFinished=true;
}

printf("Finished.\n");

return 0;

}
Comment 44 by Jack http://masters-of-the-void.com/book3.htm#comment44 http://masters-of-the-void.com/book3.htm#comment44 Hey Uli, I had a question involving float variables. When I have one that should come out as 1, it comes out as 1.0000000, which is technically correct but it is annoying and can screw things up. So how do I change the amount of decimal points that it comes out as.
Comment 43 by Uli Kusterer http://masters-of-the-void.com/book3.htm#comment43 http://masters-of-the-void.com/book3.htm#comment43 Uli Kusterer writes:
Dan, yes, you can run your program in the debugger. See chapter 13 for more on that: http://www.masters-of-the-void.com/book12.htm
Comment 42 by Dan Kelleher http://masters-of-the-void.com/book3.htm#comment42 http://masters-of-the-void.com/book3.htm#comment42 Hi Uli,
I just successfully finished the initial part (+ only) of the crude calculator tutorial.
My 13-year-old son, Jeremy, is starting your tutorial also. (He wants to write applications for the iPhone and iPad and Steve Jobs has convinced him it's easy!)

Your tutorial is a great place to start- Thank You!

My Question; Is there any way to execute each step of a C program one-step-at-a-time and observe the intermediate results as one can using the SuperCard Script Tracer and the Trace command ?
-Dan
Comment 41 by Al http://masters-of-the-void.com/book3.htm#comment41 http://masters-of-the-void.com/book3.htm#comment41 Uli - Just wanted to say thanks a million for providing this resource. I first started programming in HS in the 1980's on my TRS-80 in BASIC. I then moved onto Pascal and Fortran (on a mainframe) in college. Alas, I lost the bug when I chose Assembly instead of C in my junior year of college. Fast forward to today and I have the itch to learn C and then move on to Objective-C for the iPhone. So far so good, as all the old logic (and habits) are slowly coming back to me. Can't wait to see what's coming next in these tutorials!! You are making this return to coding a lot of fun, thanks!
Comment 40 by Jack http://masters-of-the-void.com/book3.htm#comment40 http://masters-of-the-void.com/book3.htm#comment40 Hey Uli, I was attempting to create a rudimentary quiz-type program to test what I've learned here, and in my program, I was using scanf so that the user could enter in their answer, either a,b,c, or d. I then used if( vAnswer == 'B' ) the answer was correct. However, this means the answer has to be entered in as a capital B, and if it is lowercase, it will read that it is incorrect. This seemed sort of annoying so I was wondering if there was a way to get around this without creating another if statement, for instance if( vAnswer == 'B' or 'b' ) or something like that.
Comment 39 by Jones http://masters-of-the-void.com/book3.htm#comment39 http://masters-of-the-void.com/book3.htm#comment39 Great tutorial :) See if you can spot the small difference from the examples above...

#include <stdio.h>
#include <stdbool.h>

int main () {
float vFirstArg;
float vSecondArg;
char vOperation;
bool vFinished;

vFinished = false;

while (vFinished != true) {
printf("What operation do you want to do?\n");
scanf("%c", &vOperation);
fpurge( stdin );

switch (vOperation) {
case '+':
printf("Enter left argument: ");
scanf("%f",&vFirstArg);
fpurge(stdin);

printf("\nEnter right argument: ");
scanf("%f",&vSecondArg);
fpurge(stdin);

printf("\n%f + %f = %f\n", vFirstArg,vSecondArg,vFirstArg+vSecondArg);
break;
case '-':
printf("Enter left argument: ");
scanf("%f",&vFirstArg);
fpurge(stdin);

printf("\nEnter right argument: ");
scanf("%f",&vSecondArg);
fpurge(stdin);

printf("\n%f - %f = %f\n", vFirstArg,vSecondArg,vFirstArg-vSecondArg);
break;
case 'x':
printf("Enter left argument: ");
scanf("%f",&vFirstArg);
fpurge(stdin);

printf("\nEnter right argument: ");
scanf("%f",&vSecondArg);
fpurge(stdin);

printf("\n%f * %f = %f\n", vFirstArg,vSecondArg,vFirstArg*vSecondArg);
break;
case 'd':
printf("Enter left argument: ");
scanf("%f",&vFirstArg);
fpurge(stdin);

printf("\nEnter right argument: ");
scanf("%f",&vSecondArg);
fpurge(stdin);

printf("\n%f / %f = %f\n", vFirstArg,vSecondArg,vFirstArg/vSecondArg);
break;
default:
vFinished = true;
break;
}

}

printf("Finished.\n");

return 0;
}
Comment 38 by Byto http://masters-of-the-void.com/book3.htm#comment38 http://masters-of-the-void.com/book3.htm#comment38 Many thanks for posting this tutorial - it really is very manageable and, strangely, quite addictive fun.
Comment 37 by Jason http://masters-of-the-void.com/book3.htm#comment37 http://masters-of-the-void.com/book3.htm#comment37 Thanks for the left-arrow hint.

The only headers I've used are the ones your tutorial told me to use. It's all I really know how to do at this point!
Comment 36 by Uli Kusterer http://masters-of-the-void.com/book3.htm#comment36 http://masters-of-the-void.com/book3.htm#comment36 Uli Kusterer writes:
Jason,

haven't seen that yet, but that must be Xcode's autocompletion ("CodeSense"). It will only complete things you actually inclue somewhere, so I guess you must be including some header that includes an all-upper TRUE or FALSE (maybe from a prefix header?).

FWIW, you don't have to fix it for each character. Xcode remembers what you actually typed. So if you type the whole word, and only hit the left arrow key at the end, it should restore it to lowercase again.
Comment 35 by Jason http://masters-of-the-void.com/book3.htm#comment35 http://masters-of-the-void.com/book3.htm#comment35 First, I love your tutorials. Thanks for putting this together and--more importantly--making it available!

Now my problem... XCode (version 3.2.2) really wants to put 'true' and 'false' into all caps (i.e. TRUE instead of true and FALSE instead of false). It automatically completes as I type. This gives me errors when I compile.

However, when I force it into lower case (with a series of deletes as I type and it replaces my typing with capital letters) it works beautifully.

Have you seen this before? Any thoughts?
Comment 34 by Shadow Luster http://masters-of-the-void.com/book3.htm#comment34 http://masters-of-the-void.com/book3.htm#comment34 Why does the complier always say that my else statement needs an ; behind it. I check and all semicolons are there.I even tried copying and pasting your code but i still got the same error. Great tutorials by the way. Lots of fun.
Comment 33 by Uli Kusterer http://masters-of-the-void.com/book3.htm#comment33 http://masters-of-the-void.com/book3.htm#comment33 Uli Kusterer writes:
Yes Tristan, a project is essentially the wrapper for a program. There are ways to add more programs to one project, but that's a little more involved, and you generally only do that for a program that, for some reason, needs several actual program files that e.g work together.

If you want to learn more about several programs in one project, read the Xcode documentation for "targets".
Comment 32 by Tristan Moriarty http://masters-of-the-void.com/book3.htm#comment32 http://masters-of-the-void.com/book3.htm#comment32 These are a great set of tutorials, thankyou! I'm having alot of fun. I was childishly pleased with my calculator and quite want to save it before moving on to the next step, but I find that if I try and create a new file in the same source folder it won't compile. Do I need to start a new Project for every new program I write, or is there some way I can include new programs in the same project? I know this isn't strictly a "C" question, and I'm not sure if my terminology is correct, but I hope you understand! Danke!
Comment 31 by Chris http://masters-of-the-void.com/book3.htm#comment31 http://masters-of-the-void.com/book3.htm#comment31 I found this to be a nice clean code that also includes the floating-point division:

#include <stdio.h>
#include <stdbool.h>

int main ()
{
int vFirstArg,
vSecondArg;
double vQuotient; // for proper floating point division, as integer division sucks.
char vOperation;
bool vFinished;

//init finished flag
vFinished = false;

//begin main loop
while (! vFinished) {
printf("Mathematical operation (+, -, *, /), any other key to quit: \n");
scanf("%c", &vOperation);
fpurge(stdin);

// check to see if we're doing something, and if so, ask for arguments:
if (vOperation == '+' || vOperation == '-' || vOperation == '*' || vOperation == '/') {
printf("Enter first argument: ");
scanf("%d", &vFirstArg);
fpurge(stdin);

printf("Enter second argument: ");
scanf("%d", &vSecondArg);
fpurge(stdin);
}

// figure out what we're doing, and do it:
switch (vOperation) {
case '+':
printf("\n%d + %d = %d\n", vFirstArg, vSecondArg, vFirstArg + vSecondArg);
break;

case '-':
printf("\n%d - %d = %d\n", vFirstArg, vSecondArg, vFirstArg - vSecondArg);
break;

case '*':
printf("\n%d * %d = %d\n", vFirstArg, vSecondArg, vFirstArg * vSecondArg);
break;

case '/':
vQuotient = (double) vFirstArg / vSecondArg;
printf("\n%d / %d = %f\n", vFirstArg, vSecondArg, vQuotient);
break;



default:
vFinished = true; //get out of the loop and quit
break;

}
}

printf("Done!\n");

return 0; //Tell the OS that it's all good.
}

It does introduce a couple concepts not yet covered, specifically using the ! operator directly on a boolean in the loop, and the (double) in the division.
Comment 30 by Alastair Leith http://masters-of-the-void.com/book3.htm#comment30 http://masters-of-the-void.com/book3.htm#comment30 When declaring a string literal in printf double quotes are used ie. " ". In the if statement:
if ( vOperation == '+' )
single quotes are required (doubles give error).

When is one used and not the other and is there an underlying reason for this (to help me understand and remember it b/c in some scripting languages they are interchangeable (I think!))
Comment 29 by Garotas* http://masters-of-the-void.com/book3.htm#comment29 http://masters-of-the-void.com/book3.htm#comment29 Here is the source code for the calculator with switches and states for everyone interested. I have one question though. In line 86 there is a if loop for catching a user pressing "0". How could I do this with a switch?

// Crude Calculator
// C beginner tutorial on the Mac
// By "Masters of the Void"
// Case Study, November 2009

#include <stdio.h> // Defines printf etc.
#include <stdbool.h> // Defines Boolean operations.

int main() // Program start.
{
int vFirstArg, // Integers for +, -, *.
vSecondArg;
float vFirstArgDiv, // Floats for /.
vSecondArgDiv;
char vOperation, // Chars for the calculator...
vQuit; // ...and for quitting.
bool vFinished; // Loop ending condition.

// Make sure our flag is initialized!
vFinished = false;

// Now loop until user doesn't want anymore:
while(vFinished != true )
{
printf("\nWelcome to Crude Calculator!
\nWhat operation do you want to do?
\nPress (+) , (-) ,(*) or (/)\n");
scanf("%c", &vOperation); // Read.
fpurge( stdin ); // Stop reading.

switch (vOperation) // Case switch.
{

case '+': // First case: addition.
printf("Enter left argument: "); // Enter first argument.
scanf("%d", &vFirstArg);
fpurge(stdin);

printf("\nEnter right argument: "); // Enter second argument.
scanf("%d", &vSecondArg);
fpurge(stdin);

printf("\n%d + %d = %d\n", // Show result.
vFirstArg,
vSecondArg,
vFirstArg + vSecondArg);
break; // End case.

case '-': // Next case...
printf("Enter left argument: ");
scanf("%d", &vFirstArg);
fpurge(stdin);

printf("\nEnter right argument: ");
scanf("%d", &vSecondArg);
fpurge(stdin);

printf("\n%d - %d = %d\n",
vFirstArg,
vSecondArg,
vFirstArg - vSecondArg);
break;

case '*':
printf("Enter left argument: ");
scanf("%d", &vFirstArg);
fpurge(stdin);

printf("\nEnter right argument: ");
scanf("%d", &vSecondArg);
fpurge(stdin);

printf("\n%d * %d = %d\n",
vFirstArg,
vSecondArg,
vFirstArg * vSecondArg);
break;

case '/':
printf("Enter left argument: ");
scanf("%f", &vFirstArgDiv);
fpurge(stdin);

printf("\nEnter right argument: ");
scanf("%f", &vSecondArgDiv);
fpurge(stdin);

if (vSecondArgDiv == 0) // If someone divides by zero.
{
while( vSecondArgDiv == 0) // Nested if, so it is possible to re-enter second arg.
{
printf("\nYou can't divide by zero!\nEnter new right argument: ");
scanf("%f", &vSecondArgDiv);
fpurge(stdin);
}
printf("\n%f / %f = %f\n", // Show result after re-entry.
vFirstArgDiv,
vSecondArgDiv,
vFirstArgDiv / vSecondArgDiv);
}
else
printf("\n%f / %f = %f\n", // Standard result.
vFirstArgDiv,
vSecondArgDiv,
vFirstArgDiv / vSecondArgDiv);
break;

default: // Case, user pressed none of them above.
printf("Do you want to quit? (y) / (n)\n"); // Want to quit?
scanf("%c", &vQuit);
fpurge(stdin);

switch (vQuit) // New switch for quit.
{
case 'y': // Case "yes" sets quitting condition flag to "true":
vFinished = true;
break;

case'n': // "No" returns to loop.
vFinished != true;
break;
}
}
}

printf("Finished.\n"); // Outside of the while-vFinished-is-not-true loop.

return 0; // Tell OS everything is OK.
}
Comment 28 by Charles Marshall http://masters-of-the-void.com/book3.htm#comment28 http://masters-of-the-void.com/book3.htm#comment28 Looks like I misinterpreted the logic of "or" versus "and"! Thanks again, Uli!
Comment 27 by Uli Kusterer http://masters-of-the-void.com/book3.htm#comment27 http://masters-of-the-void.com/book3.htm#comment27 Uli Kusterer writes:
The || operator is defined as

<bool> || <bool>

I.e. it takes two bool expressions, and returns true (another bool) if one of them is true. But you aren't giving it a bool, you are giving it a char. Since a bool and a char are essentially just ints to the computer (a bool is either 0 or 1), your code takes the '-' as a number, which is something other than 0, so is automatically seen as true. So your whole expression could be re-written as "else if( true )", which is why you always end up in this case.

So, since this expects a bool, you need to provide it a bool, not just a char:

else if( myOperator != '+' || myOperator != '-' || ... )

read as "else if my operator is not plus or my operator is not minus or..." Of course, this isn't what you wanted to say. The above would also always be true, because myOperator holds exactly one value. So what you really need is another cool new operator, the "and" operator, &&:

else if( myOperator != '+' && myOperator != '-' && ... )

read as "else if my operator is not plus and my operator is not minus and ...".

PS - I posted your request here because your e-mail provider refuses messages from zathras.de.
Comment 26 by Charles Marshall http://masters-of-the-void.com/book3.htm#comment26 http://masters-of-the-void.com/book3.htm#comment26 In a nifty little iPhone app called C Reference I discovered the "or" boolean so I thought I'd try it out with your calculator. This is what I tried:

while(areWeDoneYet!=true)
{
printf("Please select an operation (+,-,*,/) or select 'q' to
quit:");
scanf("%c", &myOperator);
fpurge(stdin);

if(myOperator=='+')
{etc}

else if(myOperator=='-')
{etc}

else if(myOperator=='*')
       {etc}

else if(myOperator=='/')
       {etc}

else if(myOperator!='+'||'-'||'*'||'/'||'q')

printf("I'm sorry.  That's an invalid selection.n");

else if(myOperator=='q')

areWeDoneYet=true;
}

printf("We're Done!");

return 0;

This doesn't work.  It stays trapped in a loop.  Selecting "q" still prints "I'm sorry. That's an invalid selection."  However if I reverse the order of the last two "else if"s like this:

     else if(myOperator=='q')

  areWeDoneYet=true;

     else if(myOperator!='+'||'-'||'*'||'/'||'q')

  printf("I'm sorry.  That's an invalid selection.n");

It works fine.  Granted this is probably a really clumsy way of using || with != but I don't see why it fails in the first version.  Does it have something to do with 'q' not being "used" or "initialized" before the appearance of the "invalid selection" else if?

(I probably deserve what I get for messing around with operators I don't fully understand!!!)
Comment 25 by Charles Marshall http://masters-of-the-void.com/book3.htm#comment25 http://masters-of-the-void.com/book3.htm#comment25 Oh. Of course! (...time for me to find some stronger coffee.)

Thanks Uli!
Comment 24 by Uli Kusterer http://masters-of-the-void.com/book3.htm#comment24 http://masters-of-the-void.com/book3.htm#comment24 Uli Kusterer writes:
YOU are typing the line breaks in the first three examples. You type in values (when scanf() waits for them), and press the return key. In the last case, there's no input from you, so there's no return keypress either.
Comment 23 by Charles Marshall http://masters-of-the-void.com/book3.htm#comment23 http://masters-of-the-void.com/book3.htm#comment23 In the basic addition calculator example you've got several new line characters. That's fine but I've taken them out to tighten up the text lines on the console. But in doing so I've noticed something I can't figure out: when I remove the "\n"s from the first three printf's it works fine. The line prints and the following line appears immediately below it. But if I remove the "\n" on the fourth printf ("%d + %d = %d, vFirstArg, vSecondArg, vFirstArg+vSecondArg), the "What do you want to do" line appears on the same line (as the fourth printf). Why is that? My fourth printf line ends in a semicolon just like all the others. Why doesn't the first printf line appear on the line below the equation?

Thanks!
Comment 22 by Teoten http://masters-of-the-void.com/book3.htm#comment22 http://masters-of-the-void.com/book3.htm#comment22 First of all, congrats for this great tutorial. I think i'm learning a lot with this. Thanks Uli.

I've understood well I think and I understand why we have to use "else" or "else if" to continue with other operations. But I have a question, why when I just write a next "if" without the else next to it, why when I do a + operation, the program do it and then finish it?

Is something like this:

if( vOperation == '+' )
{ .... }

if ( vOperation == '-' )
{ .... }
else
vFinished = true;
}
printf( "Finished.\n" );
return 0;
}

With this, I can - all the times I want, but If I do a + operation, the program answer and then it ends. And I would like to know why.

And thanks a lot for all the tips and knowledge

Comment 21 by Uli Kusterer http://masters-of-the-void.com/book3.htm#comment21 http://masters-of-the-void.com/book3.htm#comment21 Uli Kusterer writes:
AlphaSite, there are library functions to do square roots and powers. There's pow() and sqrt(). To get them, you have to #include <math.h>. Note that sqrt() only works if you change your calculator to use floating point numbers (float or double, which use %f resp. %lf in scanf() and printf() ), as most square roots are not integers, after all.
Comment 20 by Uli Kusterer http://masters-of-the-void.com/book3.htm#comment20 http://masters-of-the-void.com/book3.htm#comment20 Uli Kusterer writes:
Andrew, what is the line where you get that error? If it says you are comparing between a pointer an integer, that is usually what you are doing. You are taking a variable that is declared as an "int * foo;" and comparing it to another that is declared as "int bar;". foo has the asterisk, so is a pointer, an address of an int. bar _is_ an int. Alternately, you may have a stray "&" before a variable name. Until you've read the later chapters that describe what "&" does in detail, you should only use it when you give a parameter to scanf(). In all other cases, just use that variable normally.
Comment 19 by Uli Kusterer http://masters-of-the-void.com/book3.htm#comment19 http://masters-of-the-void.com/book3.htm#comment19 Uli Kusterer writes:
Alistair, there's no need to check if vFirstArg is 0. It is a perfectly valid mathematical operation to divide 0 by anything, multiply it by anything, or add to or subtract from it. But dividing something by 0 doesn't make much sense (what would the result be?), which is why we check vSecondArg in that case.
Comment 18 by Uli Kusterer http://masters-of-the-void.com/book3.htm#comment18 http://masters-of-the-void.com/book3.htm#comment18 Uli Kusterer writes:
Eric, good source code you posted. Although you never put true into vFinished, so your program never really ends, it will just eternally keep looping until you shoot it down using the "Stop" button in Xcode.
Comment 17 by Uli Kusterer http://masters-of-the-void.com/book3.htm#comment17 http://masters-of-the-void.com/book3.htm#comment17 Uli Kusterer writes:
Matt, to answer your question: scanf() waits for a return before it lets your program continue, so you *have* to press return to get it to finish. If you just press return, that return will end up in vOperation, and since that is not a command we understand, we will end up in the last "else" case and set vFinished to true and end the program.
Comment 16 by Uli Kusterer http://masters-of-the-void.com/book3.htm#comment16 http://masters-of-the-void.com/book3.htm#comment16 Uli Kusterer writes:
Matt,

good catch, I forgot to mention how scanf() works in enough detail. I've adjusted the previous chapter to say more closely how scanf() works, that should clear things up.
Comment 15 by Matt http://masters-of-the-void.com/book3.htm#comment15 http://masters-of-the-void.com/book3.htm#comment15 Is the only way for program to quit when I hit enter on the keyboard, or does it quit automatically? I have read over twice and may have missed it or just confused.
Great tutorial so far!
Comment 14 by Andrew Hobson http://masters-of-the-void.com/book3.htm#comment14 http://masters-of-the-void.com/book3.htm#comment14 When I Build and Run I get a warning/error message that I am making a comparison between a pointer and an integer. I use Xcode version 3 and MacOS X 10.5.8. Is there something to change?
Comment 13 by Alistair Hutchinson http://masters-of-the-void.com/book3.htm#comment13 http://masters-of-the-void.com/book3.htm#comment13 Is noone else checking to see if vFirstArg is zero?
Comment 12 by thinkdunson http://masters-of-the-void.com/book3.htm#comment12 http://masters-of-the-void.com/book3.htm#comment12 wouldn't "else if" be more efficient than an "if" nested inside of an "else" since it's only one thing compared to two? it might not matter here, but i work with microcontrollers, and they're much slower than a personal computer. so even that extra microsecond adds up a lot when you're in a loop. especially when you're trying to time something. anyway, here's what i came up with…



#include <stdio.h>
#include <stdbool.h>

int main()
{
// declare our variables:
int vFirstArg, vSecondArg;
char vOperation, vFinished;

// Make sure our exit condition is initialized:
vFinished = 'n';

// Loop until the user decides to exit:
while(vFinished != 'y'){

// Request the parameters of the operation:
printf("\nEnter left argument: ");
scanf("%d", &vFirstArg);
fpurge(stdin);

printf("Enter right argument: ");
scanf("%d", &vSecondArg);
fpurge(stdin);

printf("\nWhat operation do you want to do (+, -, *, or /) ? ");
scanf("%c", &vOperation);
fpurge(stdin);

// Carry out the requested operation:
if(vOperation == '+')
printf("\n%d + %d = %d\n",
vFirstArg, vSecondArg, vFirstArg + vSecondArg);
else if(vOperation == '-')
printf("\n%d - %d = %d\n",
vFirstArg, vSecondArg, vFirstArg - vSecondArg);
else if(vOperation == '*')
printf("\n%d x %d = %d\n",
vFirstArg, vSecondArg, vFirstArg * vSecondArg);
else if(vOperation == '/')
// Make sure we're not being asked to divide by zero:
if(vOperation == '/')
if(vSecondArg == 0)
printf("\nYou cannot divide by zero.\n");
else
printf("\n%d ÷ %d = %d\n",
vFirstArg, vSecondArg, vFirstArg / vSecondArg);

// Ask the user if he wants to exit the program:
printf("\nAre you finished? (y/n) ");
scanf("%c", &vFinished);
fpurge(stdin);
}
return 0;
}
Comment 11 by Eric http://masters-of-the-void.com/book3.htm#comment11 http://masters-of-the-void.com/book3.htm#comment11 Here is the code for a extended calculator with working devision. I hope this helps some people. This is a great guide so far. Thanks!

#include <stdio.h>
#include <stdbool.h>

int main()
{
int vFirstArg,
vSecondArg;
float vFirstArgDiv,
vSecondArgDiv;
char vOperation;
bool vFinished;

vFinished = false;

while (vFinished != true) {
printf( "What operation do you want to do?\n" );
scanf( "%c", &vOperation );
fpurge( stdin );


if (vOperation == '+'){
printf("Enter your first number: ");
scanf("%d", &vFirstArg );
fpurge( stdin );

printf("Enter your second number: ");
scanf("%d", &vSecondArg);
fpurge( stdin );

printf( "\n%d + %d = %d\n",
vFirstArg,
vSecondArg,
vFirstArg + vSecondArg );
}
else if (vOperation == '-'){
printf("Enter your first number: ");
scanf("%d", &vFirstArg);
fpurge(stdin);

printf("Enter your second number: ");
scanf("%d", &vSecondArg);
fpurge(stdin);

printf("\n%d - %d = %d\n",
vFirstArg,
vSecondArg,
vFirstArg - vSecondArg);
}
else if (vOperation == '/') {
printf("Enter your first number: ");
scanf("%f", &vFirstArgDiv);
fpurge(stdin);

printf("Enter your second number: ");
scanf("%f", &vSecondArgDiv);
fpurge(stdin);

if (vSecondArgDiv == 0){
while (vSecondArgDiv == 0) {
printf("Sorry, you cannot devide by zero. Please try again: \n");
scanf("%f", &vSecondArgDiv);
fpurge(stdin);
}
printf("\n%f / %f = %f\n",
vFirstArgDiv,
vSecondArgDiv,
vFirstArgDiv/vSecondArgDiv);
}
}
else if (vOperation == '*'){
printf ("Enter your first number: ");
scanf ("%d", &vFirstArg);
fpurge(stdin);

printf("Enter your second number: ");
scanf ("%d", &vSecondArg);
fpurge(stdin);

printf ("\n%d * %d = %d\n",
vFirstArg,
vSecondArg,
vFirstArg * vSecondArg);
}
}
}
Comment 10 by Bert http://masters-of-the-void.com/book3.htm#comment10 http://masters-of-the-void.com/book3.htm#comment10 I don't know how long this has been up, but I wanted to add my thanks, and that I love puns. "Master of the Void" indeed. ;^)
Comment 9 by AlphaSite http://masters-of-the-void.com/book3.htm#comment9 http://masters-of-the-void.com/book3.htm#comment9 Hi, i was wondering if you wouldn't mind checking out my code for me and seeing why it won't properly end, should i send an email?

Also great tutorial, had this down really quickly and nice to see a mac oriented one. One last question is there an easy way to preform sq root or power calculations?
Comment 8 by Uli Kusterer http://masters-of-the-void.com/book3.htm#comment8 http://masters-of-the-void.com/book3.htm#comment8 Uli Kusterer writes:
Pelle,

I just copied the program and started compiling it, and it worked just fine (with the includes mentioned earlier, of course). Your error message sounds like you have some error somewhere, or your text editor adds garbage at the end of the file or so. Have you maybe forgotten a closing bracket or a semicolon? Or added ones where they shouldn't be?
Comment 7 by Pelle http://masters-of-the-void.com/book3.htm#comment7 http://masters-of-the-void.com/book3.htm#comment7 HEy, thanks for the great lessons. When I built the improved the calculator( aded other functions) it doesn't work. I tried copy+pasting yours and it gives the same error. At the last bracket which should be the last character in the program its gives this error.
"Parse error at end of input."
Can you help me?
Comment 6 by Uli Kusterer http://masters-of-the-void.com/book3.htm#comment6 http://masters-of-the-void.com/book3.htm#comment6 Uli Kusterer writes:
Division does work properly. The code uses integers, which means it only works with whole numbers. You'd have to change the code to use doubles or floats (and %f) if you want division to produce fractional numbers.
Comment 5 by Regis http://masters-of-the-void.com/book3.htm#comment5 http://masters-of-the-void.com/book3.htm#comment5 Hi, the division does not seem to work properly. For example, 5 / 6 = 0
Could you suggest a fix?

Thanks for the class,
Comment 4 by Colton http://masters-of-the-void.com/book3.htm#comment4 http://masters-of-the-void.com/book3.htm#comment4 I went just a step further and, in my DIVISION if statement, I not only put in the if(vSecondArg == 0) statement, I also nested this:

while(vSecondArg == 0)
{
printf("\nCan't divide by zero. Enter a nonzero int: ");
scanf("%d", &vSecondArg);
fpurge(stdin);
}



This keeps the program going, in case someone enters an accidental zero.
Just a tip :)
Comment 3 by Joshua Friedman http://masters-of-the-void.com/book3.htm#comment3 http://masters-of-the-void.com/book3.htm#comment3 Hey There Uli,
When I type the code above, and also when i copy and paste it into my text editor, the program runs as follows:"

What operation do you want to do?
+
Enter left argument: 1

Enter right argument: 1

-1881139919 + 1 = -1881139918


Thanks for the lessons, btw.
Joshua
Comment 2 by Uli Kusterer http://masters-of-the-void.com/book3.htm#comment2 http://masters-of-the-void.com/book3.htm#comment2 Uli Kusterer writes:
@Robert: No, that's perfectly OK. Since an "if...else" construct counts as only one command, you can provide that as the only command in an else. And since C doesn't care about how much or what whitespace you put in, you can put the "if" on the same line as the "else".
Comment 1 by Robert http://masters-of-the-void.com/book3.htm#comment1 http://masters-of-the-void.com/book3.htm#comment1 Robert writes:
.... confused by modern technology :-) i am amazed my Programm is working just as excepted, but it doesnt exactly look like the example shown here as result.

my (simplified) programm-structure looks like that:

if( vOperation == '+' )
{ }

else if ( vOperation == '-' )
{ }

else if ( vOperation == '*' )
{ }

else if ( vOperation == '/' )
{ }

still ok, or is it problematic in any way?