Error in my programming

solve error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include<conio.h>
#include<iostream.h>

void main()
{
double s=1,n,i;
clrscr();
cout<<"Enter n(n=2k):";
cin>>n;
if(n%2>0)
	{
	cout<<"the number you entered is odd";
	cin>>n;
	}
	else if(n%2!=0)
	{
for(i=2;i<=n;i+=2)
	{
	s+=(i+1)/i;
	}
cout<<"sum of them="<<s;
	 }
getch();
}


Errors:
compiling noname.cpp
Error noname.cpp 10:illegal use of floating point
Error noname.cpp 15:illegal use of floating point
You cannot use straight mod operation on doubles. Why n is double? Would int suffice?
my target:
1+3/2+5/4+7/6+...+(n+1)/n
But you are not dividing anything by n.
line 19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<conio.h>
#include<iostream.h>

void main()
{
double s=1,n,i;
clrscr();
cout<<"Enter n:";
cin>>n;
for(i=2;i<=n;i+=2)
	{
	s+=(i+1)/i;
	}
cout<<"sum of them="<<s;

getch();
}

I want to tell the user to enter an even number.
line 19
s+=(i+1)/i; I do not see a single 'n' in this line.

What prevents you to make n int? You want user to enter even integer number. So you do not need n to be floating point one.

Without condition, works great
but i want to use condition
CHANGE. TYPE. OF. n. TO. int.

Remove if in the else statement at line 15.

Here is code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include<conio.h>
#include<iostream.h>

void main()
{
double s=1,i; int n;
clrscr();
cout<<"Enter n(n=2k):";
cin>>n;
if(n%2>0)
	{
	cout<<"the number you entered is odd";
	cin>>n;
	}
	else if(n%2!=0)
	{
for(i=2;i<=n;i+=2)
	{
	s+=(i+1)/i;
	}
cout<<"sum of them="<<s;
	 }
getch();
}
Enter n(n=2k):8
sum of them=6.04167
Proof: http://ideone.com/YHU5SU
Had to change code a little to make it compile on something other than ancient non-standard compilers.
Last edited on
I tried your code.
But when I entered odd number
message (the number you entered is odd) not found
Works for me:
Enter n(n=2k):7
the number you entered is odd
Works for me:

Enter n(n=2k):7
the number you entered is odd

see link
http://upir.ir/1393.2/Screenshot-4-.png
WHy you are using code from ideone when it was changed to work on modern compilers?
I posted code you need in my post earlier.
Compiler problems. Code should work, but buggy compiler cannot correctly link it.
You should really think about upgrading: you are using headers which were discontinued in 1998. After that another 3 4 times language was updated.
Can you give me download link of the latest version of c++ for Windows 8???
codeblocks-13.12mingw-setup-TDM-GCC-481.exe
http://www.codeblocks.org/downloads/26
or Express 2013 for Windows Desktop
http://www.visualstudio.com/en-us/products/visual-studio-express-vs.aspx

Note that with those your code will not work (as it is non-standard). Use ideone code.
Topic archived. No new replies allowed.