Problem's solution required

Can anybody help me to compile this code? I am new to C++ language and in learning process.I am trying to compile it in Dev.C++ on win 7 x64 but giving errors.

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.h>
main()
{
 int sum,number,upperLimit;
 sum=0;
 number=1;
 
//prompt the user to enter upper limit of integers
   cout<<"Please enter the upper limit for which you want the sum ” ;
   cin>>upperLimit;
// using the while loop to find out the sum of even integers starting from 1

while(number <= upperLimit)
{
// Adding the even integer to the contents of sum
if ((number%2)==0 )
{

sum = sum + number;
}
// Generate the next integer by adding 1 to the integer
number = number + 1;
}

cout << "The sum of even numbers “ <<upperLimit<< “ integers startingfrom 1 is " << sum; }
 
It looks like you are using Word, your quotes are messed up. Also, no .h when you include iostream:
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
#include<iostream>
main()
{
 int sum,number,upperLimit;
 sum=0;
 number=1;
 
//prompt the user to enter upper limit of integers
   cout<<"Please enter the upper limit for which you want the sum " ;
   cin>>upperLimit;
// using the while loop to find out the sum of even integers starting from 1

while(number <= upperLimit)
{
// Adding the even integer to the contents of sum
if ((number%2)==0 )
{

sum = sum + number;
}
// Generate the next integer by adding 1 to the integer
number = number + 1;
}

cout << "The sum of even numbers " <<upperLimit<< " integers startingfrom 1 is " << sum; }


Notice the color of my code is different, this is because it uses normal quotes. Use notepad ( or an IDE or a text editor with syntax highlighting) nto make your code.
Adnanqadri wrote:
I am trying to compile it in Dev.C++ on win 7 x64 but giving errors.


Just a small tip for the future: If you have compilation errors - then post them. It just makes life easier for the rest of us. In this case it's a good thing LowestOne has an eagle eye - things like that can be particularly hard to spot.

A couple of other things to help you out:

sum = sum + number;

Can be written like this:

sum += number;

There are other operators like this - read about them in the reference section link at the top left of this page.

number = number + 1;

If incrementing by 1 and the variable is of int type, then do this:

number++;

There is also the decrement version number-- and prefix --number & postfix versions which make a differences on assignment.

There is also a good tutorial in the Documentation section.

Finally there a lot of good stuff in this book by the inventors of the C language - worth reading the first 4 chapters at least. Might be handy to do some of the exercises as well - do them in C++ rather than C though.

http://zanasi.chem.unisa.it/download/C.pdf


Hope all goes well.
Rather than a word-processor for editing, try a proper text editor designed for programming. One example is Notepad++ though everyone has their own favourite.
http://notepad-plus-plus.org/
closed account (30X1hbRD)
using notepad I think I've managed to get it working properly. (Just so you know, I'm using mingw console gcc so it might not work right for you.) I've compiled and run it, so here's the revised code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
using namespace std;
int main()
{
 int sum,number,upperLimit;
 sum=0;
 number=1;
 //prompt the user to enter upper limit of integers
   cout<<"Please enter the upper limit for which you want the sum " ;
   cin>>upperLimit;
// using the while loop to find out the sum of even integers starting from 1
while(number <= upperLimit)
{
// Adding the even integer to the contents of sum
if ((number%2)==0 )
{
sum = sum + number;
}
// Generate the next integer by adding 1 to the integer
number = number + 1;
}
cout << "The sum of even numbers " <<upperLimit<< " integers startingfrom 1 is " << sum; }
all i did was get rid of the .h extension, make main int main, added using namespace std, and that's really it. hope it helps!
Last edited on
Topic archived. No new replies allowed.