When I add a 2nd file, I get an error and my code doesn't run on that file.
EdwardGomez (1)

Hello everyone,
I just started using repl.it and so far its great for a quick coding program!

I am using it for a C++ class and the professor gives us multiple examples to note down.
When I create a new repl and use the main.cpp file I type
#include <iostream>
using namespace std;
int main() {
then I begin to code the example. But when I try to add a new file and start with the same beginning 3 lines of code it gives me an error when running the code it says: /tmp/number-76d309.o: In function main': number.cpp:(.text+0x0): multiple definition ofmain'
/tmp/age-f5685b.o:age.cpp:(.text+0x0): first defined here
clang: error: linker command failed with exit code 1 (use -v to see invocation)
compiler exit status 1

Does anyone know how to bypass this? It would be cool to make new files for each coding example the professor gives us.

Thanks for the help!

You are viewing a single comment. View All
theangryepicbanana (857)

You cannot run multiple files in one repl (however you can include them). Just make another C++ repl

EdwardGomez (1)

Damn alright, thank you for the quick reply! @theangryepicbanana

mwilki7 (282)

@EdwardGomez
The best you can do is to use c++ threads.
But if you want to run many functions at the same time while getting user input, you won't know which one asked for your age or number first.

But if you really want to run these at the same time try:

// Example program
#include <iostream>
#include <string>
#include <thread>

using namespace std;

void getNumber() 
{
    int x;
    cout << "Pick a number 1-10." << endl;
    cin >> x;
    if (x < 1 || x > 10) {
        cout << "Pick a vaild number.";
        cin >> x;
    }
    cout << "Thanks! You number is " << x << ".";
}

void getAge()
{
    int age;
    cout << "How old are you?" << endl;
    cin >> age;
    cout << "You are " << age << " years old." << endl;
}

int main() 
{
  thread number_thread (getNumber); // spawn new thread that calls getNumber()
  thread age_thread    (getAge   ); // spawn new thread that calls getAge()

  cout << "main, getNumber and getAge now execute concurrently...\n";

  // main will wait here for these functions to finish
  number_thread.join();                // pauses until first finishes
  age_thread.join();               // pauses until second finishes
}

If you meant to do it without multi-threading try:

// Example program
#include <iostream>
#include <string>
#include <thread>

using namespace std;

void getNumber() 
{
    int x;
    cout << "Pick a number 1-10." << endl;
    cin >> x;
    if (x < 1 || x > 10) {
        cout << "Pick a vaild number.";
        cin >> x;
    }
    cout << "Thanks! You number is " << x << ".";
}

void getAge()
{
    int age;
    cout << "How old are you?" << endl;
    cin >> age;
    cout << "You are " << age << " years old." << endl;
}

int main() 
{
    getNumber();
    getAge();
}