Ok, for future reference, don't paste images... post actual C source code like:
Code:
[code]
// Your code here
[/code]
Now, to the program. I've typed it in basically as you presented.
Code:
#include <stdio.h>
void main()
{
//
int B1, B2;
double R;
char O;
//
printf("Enter the 1st number: ");
scanf("%d", &B1);
printf("Enter the 2nd number: ");
scanf("%d", B2);
printf("Insert the symbol for the desired operation (+, -, *, /) : ");
O = getchar();
//
if (O == '+') {
R = B1 + B2;
}
else if (O == '-') {
R = B1 - B2;
}
else if (O == '*') {
R = B1 * B2;
}
else if (O == '/') {
R = B1 / B2;
}
//
printf("Result of the operation is: %d", R);
system("PAUSE");
return 0;
}
Since this is probably very new to you, your professor has probably omitted error checking code deliberately (for demonstration purposes) so I won't comment on that too much. But in real life you should be checking the return values of scanf() and getchar() (also char O; should be int O; because getchar returns an integer that might be EOF which you should also check for.) But let's skip past all that for now.
Code:
#include <stdio.h>
//!!! There are only two valid ways to declare void:
//!!! int main(void) and
//!!! int main(int argc, char *argv[])
//!!! The one you want in this example is int main(void)
//!!! Note also that there is another problem with void main(void): At the end you have
//!!! return 0; but your declaration says that main returns nothing. Leave the return 0
//!!! because that is correct, but change how you declare main()
void main() //!!! Change this to int main(void)
{
//
int B1, B2;
double R;
char O; //!!! Technically this should be int O; because getchar returns int
//
printf("Enter the 1st number: ");
scanf("%d", &B1);
printf("Enter the 2nd number: ");
scanf("%d", B2); ///!!! Does this look similar to the previous scanf()? %d expects you to
///!!! supply "a pointer to the place to store the result". Don't worry about
///!!! what & does at the moment I guess (you'll learn later), just note that it should be &B2
printf("Insert the symbol for the desired operation (+, -, *, /) : ");
O = getchar();
///!!! Some time when you learn more you should check for EOF here. You should also check that you have
///!!! actually read a character that is not whitespace (spaces, tabs, end of line characters etc).
//
if (O == '+') {
R = B1 + B2;
}
else if (O == '-') {
R = B1 - B2;
}
else if (O == '*') {
R = B1 * B2;
}
else if (O == '/') {
R = B1 / B2; ///!!! R is of type double, but the expression B1 / B2 is of type int, so this
///!!! likely does not do what it's meant to do
}
///!!! what happens if O did not equal any of those characters (+ - * /)? In the next logical line you
///!!! will print the value of R, but R is not initialized.
//
printf("Result of the operation is: %d", R); ///!!! %d says to print an integer, but R is a double
system("PAUSE"); ///!!! Don't worry about this for now, but note that it won't work on anything but (probably) Windows
return 0; //!!! See my comment before main() (this line is correct, the declaration of main is not. You can't return a
//!!! value from a function that is declared to return void (i.e. nothing)
}