Thread: Win XP and C++(error)

  1. #1
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69

    Win XP and C++(error)

    I gave a program that I made to my friend, (I use win 98 se) and it worked fine on my computer, but there is some dos related error on his computer. Why, and how can I by pass that?

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    53
    I lost my crystal ball; could you please tell us what is the exact error ? What compiler you used ? What is the program doing ? ............................................

  3. #3
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69
    I used Dev C++ 4, and once the program just popped open, then closed, and the other it said something like, "this system doesnt use DOS, blah,blah"

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    53
    1) Remove every call to system()
    2) Add std::cin.get() at the end of your program.

    Can't help you much without code.

  5. #5
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69
    ok, i'll try getting rid of the system stuff, but then how do I make the screen clear(system("cls")) and how do i make the program wait for any key(system("PAUSE")). I'll post my code:
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <iomanip.h>
    #include <string>
    int choice;
    int quantity;
    string text;
    
    class sector
    {
    private:
      unsigned int credits;
      unsigned int power;
      unsigned int metal;
      unsigned int crew;
      unsigned int jets;
      unsigned int starships;
      unsigned int days;
      unsigned int jettime[2];
      unsigned int startime[2];
      unsigned int minetime[2];
      unsigned int traintime[2];
    public:
      sector();
      int timer();
      int retrieve(int);
      int attack(int,int);
      int build();
      int mine();
      int train();
      int stats();
      int next();
     };
    sector::sector()                                                    //Constructor
        {
          credits = 1000000;
          power = 100;
          metal = 100;
          crew = 50;
          jets = 0;
          starships = 1;
          days = 1;
          jettime[0] = 0;
          startime[0] = 0;
          minetime[0] = 0;
          traintime[0] = 0;
          jettime[1] = 0;
          startime[1] = 0;
          minetime[1] = 0;
          traintime[1] = 0;
        }
    
    int sector::timer()                                                  //TIMER
        {
          system("cls");
          days++;
          power = power + 20;
          if (jettime[0] == days)
              {
                jets = jets + jettime[1];
                cout<<"Alas, your jets are complete!"<<endl;
                jettime[0] = 0;
                jettime[1] = 0;
              }
          if (startime[0] == days)
             {
               starships = starships + startime[1];
               cout<<"Alas, your StarShips are complete!"<<endl;
               startime[0] = 0;
               startime[1] = 0;
             }
          if (minetime[0] == days)
             {
               metal = metal + (minetime[1]* 10);
               cout<<"Alas, your mining team has returned!"<<endl;
               minetime[0] = 0;
               minetime[1] = 0;
             }
          if (traintime[0] == days)
             {
               crew = crew + traintime[1];
               cout<<"Alas, your crew training is complete!"<<endl;
               traintime[0] = 0;
               traintime[1] = 0;
             }
          system("PAUSE");
          if (days == 10)
             {
               text = "Scouts have reported an attack on the asteroid.\n5 jets and 1 StarShip!";
               if (attack(5,1) == 1)
                  {
                    text = "PPP";
                    return 0;
                  }
               else
               return 0;
             }
          else if (days == 25)
             {
               text = "Scouts have reported an attack on the asteroid.\n12 jets and 3 StarShips!";
               attack(12,3);
               return 0;
             }
          else if (days == 30)
             {
               text = "Sir! Intruders are attacking the outpost. If we don't do something quick,\nWe're done for!\n30 jets and 8 StarShips!";
               attack(30,8);
               return 0;
             }
          else if (days == 42)
             {
               text = "OOO";
               return 0;
             }
          else
             {return 0;}
          return 0;
        }
    
    int sector::attack(int a,int b)
        {
          int quantjet;
          int quantstar;
          cout<<"\n\n"<<text<<endl;
         do
         {
          cout<<"\n\nHow many Jets will you send? ";
          cin>>quantjet;
          cin.ignore();
         }while (quantjet > jets);
          jets = jets - quantjet;
         do
         {
          cout<<"\n\nHow many StarShips will you send? ";
          cin>>quantstar;
          cin.ignore();
         }while (quantstar > starships);
          starships = starships - quantstar;
          if (quantjet > a && quantstar > b)
             {
               cout<<"\n\n"<<setw(10)<<"You are victorious!\n"<<endl;
               system("PAUSE");
               return 0;
             }
          else if (quantjet == a && quantstar == b)
             {
               cout<<"\n\nThat was suicidal!GAME OVER!!\n"<<endl;
               system("PAUSE");
               return 1;
             }
          else
             {
               cout<<"\n\nYou lost!GAME OVER!!\n"<<endl;
               system("PAUSE");
               return 1;
             }
        }
    
    int sector::build()                                         //BUILD
        {
          if ((jettime[1] != 0) || (startime[1] != 0))
             {return 0;}
          else
          quantity = 0;
          int cred;
          int pow;
          int met;
          int cre;
          cred = 0;
          pow = 0;
          met = 0;
          cre = 0;
          system("cls");
          cout<<"Credits: "<<retrieve(1)<<setw(15)<<"Power: "<<retrieve(2)<<setw(15)<<"Metal: "<<retrieve(3)<<setw(15)<<"Day "<<retrieve(7)<<endl;
          cout<<"Crew: "<<retrieve(4)<<setw(15)<<"Jets: "<<retrieve(5)<<setw(15)<<"Starships: "<<retrieve(6)<<endl;
          cout<<"\nWhat are your building orders, Admiral?"<<endl;
          cout<<"\n[1] Jets"
              <<"\n[2] StarShips"
              <<"\n[0] Back\n"<<endl;
          cout<<"Order: ";
          cin>>choice;
          cin.ignore();
          if (choice == 1)
             {
               cout<<"\nThis costs 100 credits, 10 power, 10 metal, and 2 crew. Quantity: ";
               cin>>quantity;
               cin.ignore();
               cred = quantity * 100;
               pow = quantity * 10;
               met = quantity * 10;
               cre = quantity * 2;
               if ((retrieve(1) >= cred) && (retrieve(2) >= pow) && (retrieve(3) >= met) && (retrieve(4) >= cre))
                   {
                    cout<<"\n\n SUCCESS! Your ships will be built in "<<(quantity * 1)<<" days.\n"<<endl;
                    system("PAUSE");
                    credits = credits - cred;
                    power = power - pow;
                    metal = metal - met;
                    crew = crew - cre;
                    jettime[0] = days + (quantity * 1);
                    jettime[1] = quantity;
                    return 0;
                   }
                else
                   {
                    cout<<"\n\nNOT ENOUGH SUPPLIES! Cannot build.\n"<<endl;
                    system("PAUSE");
                    return 0;
                   }
             }
          else if (choice == 2)
             {
               cout<<"\nThis costs 1000 credits, 50 power, 50 metal, and 8 crew. Quantity: ";
               cin>>quantity;
               cin.ignore();
               cred = quantity * 1000;
               pow = quantity * 50;
               met = quantity * 50;
               cre = quantity * 8;
               if ((retrieve(1) >= cred) && (retrieve(2) >= pow) && (retrieve(3) >= met) && (retrieve(4) >= cre))
                   {
                    cout<<"\n\n SUCCESS! Your ships will be built in "<<(quantity * 5)<<" days.\n"<<endl;
                    system("PAUSE");
                    credits = credits - cred;
                    power = power - pow;
                    metal = metal - met;
                    crew = crew - cre;
                    startime[0] = days + (quantity * 5);
                    startime[1] = quantity;
                    return 0;
                   }
                else
                   {
                    cout<<"\n\nNOT ENOUGH SUPPLIES! Cannot build.\n"<<endl;
                    system("PAUSE");
                    return 0;
                   }
             }
          else
             {return 0;}
        }
    
    int sector::mine()
        {
          if (minetime[1] != 0)
             {return 0;}
          else
          quantity = 0;
          system("cls");
          cout<<"Credits: "<<retrieve(1)<<setw(15)<<"Power: "<<retrieve(2)<<setw(15)<<"Metal: "<<retrieve(3)<<setw(15)<<"Day "<<retrieve(7)<<endl;
          cout<<"Crew: "<<retrieve(4)<<setw(15)<<"Jets: "<<retrieve(5)<<setw(15)<<"Starships: "<<retrieve(6)<<endl;
          cout<<"\n\nShall we mine, Admiral?"<<endl;
          cout<<"\n   How many crew members will you send? ";
          cin>>quantity;
          cin.ignore();
          if (quantity > retrieve(4))
              {
                cout<<"\n\nNOT ENOUGH CREW! Cannot send.\n"<<endl;
                system("PAUSE");
                return 0;
              }
          else if (quantity == 0)
              {return 0;}
          else
              {
                cout<<"\n\nSUCCESS! "<<quantity<<" crew members have been sent to mine.\n"
                    <<"They will be back in 4 days.\n"<<endl;
                system("PAUSE");
                crew = crew - quantity;
                minetime[0] = days + 4;
                minetime[1] = quantity;
                return 0;
              }
        }
    
    int sector::train()
        {
           if (traintime[1] != 0)
             {return 0;}
          else
          quantity = 0;
          system("cls");
          cout<<"Credits: "<<retrieve(1)<<setw(15)<<"Power: "<<retrieve(2)<<setw(15)<<"Metal: "<<retrieve(3)<<setw(15)<<"Day "<<retrieve(7)<<endl;
          cout<<"Crew: "<<retrieve(4)<<setw(15)<<"Jets: "<<retrieve(5)<<setw(15)<<"Starships: "<<retrieve(6)<<endl;
          cout<<"\n\nShall we train the crew, Admiral?"<<endl;
          cout<<"\n   How many men do you want to train into crew($10 each)? ";
          cin>>quantity;
          cin.ignore();
          if ((quantity * 10) > retrieve(1))
              {
                cout<<"\n\nNOT ENOUGH CREDITS! Cannot train.\n"<<endl;
                system("PAUSE");
                return 0;
              }
          else if (quantity == 0)
              {return 0;}
          else
              {
                cout<<"\n\nSUCCESS! "<<quantity<<" crew members are being trained.\n"
                    <<"They will be ready in "<<(quantity / 10)<<" days."<<endl;
                system("PAUSE");
                credits = credits - (quantity * 10);
                traintime[0] = days + (quantity / 10);
                traintime[1] = quantity;
                return 0;
              }
        }
    
    int sector::stats()
        {
          system("cls");
          cout<<"Credits: "<<retrieve(1)<<setw(15)<<"Power: "<<retrieve(2)<<setw(15)<<"Metal: "<<retrieve(3)<<setw(15)<<"Day "<<retrieve(7)<<endl;
          cout<<"Crew: "<<retrieve(4)<<setw(15)<<"Jets: "<<retrieve(5)<<setw(15)<<"Starships: "<<retrieve(6)<<endl;
          cout<<"\n\n  Jets in Production | StarShips in Production | Miners Gone | Crew in Training"<<endl;
          cout<<"-------------------------------------------------------------------------------"<<endl;
          cout<<"#of| "<<setw(5)<<jettime[1]<<setw(20)<<startime[1]<<setw(20)<<minetime[1]<<setw(20)<<traintime[1]<<endl;
          cout<<"-------------------------------------------------------------------------------"<<endl;
          cout<<"Day| "<<setw(5)<<jettime[0]<<setw(20)<<startime[0]<<setw(20)<<minetime[0]<<setw(20)<<traintime[0]<<endl;
          cout<<"-------------------------------------------------------------------------------"<<endl<<endl<<endl;
          cout<<"\n\n[1]Cancel order"
              <<"\n[0]Back\n"<<endl;
          cin>>choice;
          cin.ignore();
          if (choice == 1)
             {
               cout<<"\n\nWhich order?"<<endl;
               cout<<"\n[1]Jets"
                   <<"\n[2]StarShips"
                   <<"\n[3]Crew"
                   <<"\n[0]Back\n"<<endl;
               choice = 0;
               cin>>choice;
               if (choice == 1)
                  {
                    jettime[0] = 0;
                    jettime[1] = 0;
                    return 0;
                  }
               else if (choice == 2)
                  {
                    startime[0] = 0;
                    startime[1] = 0;
                    return 0;
                  }
               else if (choice == 3)
                  {
                    traintime[0] = 0;
                    traintime[1] = 0;
                    return 0;
                  }
               else
                  {
                    return 0;
                  }
             }
          else
             {
               return 0;
             }
        }
    
    int sector::retrieve(int a)
        {
          switch (a)
           {
             case 1:
               return (credits);
               break;
             case 2:
               return (power);
               break;
             case 3:
               return (metal);
               break;
             case 4:
               return (crew);
               break;
             case 5:
               return (jets);
               break;
             case 6:
               return (starships);
               break;
             case 7:
               return (days);
               break;
             default:
               break;
           }
        }
    
    int main()
    {
    cout<<setw(30)<<"Defender 1.0"
                  <<"\nBy Adam Lesinski"<<endl;
    cout<<"\n[1]Play"
        <<"\n[0]Quit\n"<<endl;
    cin>>choice;
    cin.ignore();
    if (choice == 0)
        return 0;
    else if (choice != 0 && choice != 1)
            return 0;
    else
    system("cls");
    string name;                                             //define name
    sector account;                                          //define object
    cout<<"Good day, Admiral... My apologies, what was your name again?"<<endl;
    cin>>name;
    cin.ignore();
    cout<<"\nRight! Welcome to this sector Admiral "<<name<<"."
        <<"\nI am Grand Admiral Drugor of the Authority. "
        <<"I have called you here this evening\nto discuss your defection to the Authority. "
        <<"It is very rare that defects such asyourself maintain their previous ranks, but you are an "
        <<"exception. Your skill is\namazing!\n\nI want you to protect an outpost situated in the Senex sector,\norbiting the "
        <<"planet Kraul. We cannot spare any ships, because of the pressing\nwar, but I will supply you with a limited amount "
        <<"of ressources to begin with,\nalong with one hundred thousand credits. The rest you will have to obtain on\nyour "
        <<"own. My scouts have reported that a large asteroid full of raw metal ore\ntravels in a loose orbit around Kraul. "
        <<"You could harvest some materials from\nthere. I trust a man with your talent could accomplish this quite easily."
        <<"\nI want a full report 6 standard weeks from your arrival at the Senex sector.\nGood luck!\n"<<endl;
    system("PAUSE");
    do {
        system("cls");
        cout<<"Credits: "<<account.retrieve(1)<<setw(15)<<"Power: "<<account.retrieve(2)<<setw(15)<<"Metal: "<<account.retrieve(3)<<setw(15)<<"Day "<<account.retrieve(7)<<endl;
        cout<<"Crew: "<<account.retrieve(4)<<setw(15)<<"Jets: "<<account.retrieve(5)<<setw(15)<<"Starships: "<<account.retrieve(6)<<endl;
        cout<<"\nWelcome Admiral "<<name<<endl;
        cout<<"------"
            <<"\nOrders"
            <<"\n------"<<endl;
        cout<<"\n[1]Build"
            <<"\n[2]Mine"
            <<"\n[3]Train"
            <<"\n[4]Stats"
            <<"\n[5]Next"
            <<"\n[0]Quit"<<endl;
        cout<<"\nOrder: ";
        cin>>choice;
        cin.ignore();
        switch (choice)
          {
            case 0:
              return 0;
              break;
            case 1:
              account.build();
              break;
            case 2:
              account.mine();
              break;
            case 3:
              account.train();
              break;
            case 4:
              account.stats();
              break;
            case 5:
              account.timer();
              if (text == "OOO")
                 {
                   system("cls");
                   cout<<"\n\n\n"<<setw(10)<<"Well done Admiral "<<name<<". You have proven yourself well.\nCan I Interest you in a position among my battalion..."<<endl;
                   cout<<"\n"<<setw(20)<<"YOU ARE VICTORIOUS!!!!\n\n\n";
                   system("PAUSE");
                   return 0;
                 }
              else if (text == "PPP")
                 {
                   system("cls");
                   cout<<"\n\n\n"<<setw(10)<<"You disappoint me... 'Private' "<<name<<"!"<<endl;
                   cout<<"\n"<<setw(20)<<"GAME OVER!!!!!!!!\n\n\n";
                   system("PAUSE");
                   return 0;
                 }
              else
              break;
            default:
              break;
          }
       }while (choice != 99);
    return 0;
    }

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    53
    1) system("PAUSE") can be replaced by std::cin.get()
    2) There are no standard functions to do that, you may want to create a function that will output a bunch of new line characters ('\n')
    3) You're using outdated headers, you should use the new ones: iostream, cstdlib and iomanip (no .h)
    4) Finally, along with #3, you will have to put std:: before cout, cin and endl... or you might consider adding this at the top of your file
    Code:
    using std::cout;
    using std::cin;
    using std::endl;

  7. #7
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69
    Thanks alot! I'm gonna try that out and send it to my friend.

  8. #8
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69
    to make my own header file(to put it in the include place) how do i do that(to make my own clear screen function)? And if i put the
    Code:
    using std::cout;
    etc..., do I still have to put the std:: in front of all the couts and cins?

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    53
    1) I don't understand your question:
    2)
    Code:
    void clearscreen(const unsigned int num_lines) {
        for(int i = 0; i < num_lines; ++i)
            cout << endl;
    }
    3) If you use the using... you don't need to put std::

  10. #10
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69
    i tried making my own clear function:
    Code:
    int clear()
      {
         cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
      }
    how do i put the cursor in the top left corner?

  11. #11
    Registered User
    Join Date
    Dec 2004
    Location
    Mercer, PA
    Posts
    25
    just type using namespace std; right below your headers and you will no longer have to use std::cout
    and you can use system("cls"); on xp - iv tried it lol

  12. #12
    Registered User
    Join Date
    Jul 2004
    Posts
    53
    Code:
    using namespace std;
    That line is evil since it completely the std namespace's purpose that is protect the global namespace from name collision.
    Last edited by tilex; 12-21-2004 at 06:41 AM.

  13. #13
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >> to make my own header file(to put it in the include place) how do i do that(to make my own clear screen function)?

    Create and save a file called something like MyUtilities.h. Put your function in that file. Then you can include that file in whatever project you like, usually after std header files.

    #include <iostream>
    #include "MyUtilities.h"

    IF MyUtilities.h isn't in the same directory as the program you are compiling then you may need to use the entire path name rather than just the file name in the double quotes. If you know what you're doing you can even set it up so you use the angled brackets like for iostream instead of the double quotes, but I forget the exact criteria necessary for that option.


    #include <iostream>
    using namespace std;

    now all items in namespace std are global in scope. It's not evil, but it is not ideal not ideal either. It does remove a fair amount of clutter for those of us just starting out.

    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;

    now just cout, cin, and endl from namespace std have global scope. All other items in namespace std are tucked quitely away someplace.

    #include <iostream>

    int main()
    {
    std::cout << ....
    }

    now none of the items in namespace std have global scope so you have to use the std:: syntax everytime you use them. This provides the greatest control; and, IMHO, the most cluttered appearing code.


    >> how do i put the cursor in the top left corner?

    This requires use of Windows API functions or API functions that have been wrapped in another function. You can search the board for discussions, or links to other sites if you really want to do this.

  14. #14
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    Quote Originally Posted by ay_okay
    how do i put the cursor in the top left corner?
    u
    if you want to control the position of the cursor on the screen with that much detail you might want to use something like the *nix curse library (no idea of the windows equivalent) to do your output.

  15. #15
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Clearing the screen, moving the cursor about and a whole load more console stuff is included in my 6 part console tutorial starting here.

    I suspect the reason it is not working as it is, is because you have compiled it with a 16 bit DOS compiler and are trying to run it as a 32 bit executable. You could try to recompile it on his XP system, or run it in a DOS emulating shell.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed