help calculation code kinda

Oct 25, 2013 at 11:19am
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 :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  

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();

     }
Oct 25, 2013 at 11:44am
closed account (o3hC5Di1)
Hi there,

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:

1
2
3
4
if(answer == yes)
     printf("Here you go:\n %d", sum*sum);
else
     printf("To bad..");


Hope that helps, please do let us know if you have any further questions.

All the best,
NwN
Last edited on Oct 25, 2013 at 12:18pm
Oct 25, 2013 at 11:59am
if(answer == yes);

This will not work since you haven't initialized the value of them, or either changed it anywhere in your code
Oct 25, 2013 at 12:06pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>

int main()
{
     printf("Enter your numbers:");

     int num1;
     int num2;
     scanf("%d", &num1);
     scanf("%d", &num2);

     const int sum = num1 + num2;
     printf("the sum is %d\n", sum);

     const int yes = 1 ;
     printf("would you like to square that (1==yes)?");
     int answer;
     scanf("%d", &answer);

     if(answer == yes) // ; removed
     {
         const int square = sum * sum ; // **** ; added

         printf("Here you go: %d\n", square);
     }

     else printf("Too bad..");

     getchar();
}
Oct 25, 2013 at 12:14pm
Thanks so much for the answers!

Can anyone further explain const int? Why not just int?

and how come you didn't declare "sum"
Last edited on Oct 25, 2013 at 12:23pm
Oct 25, 2013 at 12:25pm
i think const int is the constant int,............... like the one whose value you've already set
1
2
int a;
a=6;
Oct 25, 2013 at 12:29pm
But as you can see in the code jlborges posted "sum" is not set to any value yet he uses "const int sum"


well, after playing around i got to this thanks for the help guys

#include <stdio.h>
#include <stdafx.h>


int main()
{
printf("Enter your numbers:\n");

int num1;
int num2;
scanf_s("%d", &num1);
printf("+\n");
scanf_s("%d", &num2);

const int sum = num1 + num2;
printf("= \n%d\n", sum);

const int yes = 1 ;
printf("would you like to square that? 1 = yes 2 = no\n");
int answer;
scanf_s("%d", &answer);

if(answer == yes)
{
const int square = sum * sum ;

printf("Here you go: %d\n", square);
}

else printf("Too bad..");

getchar();
}
Last edited on Oct 25, 2013 at 12:29pm
Oct 25, 2013 at 12:35pm
Can anyone further explain const int


the const keyword makes a variable read-only -> ( the value cannot be changed, it can only be determined at the time it was created:

1
2
3
4
5
const int var = 1234;
var = 2; // Will produce a compiler error !

int bla = 1234;
bla = 1; // Okay  
Oct 25, 2013 at 12:38pm
A const int is an int that
a. must be initialized
b. once initialised, the value cannot be changed


> "sum" is not set to any value yet he uses "const int sum"

1
2
3
const int sum = num1 + num2; // initializes 'sum' with the value of 'num1 + num2'
// ...
const int yes = 1 ; // initializes 'yes' with the value 1 


After that, 'sum' and 'yes' can't be changed.

sum = 0 ; // *** error: we can't modify a constant
Oct 25, 2013 at 1:21pm
awesome, thanks alot for the explanation jlborges!, and shadow :O
Topic archived. No new replies allowed.