-6

This is probably a very easy problem, but I am studying while loops and I am trying to write a program that sums numbers from 50 to 100. This is my code that I wrote

#include  <iostream>
using namespace std;

int main() {
    int sum=0;
    int val=1;
    while(50 <= val <= 100) {
        sum = sum + val;
        val = val + 1;
    }
    cout << "Sum is: " << sum <<  endl;

    return 0;
}

I was to compile the code and get a program, but each time I am trying to run the program on terminal is just goes idle. Is there something wrond with my code? Thanks!

bereal
  • 32,519
  • 6
  • 58
  • 104
Learner
  • 91
  • 4
  • 3
    Take a C++ book and learn from the scratch – Danh Sep 01 '16 at 07:08
  • 1
    Danh nailed it. But to give you a hint: your condition check in while is not correct. – Kami Kaze Sep 01 '16 at 07:09
  • Im completely new to C++. I have just started learning it this week. Sorry if this question is extremely elementary. – Learner Sep 01 '16 at 07:10
  • A hint: look what happens the first time you get in the while loop with val variable. – FrankS101 Sep 01 '16 at 07:12
  • then you should at least check how to code logical statements in c++. The conditions of your while loop are logical statements and thats not how it works. AND you will never really enter the loop because your val is not in the desired range – Kami Kaze Sep 01 '16 at 07:13

1 Answers1

2

All the comments are valid. Please look at the C++ reference for syntax and how to use operators and loop.

I believe looking at some correct code is also a way to learn and hence posting this :

#include <iostream>
using namespace std;
int main ()
{
    int sum = 0;
    int i = 50; // Why not start from 50 itself, when you want sum(50-100)
    while (i <=100)
    {
        sum += i; // Same as sum = sum + i
        i++; // Same as i = i + 1
    }   
    cout<<sum<<"\n";
    return 0;
}
Rohith R
  • 1,309
  • 2
  • 17
  • 36