I need help with this program its from a C++ book

This is a discussion on I need help with this program its from a C++ book within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <string> #include <ctime> #include <cstdlib> using namespace std; int main( void ) { srand(time(0)); // seed ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    6

    Post I need help with this program its from a C++ book

    Code:
    #include <iostream>
    #include <string>
    #include <ctime>
    #include <cstdlib>
    
    using namespace std;
    int main( void )
    {
    srand(time(0)); // seed random number genrator
    string name; // used to store string names
    bool end = false; // used to test if the user chose to quit
    bool lost; // used to test if the user lose the game
    
      // soteres the user's choice fromk the menu
    // units that the player starts with
    int menu_choice;
    int archers = 50;
    int catapults = 25;
    int swordsmen = 100;
    
    // units that the germains start witlh (random)
    // random numbers betweeen 70 and 20
    int g_archers = rand() % (51) + 20;
    int g_catapults = rand() % (41) + 10; // between 50 and 10
    // between 150 and 50
    int g_swordsmen  = rand() % (101) + 50;  
    // stores whice numbers coorrspond the whice menu choice
    int archers_menu, catapults_menu, swordsmen_menu;
    int fight_menu;
    cout <<"wlecome adventurer. what is your name?\n";
    cin >> name;
    cout << "Well." << name
    << " welcome to the Roman commader game. \n"
    << "\nYou are the commander kof the Roman Army"
    << "attacking the Germania.";
    while (!end) // main loop
    {
    // varibles to store ho many units the player sends
    int archers_sent=0; 
    int catapults_sent=0;
    int swordsmen_sent=0;
    cout << "\nYou have " << archers
    << " archers,"<<catapults
    <<" catapults, and "
    << swordsmen << " swordsmen.\n"
    << "\nGermania has " << g_archers
    << " archers,"
    << g_catapults << " catapults, and"
    << g_swordsmen
    << "sworedsmen.\n";
    do // pre battle loop
    // that are being used
    {
    int i = 1;
    if (archers > 0 &&
    ((archers - archers_sent)!=0))
    {
    archers_menu = i;
    cout << "[" << i << "] send Archers\n";
    i++;
    }
    
    else archers_menu = 0;
    if (catapults > 0 &&
    ((catapults - catapults_sent) !=0))
    {
    catapults_menu = i;
    cout << "[" << i<< "] Send Catapults\n";
    i++;
    }
    else catapults_menu = 0;
    if (swordsmen > 0 &&
    ((swordsmen - swordsmen_sent) != 0 ))
    {
    swordsmen_menu = i;
    cout <<"[" << i << "] send Swordsmen \n";
    i++;
    }
    else swordsmen_menu = 0;
    fight_menu = i;
    cout << "["<< i<<"] Go Fight\n";
    cin >> menu_choice;
    if (menu_choice == archers_menu)
    {
    do {
    cout << " how many archers"
    "would you like to send;
    cin >> archers_sent;
    }while (!(archers_sent > -1 
    && archers_sent <= archers));
    }
    else if (menu_choice == catapults_menu)
    {
    do {
    cout << " how many catapults"
    "would you like to send?\n";
    cin >> catapults_sent;
    }while(!(catapults_sent >  -1 && 
    catapults_sent <= catapults));
    }
    else if (menu_choice == swordsmen_menu)
    {
    do {
    cout << " how many swordsmen"
    "would you like to send?\n";
    cin >> swordsmen_sent;
    }
    while (!(swordsmen_sent > -1 &&
    swordsmen_sent <= swordsmen));
    
    }
    }
    // end pre battle loop
    while (menu_choice != fight_menu);
    cout << "\n Entering Battle ..\n";
    int archers_dead, catapults_dead;
    int swordsmen_dead;
    int g_archers_dead, g_catapults_dead; 
    int g_swordsmen_dead;
    
    // catapults kill 2 archers
    archers_dead = 2 * g_catapults;
    // each swordsman kills 1 catapult
    catapults_dead = g_swordsmen;
    // each archer kills 3 swordsmen
    swordsmen_dead = 3 * g_archers;
    
    g_archers_dead = 2 * catapults_sent;
    g_catapults_dead = swordsmen_sent;
    g_swordsmen_dead = 3 * archers_sent;
    
    // make sure the numbers do not go below 0
    archers = (archers_dead < archers) ?
    archers - archers_dead : 0;
    catapults = (catapults_dead < catapults) ?
    catapults - catapults_dead : 0;
    swordsmen = (swordsmen_dead < swordsmen) ?
    swordsmen - swordsmen_dead : 0 ;
    
    cout << " it was a long battle. "
    << archers_dead << " archers died.\n"
    << catapults_dead << " catapults died.\n"
    << swordsmen_dead << " swordsmen died \n";
    // if player army is dead then tehy lost 
    if (( archers + catapults + swordsmen) == 0)
    end = lost = true;
    // if germainum army is dead, player has won
    else if ((g_archers + g_catapults
    + g_swordsmen) == 0)
    {
    end = true;
    lost = false;
    }
    
    } // end of main game loop
    
    // display approriate ending message 
    if (lost)
    {
    cout << "\nYou Lost. Try again next time.\n";
    return 0;
    }
    cout << "\nCongratulations. you won!";
    return 0;
    }
    I need help with debuging this for learning porpuses
    here are the errors i keep getting
    Romanwar.cpp:87:1: warning: missing terminating " character
    Romanwar.cpp:87: error: missing terminating " character
    Romanwar.cpp: In function ‘int main()’:
    Romanwar.cpp:88: error: expected `;' before ‘cin’

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    you should indent your code

    as compiler says
    Code:
    						"would you like to send;
    something is missing in this line, don't you think?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Your indentation is terrible, no wonder you have a problem finding errors.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  4. #4
    Registered User BuzzBuzz's Avatar
    Join Date
    Feb 2009
    Posts
    89
    Code:
    << " would you like to send?\n";
    That's what I would put - for clarity.

    As the others said, indenting your code consistently will help you in the future and improve the speed at which you can debug - it'll also help others to help you with it by making it easier to follow. There are a couple of standards for indenting your code, adopting one of them would be a good idea

  5. #5
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    I use visual studio 2005 and when you highlight the code and press ALT+F8 it indents it for you. If your using that it's real handy

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    6

    dark21

    thanks guys. by indent you mean add this right
    <<" lines of code ";<<
    or do you mean i should dobule space my code or do both?

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Quote Originally Posted by dark21 View Post
    thanks guys. by indent you mean
    Let me google that for you
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User BuzzBuzz's Avatar
    Join Date
    Feb 2009
    Posts
    89
    Quote Originally Posted by dark21 View Post
    thanks guys. by indent you mean add this right
    <<" lines of code ";<<
    or do you mean i should dobule space my code or do both?
    Indentation is used primarily for your benefit. You indent and space your code so that it's easy for YOU to read and navigate. Your compiler doesn't care whether or not you indent your code or write your entire program in one huge line. However, with indenting styles, if you intend to write code that other people are going to read (for example if you are working as a coder, or are posting your code to a forum for help/advice/kudos) it is advisable to conform to one of the standard indentation schemes.

    At the very least consistent indenting will enable you read your own code quicker and spot issues that you may miss if it is flat.

    non-indented:
    Code:
    using namespace std;
    int main( void )
    {
    srand(time(0)); // seed random number genrator
    string name; // used to store string names
    bool end = false; // used to test if the user chose to quit
    bool lost; // used to test if the user lose the game
    
    
    [...sic]
    
    
    cout << "\nYou have " << archers
    << " archers,"<<catapults
    <<" catapults, and "
    << swordsmen << " swordsmen.\n"
    << "\nGermania has " << g_archers
    << " archers,"
    << g_catapults << " catapults, and"
    << g_swordsmen
    << "sworedsmen.\n";
    do // pre battle loop
    // that are being used
    {
    int i = 1;
    if (archers > 0 &&
    ((archers - archers_sent)!=0))
    {
    archers_menu = i;
    cout << "[" << i << "] send Archers\n";
    i++;
    }
    indented (this is the style that I use):
    Code:
    using namespace std;
    int main()
    {
        srand(time(0)); // seed random number genrator
        string name; // used to store string names
        bool end = false; // used to test if the user chose to quit
        bool lost; // used to test if the user lose the game
    
    
    [...sic]
    
    
        cout << "\nYou have " << archers
             << " archers,"<<catapults
             <<" catapults, and "
             << swordsmen << " swordsmen.\n"
             << "\nGermania has " << g_archers
             << " archers,"
             << g_catapults << " catapults, and"
             << g_swordsmen<< "sworedsmen.\n";
             
       do // pre battle loop
       // that are being used
       {
               int i = 1;
               if (archers > 0 &&((archers - archers_sent)!=0))
               {
                    archers_menu = i;
                    cout << "[" << i << "] send Archers\n";
                    i++;
               }
               
       [....and so on]
    The style you use is personal preference and a lot of editors automatically indent for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. URGENT: Help wanted...in C
    By iamjimjohn in forum C Programming
    Replies: 16
    Last Post: 05-18-2007, 05:46 AM
  2. Replies: 6
    Last Post: 11-29-2004, 09:28 PM
  3. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 04:23 PM
  4. Books on C and C++
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-28-2002, 03:18 PM
  5. Newbie - MFC code from a book in VC++.Net
    By Guardian in forum Windows Programming
    Replies: 2
    Last Post: 04-27-2002, 07:17 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21