C++ Question ...

Discussion in 'Mac Apps and Mac App Store' started by Fender2112, Nov 6, 2003.

  1. Fender2112 macrumors 6502a

    Fender2112

    Joined:
    Aug 11, 2002
    Location:
    Charlotte, NC
    #1
    In my c++ class I've been exposed to these commands:

    system("cls") which clears the screen.
    system("pause") which envokes the 'Press any key to continue' command.

    I informed my instructor that these commands don't work on my Mac. He told me that these are DOS commands.

    My question: What are the Mac OS equivalents to these?

    Thanks much.
     
  2. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #2
    There really aren't any equivalents.

    I'm guessing that you're using these to show your output clearly in the DOS Window.

    You can probably code these as:

    #ifdef __MSDOS__
    system("cls");
    #endif

    ...

    #ifdef __MSDOS__
    system("pause");
    #endif

    They will execute on the DOS/Windows machines but work without error on Mac OS X or most anything else as well.

    To execute the programme within Mac OS X, you would open the Terminal application and type ./programme-name and press <return> or <enter>.
     
  3. db2guru macrumors newbie

    Joined:
    Oct 9, 2003
    #3
    Re: C++ Question ...

    system("clear"); // clear the screen
    system("echo -n Press ENTER to continue. ; echo $<"); // pause for input

    should get you going, if you are using the default tcsh shell. Try these from the command line and then work them into the code.
     
  4. Fender2112 thread starter macrumors 6502a

    Fender2112

    Joined:
    Aug 11, 2002
    Location:
    Charlotte, NC
    #4
    I tried both of these suggestions but neither worked.

    Here's the code I'm using with CodeWarrior:


    //***************************************************************************************
    //* This program uses a two dimensional arrary to store the hours worked for five days *
    //* for five employees. The user enters the hours worked each day for each employee. *
    //* A daily average is calculated for the five day period for each employee. *
    //***************************************************************************************


    int main ()
    {

    float hoursWorked [5] [5]; // Two dimensional array with five elements in each dimension
    float totHours, dayAvg;


    for (int emp = 1; emp <= 5; emp ++) // This loop sets first index of the array
    {
    //*** I need to clear the screen here ***
    system("cls");
    system ("clear");

    totHours = 0; // reset totals for each employee
    dayAvg = 0;


    cout.precision(2); // Use this and
    cout.setf(ios::fixed | ios::showpoint); // this to set decimal precision

    cout << "Enter the hours workd each day for employee: " << emp << endl;

    for (int day = 1; day <= 5; day ++) // This loop sets the second index of the array
    {
    cout << "How many hours did this employee work on day " << day << ": ";
    cin >> hoursWorked [emp] [day];
    totHours = totHours + hoursWorked [emp] [day];
    }

    // Calculate the daily average for this employee
    dayAvg = totHours / 5;
    cout << "This employee has a daily average of " << dayAvg << " hours.";
    cout << endl << endl;

    //*** I need to pause here ***
    system("pause");
    system("echo -n Press ENTER to continue. ; echo $<"); // pause for input


    }

    return 0;
    }

    //*** End Program ***
     
  5. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #5
    Well, you can't use them quite that way. Instead of

    system("cls");
    system ("clear");

    use

    #ifdef __MSDOS__
    system("cls");
    #else
    system ("clear");
    #endif

    Make the same type of change further down. Those are two underscores in front and in back of MSDOS. If __MSDOS__ doesn't work, you might try __WINDOWS__
     
  6. db2guru macrumors newbie

    Joined:
    Oct 9, 2003
    #6
    You don't need the dos cls command.
    // system("cls");

    Pause by doing the same thing the previous code is doing:

    cout << "Press ENTER to continue.";
    cin >> reply;

    Obviously, you need to add a variable for the reply.

    Get rid of the system calls for pause.

    Fixing the remaining bugs is an exercise for the author.....
     
  7. Fender2112 thread starter macrumors 6502a

    Fender2112

    Joined:
    Aug 11, 2002
    Location:
    Charlotte, NC
    #7
    These don't work, unless I'm doing something wrong. Which is highly possible.
     
  8. Fender2112 thread starter macrumors 6502a

    Fender2112

    Joined:
    Aug 11, 2002
    Location:
    Charlotte, NC
    #8
    Pausing is not the problem. It's the continue part. I still have to input something for the cin statement. C++ does not acknowledge a "null" entry. At least not to my limited knowledge.

    I'm sure there is a simple solution to this. I hate to think that there is actually something this simple that can be done on Windows but not a Mac. Please tell me it ain't so.:(
     
  9. Lyle macrumors 68000

    Lyle

    Joined:
    Jun 11, 2003
    Location:
    Madison, Alabama
    #9
    For the "pause" part of the code, see if this works better instead:

    Code:
    #include <stdio.h>
    
    int main()
    {
        ...
        // Pause and wait for a keypress before going on
        printf("Press any key to continue...");
        getchar();
        printf("\n");
    
        // Now continue...
    }
     
  10. Fender2112 thread starter macrumors 6502a

    Fender2112

    Joined:
    Aug 11, 2002
    Location:
    Charlotte, NC
    #10
    Okay, that got me pointed in the right direction. I came up with this, which is within the scope of what I've learned in my class.

    cout << "Press ENTER to continue.";
    cin.get(); // clears white space from input buffer
    getline (cin, wait); // wait is declared as a string type
    cout << endl;

    It's not as nice system("pause"), but it works for my purposes at the moment.

    Now, how about that clear screen command? Any ideas.
     
  11. Lyle macrumors 68000

    Lyle

    Joined:
    Jun 11, 2003
    Location:
    Madison, Alabama
    #11
    Doing
    Code:
    system("clear");
    didn't work? I just tried a quickie little test program and it had the desired effect here.
     
  12. Fender2112 thread starter macrumors 6502a

    Fender2112

    Joined:
    Aug 11, 2002
    Location:
    Charlotte, NC
    #12
    Nope. It doesn't work for me. I'm using CodeWarrior 5.0 Learning Edition. What compiler are you using? Do I need a header file for system("clear")?
     
  13. Lyle macrumors 68000

    Lyle

    Joined:
    Jun 11, 2003
    Location:
    Madison, Alabama
    #13
    I'm using gcc (which comes free with the Apple Developer Tools).
    Well, technically you should include <stdlib.h> at the top of the file, but if it's compiling without errors that's probably not the source of your problem.
     
  14. ksz macrumors 68000

    Joined:
    Oct 28, 2003
    Location:
    San Jose, CA
    #14
    Fender2112: Try running your program from the Terminal command prompt. Open the Termina, cd to your program's directory, and execute the program from there.

    Running the program from within CodeWarrior (yes, I know it's a "console" application) is not likely the same as running it from the command line. It depends on the implementation of "console" by your compiler.
     

Share This Page