C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 15th, 2007, 02:11 AM
#<Hspd7> #<Hspd7> is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 12 #<Hspd7> User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 13 m 2 sec
Reputation Power: 0
Any advice?

Hello all, I'm a fairly new programmer, just started learning about class storages and stuff.

What I'm posting for is, I want to know what some of you think of my code so far, let me know if there are any shortcuts I may be able to take in the future on a program such as this.

This program was one I decided to try and make for my gaming team as a test on the various military tactics we teach so far.

I have global variables used in the function I have finished the others are just pre-declared for future use.

Code:
#include <iostream>
#include <iomanip>
#include <cstdio>
using namespace std;

void inTro()
{
     cout <<"This program will cover certain aspects of your training here with\n"
         <<"the USM."
         <<"\n"<<endl;
return;
}
/**********************************Declarations!***  ****************************/
void rangerFile(void);     // All variables are declared, grouped under their
int q1ranCalc;             //    corresponding functions.
int q2ranCalc;

void wedgeFormation(void);
int q1wedCalc;
int q2wedCalc;

void tacticalFormation(void);
int q1tacCalc;
int q2tacCalc;

void stackUp(void);
int q1stacCalc;
int q2stacCalc;

void threeWay(void);
int q1threeCalc;
int q2threeCalc;

void fourWay(void);
int q1fourCalc;
int q2fourCalc;

void roomClearing(void);
int q1roomCalc;
int q2roomCalc;

void conVoy(void);
int q1conCalc;
int q2conCalc;

void moveMents(void);
int q1moveCalc;
int q2moveCalc;

enum Status {CORRECT = 1, WRONG};
Status answerStatus;


/**********************************Main Program!*******************************/


int main()
{
    
    inTro();
    
    cout <<"In this portion of the program you will be tested on your knowledge\n"
         <<"of the ranger file formation in combat.\n"<<endl;
system("PAUSE");
          
          rangerFile();
       

system("PAUSE");
return 0;
}
/**************************************************  ****************************/

void rangerFile()
{
     int answer1;
     int answer2;
     
     do
     {
     cout << "\n\n\n(Question 1:) Enter 1, 2, 3 or 4 according to the correct answer\n"
          <<"below.\n\n"
          <<"\t(1)-    In a ranger file the first man points to the 12 o'clock and\n"
          <<"\t\tall the others face to the right side.\n\n"
          <<"\t(2)-    In a ranger file the first man points to the 12 o'clock and\n"
          <<"\t\tall the others alternate left right respectively, the last man\n"
          <<"\t\tscanning the rear.\n\n"
          <<"\t(3)-    In a ranger file the first man points to the 12 o'clock\n"
          <<"\t\tall the others face to the rear staggering for best area of\n"
          <<"\t\tfire.\n\n"
          <<"\t(4)-    In a ranger file the first man points to the 12 o'clock\n"
          <<"\t\tall the others play hide and seek together.\n\n"
          <<"Enter your answer here: ";
     cin  >>answer1;
                    if(answer1 > 4 || answer1 < 1)
                               cout <<"\n\n\n\nYOUR NOT THAT STUPID I HOPE!!!! TRY AGAIN!\n\n\n";
                               system("PAUSE");
     }while(answer1 > 4 || answer1 < 1);
     
     switch(answer1)
     {
                    case 1:
                          answerStatus = WRONG;
                          break;
                    case 2:
                         answerStatus = CORRECT;
                         break;
                    case 3:
                         answerStatus = WRONG;
                         break;
                    case 4:
                         answerStatus = WRONG;
                         break;
                    default:
                            cout <<"SHOULD NEVER GET HERE!";
                            break;
                    
     }  
     int q1ranCalc = answerStatus;
                  
     do
     {
     cout << "\n\n\n(Question 2:) Enter 1, 2, 3 or 4 according to the correct answer\n"
          <<"below.\n\n"
          <<"\t(1)-    Ranger file is used when visibility is low and the team\n"
          <<"\t\tmust provide 360 degree security.\n\n"
          <<"\t(2)-    Ranger file is used in open areas where you are more\n"
          <<"\t\texposed to enemy fire.\n\n"
          <<"\t(3)-    Ranger file is used when members of your team have to\n"
          <<"\t\ttake a ****, it keeps the team together and safe.\n\n"
          <<"\t(4)-    Ranger file is used when your team has come under attack\n"
          <<"\t\tand needs to quickly form a line and confront the enemy.\n\n"
          <<"Enter your answer here: ";
     cin  >>answer2;
                    if(answer2 > 4 || answer2 < 1)
                               cout <<"\n\n\n\nYOUR NOT THAT STUPID I HOPE!!!! TRY AGAIN!\n\n\n";
                               system("PAUSE");
     }while(answer2 > 4 || answer2 < 1);
     
     switch(answer2)
     {
                    case 1:
                          answerStatus = CORRECT;
                          break;
                    case 2:
                         answerStatus = WRONG;
                         break;
                    case 3:
                         answerStatus = WRONG;
                         break;
                    case 4:
                         answerStatus = WRONG;
                         break;
                    default:
                            cout <<"SHOULD NEVER GET HERE!";
                            break;
                    
     }  
     int q2ranCalc = answerStatus;
          
}


