Program to solve a*(1+(b/30))

Hi! I'm absolutely beginner. I learned some variables and tried to write this code to solve the equation a*(1+(b/30)). To me, the problem seems to be that I can't use letters with numbers, like I do in lines 16 and 21. What is the correct way to write this and get it to work?

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
  #include <iostream>

using namespace std;

int main()
{
    int a;
    int b;
    int c;
    int sum;


    cout << "Enter b" << endl;
    cin >> b;

    c = b / 30 + 1;

    cout << "Enter a" << endl;
    cin >> a;

    sum = a * c;

    cout << "Answer is " << sum << endl;

    return 0;
}
Last edited on
To me, the problem seems to be that I can't use letters with numbers

I don't know what you mean by that.

Keep in mind that you've declared your variables as integers. Therefore, the compiler will do integer arithmetic. i.e for any value of b less than 30, c will always be 1 and sum will always be a.



u reallly want to have the float value of in in int?

well try

c=(b/30) +1;


nd tell
closed account (E0p9LyTq)
The equation is already legal C++. Here is how I would write the program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int main()
{
   float a;
   float b;
   float c;

   std::cout << "Enter a: ";
   std::cin >> a;

   std::cout << "Enter b: ";
   std::cin >> b;

   c = (a * (1 + (b / 30)));

   std::cout << "\nAnswer is " << c << "\n";

   return 0;
}


With a possible output:

Enter a: 3
Enter b: 35

Answer is 6.5


Using ints would truncate the fractional part.
^ u sir

i would like to know what does

std::cin

stands for

instead of normal cin ???



changeing the variables data type to float would have worked btw
closed account (E0p9LyTq)
I specify cin's namespace {std} instead of polluting the global namespace with using namespace std;.

For simple programs using namespace std; should not be a problem. But when working with larger projects that use multiple libraries it can create compiler/debugging problems.
Last edited on
"Float" did the trick. Thank you very much all of you!
Topic archived. No new replies allowed.