Cloudboy22
Cloudboy22

Reputation: 1514

Unable to run a loop using while

I have written this code:

int sum,number;
sum=0;
number=1;

while(number<=11)
{
    sum=sum+number;
    cout << sum;
    number++;
}
cout << "The sum of the fist 11 is" cout << sum;

It dosent compile and gives the error:

17  37  C:\cprograms\main.cpp   [Error] expected ';' before 'cout'

I am unsable to understand what I am doing wrong?

I placed a ; after the while loop end, but it still doesn't work.

Upvotes: 0

Views: 67

Answers (3)

Abdullah Leghari
Abdullah Leghari

Reputation: 2470

Here it is. Works,

#include<iostream>
using namespace std;

int main(void)
{
    int sum, number;
    sum=0;
    number=1;

    while(number<=11)
    {
        sum = sum + number;
        cout<<sum<<endl;
        number++;
    }
    cout << "The sum of the fist 11 is "<<sum;
}

In a statement you don't write cout multiple times, just <<

Upvotes: 0

rockoder
rockoder

Reputation: 747

Change following line:

cout << "The sum of the fist 11 is" cout << sum;

to

cout << "The sum of the fist 11 is";
cout << sum;

Upvotes: 1

Ahmed
Ahmed

Reputation: 2266

replace your last line with

cout << "The sum of the fist 11 is" << sum;

Upvotes: 3

Related Questions