problems building a c++ program - command error

Discussion in 'Mac Programming' started by seedanielrun, Jul 17, 2008.

  1. seedanielrun macrumors newbie

    Joined:
    Jul 17, 2008
    #1
    I'm very new to programming and all but anyway, Ok here is the problem, i've built a really, really basic program and i've tried to build it and run it but i keep getting a build failure message, now when i click on this message this is what comes up:

    Command/Deveioper/usr/bin/g++-4.0 failed with exit code 1.

    This is my code:
    {
    cout << fixed << setprecision(2);
    double num1 = 0.0;
    int num2 = 0;
    double answer =0.0;


    cout <<"please enter temperature: ";
    cin >> num1;

    cout <<"please enter which temperature you have entered, press 1 for Fahrenheit or 2 for Celsius: ";
    cin >> num2;

    if (num2 == 1){
    answer = (num1 -32) * 5.0/9.0;
    }
    else if (num2 == 2){
    answer = (num1 * 9.0/5.0) +32;
    }
    cout <<answer;

    system("pause");
    return 0;
    }

    Now i have built this and run it on the uni computers with visual studio 5.5, and i'm working with Xcode 3.0.
    so if anyone has any advice that can help me please do, i have an assignment due in 4 days and it needs to be working. (this isn't the assignment, just a prac that we did)
     
  2. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    Read the error messages that come before this. If you don't understand them, post them. If you post them, post the smallest possible _complete_ code example that shows the problem.

    There are some pretty clever people here, but they can't read your mind.
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    With some modification this is what I came up with:
    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main(int argc, char *argv[]) {
      bool done=false;
      cout << fixed << setprecision(2);
      double num1 = 0.0;
      int num2 = 0;
      double answer =0.0;
    
    
      cout <<"please enter temperature: ";
      cin >> num1;
    
      while(!done) {
        cout <<"please enter which temperature you have entered, press 1 for Fahrenheit or 2 for Celsius: ";
        cin >> num2;
    
        if (num2 == 1){
          answer = (num1 -32) * 5.0/9.0;
          done=true;
        } else if (num2 == 2){
          answer = (num1 * 9.0/5.0) +32;
          done=true;
        } else {
          cout << "Invalid answer entered. Please try again." << endl;
        }
      }
    
      cout <<answer<<endl;
    
      return 0;
    }
    
    In the future, please include your whole source file, and the whole error you were issued. This builds fine with GCC from the commandline, I would expect it to work fine with XCode as well.

    -Lee
     
  4. seedanielrun thread starter macrumors newbie

    Joined:
    Jul 17, 2008
    #4
    still having problems...same one.

    Ok so i now realise that i didn't enough information so this is whats happend now.
    I copied the code that lee wrote and put it straight into xcode and i'm still getting the same problem and to show you exactly what its saying i took a screen shot. Hopefully this will enlighten you as to whats going on.
    By the way thank you for the help either way.

    so the code is exactly what lee1210 wrote.
    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main(int argc, char *argv[]) 
    {
      bool done=false;
      cout << fixed << setprecision(2);
      double num1 = 0.0;
      int num2 = 0;
      double answer =0.0;
    
    
      cout <<"please enter temperature: ";
      cin >> num1;
    
      while(!done) 
    	{
    		cout <<"please enter which temperature you have entered, press 1 for Fahrenheit or 2 for Celsius: ";
    		cin >> num2;
    
    			if (num2 == 1)
    			{
    			answer = (num1 -32) * 5.0/9.0;
    			done=true;
    			} 
    				else if (num2 == 2)
    				{
    				answer = (num1 * 9.0/5.0) +32;
    				done=true;
    				}
    					else 
    					{
    						cout << "Invalid answer entered. Please try again." << endl;
    					}
      
    	}
      cout <<answer<<endl;
      system("pause");
      return 0;
    }
    	
    	
     

    Attached Files:

  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    This is a link error, not a compilation error.

    It appears that you have a file called daniel.[m|c|cp|cpp] that has a function called main, as well as a file called main.[m|c|cp|cpp] in the same project. Compilation succeeds for all of your objects, but when XCode tries to link all of the objects and build an executable, there are duplicate symbols for the function main.

    It may be the case that when you started the XCode project it created a default file with a default main function, and you added a new file and put in your code, which including your own main function.

    To resolve this remove one of the files mentioned above, and put the code you want to use for your main function in the remaining file. This should take care of it.

    This is one of the many reasons I will always recommend starting with gcc at the command line while you're working on simpler programs, and only moving to XCode when you have quite a few source files and dependencies that are becoming too difficult to maintain.

    -Lee
     

Share This Page