What is wrong in my C++ program ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
using namespace std;
int main()
{
    int a,b,i,n,s=0;
    cout<<"This program calculates s=(a+b)*i, where i is between 1 and 100, and tells us the value of i."<<endl;
    cout<<"Give a and b: ";
    cin>>a; //read a
    cin>>b; //read b
    cout<<"Give n: ";
    cin>>n; //read n
    for (i=1,100; i<=n; i++)
{
    s=s+((a+b)*i);
    cout<<"s=(a+b)*i="<<s<<endl;
    cout<<"s="<<s<<". For i="<<i<<endl;
}
    system("pause");
    return 0;
}

------------------------------------------------------------------------

this is the program i tryed to create in Dev-C++ 4.9.9.2
can someone plz explain me what is wrong :?
when i try to compile it gives me the next message: "multiple definition of 'main'; first defined here; Id returned 1 exit status; [Build Error] [Project.exe]Error1"

P.S.: Thx in advance.
Last edited on
hello there,

I was able to compile it just fine with visual studio and it works good .

you forgot to put braces like this:

for (i=1,100; i<=n; i++)
{
s=s+((a+b)*i);

cout<<"s=(a+b)*i="<<s<<endl;
cout<<"s="<<s<<". For i="<<i<<endl;
}
Last edited on
thx. i will try then with the Visual Stuido.
P.S.: i corrected the program and added the braces {}. thx again.
1
2
3
4
5
6
7
for (i=1,100; i<=n; i++)
{
s=s+((a+b)*i);

cout<<"s=(a+b)*i="<<s<<endl;
cout<<"s="<<s<<". For i="<<i<<endl;
}
Last edited on
I tryed it with Visual Stuido 2010 beta1. It works.
But im still confused, why doesnt it work on Dev-C++ 4.9.9.2

P.S.: I noticed that using the braces {} makes the program give the "s=..." for each i.
you can remove the braces if you dont want the i to be shown
Currently, you have:
1
2
3
4
5
for (i = 1; i <= n; i++) {
        s += ((a + b) * i);
        std::cout << "s = (a + b) * i = " << s << std::endl;
        std::cout << "s = " << s << " for i = " << i << std::endl;
}

If you take the braces out, the equivalent will be
1
2
3
4
5
for (i = 1; i <= n; i++) {
        s += ((a + b) * i);
}
std::cout << "s = (a + b) * i = " << s << std::endl;
std::cout << "s = " << s << " for i = " << i << std::endl;

so the program will not give the correct output.

Recommendations:
+ Indent your code. This is very important for showing the flow of the program.
+ Don't use "std::endl" for every std::cout statement (read "endl" and "cout"; the std:: prefix isn't necessary because you have using namespace std;).
std::cout << std::endl;
is equal to
std::cout << "\n" << std::flush;
so you should only put std::endl in the second std::cout statement. To clarify, change
1
2
        std::cout << "s = (a + b) * i = " << s << std::endl;
        std::cout << "s = " << s << " for i = " << i << std::endl;

to
1
2
        std::cout << "s = (a + b) * i = " << s << '\n';
        std::cout << "s = " << s << " for i = " << i << std::endl;

it's likely not a big deal in terms of a performance hit, but it's more correct.
+ I mentioned using namespace std; earlier. Please don't use it. I will write an article explaining why.
u tell me not to use "using namespace std;" ... give me the link of article !?!
Perhaps he's not finished writing it yet?

Here's the cplusplus doc on the subject: http://www.cplusplus.com/doc/tutorial/namespaces/

Bottom line is that although using namespace std; saves you some typing it also imports the whole C++ std namespace so you run the risk of using a name in your code that is already defined by the C++ standard. This is called namespace collision and it can result in very subtle bugs and unexpected behavior.
Topic archived. No new replies allowed.