OK so I'm a complete noon in c and this is my first forum post :)
I've been making a calculating code for some time and changing/ adding stuff to it now I want to add an option where after you get the answer it asks you if you would like to multiply the answer..this is what I've come up with and I know its totally wrong but this is how I learn best, I just try to make with what I know lol, (BTW c++ is my first language I'm learning, I know most recommend something easier as python)
Now I know this is totally wrong and weird :)
int main(void)
{
int num1;
int num2;
int sum;
int sum2;
int answer;
int yes;
printf("Enter your number:");
scanf("%d", &num1);
scanf("%d", &num2);
sum = num1 + num2;
printf("the sum is %d", sum);
printf("would you like to multiply that?");
if(answer == yes);
sum2 = sum * sum
printf("Here you go:\n %d", sum2);
else
printf("To bad..");
getchar();
}
The program you showed us is in C - not C++.
If C++ is what you're learning, I recmmend you use the C++ streams for input and output:
http://www.cplusplus.com/doc/tutorial/basic_io/
Otherwise, you probably want curly braces around your if/else blocks:
1 2 3 4 5 6 7 8 9
if(answer == yes);
{
sum2 = sum * sum
printf("Here you go:\n %d", sum2);
}
else
{
printf("To bad..");
}
You are only allowed to omit curly braces if both the if-block and else-block only consist of one statement:
#include <stdio.h>
int main()
{
printf("Enter your numbers:");
int num1;
int num2;
scanf("%d", &num1);
scanf("%d", &num2);
constint sum = num1 + num2;
printf("the sum is %d\n", sum);
constint yes = 1 ;
printf("would you like to square that (1==yes)?");
int answer;
scanf("%d", &answer);
if(answer == yes) // ; removed
{
constint square = sum * sum ; // **** ; added
printf("Here you go: %d\n", square);
}
else printf("Too bad..");
getchar();
}