Let me explain a bit about what I'm shooting for...

Everything I've got so far in this code, works exactly how I want it too, note that I have only finished the void rangerFile() function, but the others I plan on doing pretty much the same way.

I declared the global variables under their corresponding functions to kinda help with understandability and readability.

Eventually in the end I will take all the global variables, using the answerStatus enum thing to calculate a final test percentage score.

Sorry if my code is hard to understand or read.

Please provide me with any advice you think I may be able to use. thanks

Reply With Quote
  #2  
Old January 15th, 2007, 10:39 PM
patmo98 patmo98 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 83 patmo98 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 5 m 47 sec
Reputation Power: 12
If I were you I would write two functions something like this

c++ Code:
Original - c++ Code
  1. int askQuestion( int questionNum, const char* ans1, const char* ans2, const char* ans3, const char* ans4)
  2. {
  3. char responce = NULL;
  4. do
  5.      {
  6.      cout << "\n\n\n(Question "  <<  questionNum  <<  ":) Enter 1, 2, 3 or 4 according to the correct answer\n"
  7.           <<"below.\n\n"
  8.           <<"\t(1)-    ans1  <<  "\n\n"
  9.           <<"\t(2)-    ans2  <<  "\n\n"
  10.           <<"\t(3)-    ans3  <<  "\n\n"
  11.           <<"\t(4)-    ans4  <<  "\n\n"
  12.           <<"Enter your answer here: ";
  13.      cin  >>responce ;
  14.                     if(responce > '4' || responce < '1')
  15.                                cout <<"\n\n\n\nYOUR NOT THAT STUPID I HOPE!!!! TRY AGAIN!\n\n\n";
  16.                                system("PAUSE");
  17.      }while(responce > '4' || responce < '1');
  18.      
  19.      return responce - '0';
  20. }
  21.  
  22. int testAnswer( int givenAnswer, int realAnswer )
  23. {
  24.      if( givenAnswer == realAnswer switch(answer1)
  25.      {
  26.            return CORRECT;
  27.      }
  28.      else
  29.      {
  30.           return WRONG;
  31.      }
  32. }


That would allow you to simplify this:

c++ Code:
Original - c++ Code
  1.  
  2.      int answer1;
  3.      int answer2;
  4.      
  5.      do
  6.      {
  7.      cout << "\n\n\n(Question 1:) Enter 1, 2, 3 or 4 according to the correct answer\n"
  8.           <<"below.\n\n"
  9.           <<"\t(1)-    In a ranger file the first man points to the 12 o'clock and\n"
  10.           <<"\t\tall the others face to the right side.\n\n"
  11.           <<"\t(2)-    In a ranger file the first man points to the 12 o'clock and\n"
  12.           <<"\t\tall the others alternate left right respectively, the last man\n"
  13.           <<"\t\tscanning the rear.\n\n"
  14.           <<"\t(3)-    In a ranger file the first man points to the 12 o'clock\n"
  15.           <<"\t\tall the others face to the rear staggering for best area of\n"
  16.           <<"\t\tfire.\n\n"
  17.           <<"\t(4)-    In a ranger file the first man points to the 12 o'clock\n"
  18.           <<"\t\tall the others play hide and seek together.\n\n"
  19.           <<"Enter your answer here: ";
  20.      cin  >>answer1;
  21.                     if(answer1 > 4 || answer1 < 1)
  22.                                cout <<"\n\n\n\nYOUR NOT THAT STUPID I HOPE!!!! TRY AGAIN!\n\n\n";
  23.                                system("PAUSE");
  24.      }while(answer1 > 4 || answer1 < 1);
  25.      
  26.      switch(answer1)
  27.      {
  28.                     case 1:
  29.                           answerStatus = WRONG;
  30.                           break;
  31.                     case 2:
  32.                          answerStatus = CORRECT;
  33.                          break;
  34.                     case 3:
  35.                          answerStatus = WRONG;
  36.                          break;
  37.                     case 4:
  38.                          answerStatus = WRONG;
  39.                          break;
  40.                     default:
  41.                             cout <<"SHOULD NEVER GET HERE!";
  42.                             break;
  43.                    
  44.      } 
  45.      int q1ranCalc = answerStatus;


into this

c++ Code:
Original - c++ Code
  1. int answer;
  2. int answer = askQuestion( "In a ranger file the first man points to the 12 o'clock and\n\t\tall the others face to the right side.",
  3.                           "In a ranger file the first man points to the 12 o'clock and\n\t\tall the others alternate left right respectively, the last man\n\t\tscanning the rear.",
  4.                           "In a ranger file the first man points to the 12 o'clock\n\t\tall the others face to the rear staggering for best area of\n\t\tfire.",
  5.                           "In a ranger file the first man points to the 12 o'clock\n\t\tall the others play hide and seek together." );
  6.  
  7. int q1ranCalc = testAnswer( answer, 2 );

Reply With Quote
  #3  
Old January 15th, 2007, 10:53 PM
#<Hspd7> #<Hspd7> is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 12 #<Hspd7> User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 13 m 2 sec
Reputation Power: 0
thanks, that might be a bit easier.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Any advice?


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap