int main ()

how do you separate one program from another on a single file?
i have to submit a hw problem that is 3 parts and I'm trying to send one cpp file but i currently have 3 different projects open on Xcode

like this is my code for 2 questions

#include <iostream>
using namespace std;

int main ()
{
int x;
int y;
x = 62;
y = 99;
int total = x + y;
cout << "sum of the two numbers is " << total << endl;
return 0;

}


#include <iostream>
using namespace std;

int main ()

{

int numberone, numbertwo, numberthree, numberfour, numberfive;
numberone = 28;
numbertwo = 32;
numberthree = 37;
numberfour = 24;
numberfive = 33;
int sum;
sum = numberone + numbertwo + numberthree + numberfour + numberfive;
double average;
average = sum / 5.0;
cout << "The average is " << average << endl;
return 0;

when i put these two codes on one file it won't run bc of a "redefinition of main" error.. how do i separate the two?

idk if this question made any sense....

The simple answer is that you don't do that, you can't have more than one main() function within the same compilation file.

I would suggest renaming your main functions to "problemN" functions.
And then calling each function in a new main function.


I'll just save some time and show you exactly what I mean:
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
28
29
30
31
32
33
34
35
36
37
#include <iostream>
using namespace std;

int problem1()
{
    int x;
    int y;
    x = 62;
    y = 99;
    int total = x + y;
    cout << "sum of the two numbers is " << total << endl;
    return 0;

}

int problem2()
{  
    int numberone, numbertwo, numberthree, numberfour, numberfive;
    numberone = 28;
    numbertwo = 32;
    numberthree = 37;
    numberfour = 24;
    numberfive = 33;
    int sum;
    sum = numberone + numbertwo + numberthree + numberfour + numberfive;
    double average;
    average = sum / 5.0;
    cout << "The average is " << average << endl;
    return 0;
}

int main()
{
    // Calls both problem functions:
    problem1();
    problem2();
}
but when i do this and run the program it only solves the first problem ..
ok nevermind i got it!!
thank you!!!
Topic archived. No new replies allowed.