-3

I'm new to C++ (not programming in general, just C++) and I'm learning to program in C++ with a subscription to Pluralsight. I'm writing a practice program (a set of games through the computer's console) and I'm stuck on something. While working on a Tic-Tac-Toe game, I want to call a formula for the board that I won't have to rewrite every time. Therefore I defined a set of strings to work for me, but I cannot figure out how to call my user defined formula. I'm not going to post all of the code, because it is very long, but I will show your the parts you need (if a line has "...." on it, that means I removed multiple lines of code to make it fit better for this site). Incase you're wondering, I'm using Microsoft Visual Studio Community 2017 RC and C++14.

HEADER FILE:

#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
using namespace std;

MAIN FILE:

....

#define TTTBoard () \
{ \
    system("cls"); \
    cout << "\n\n                           Let's play Tic-Tac-Toe!\n\n\n"; \
    cout << "                               A       B       C    " << endl; \
    cout << "                            _______________________ " << endl; \
    cout << "                           |       |       |       |" << endl; \
    cout << "                       1   |   " << PlayerSymA1 << "   |   " << PlayerSymB1 << "   |   " << PlayerSymC1 << "   |" << endl; \
    cout << "                           |_______|_______|_______|" << endl; \
    cout << "                           |       |       |       |" << endl; \
    cout << "                       2   |   " << PlayerSymA2 << "   |   " << PlayerSymB2 << "   |   " << PlayerSymC2 << "   |" << endl; \
    cout << "                           |_______|_______|_______|" << endl; \
    cout << "                           |       |       |       |" << endl; \
    cout << "                       3   |   " << PlayerSymA3 << "   |   " << PlayerSymB3 << "   |   " << PlayerSymC3 << "   |" << endl; \
    cout << "                           |_______|_______|_______|" << endl; \
}

....

int main()
{
    //This is where I want to call my TTTBoard formula
}

I tried to call it multiple ways, but nothing worked. Below is what I already tried. I know some of what I tried didn't make complete sense, but I was annoyed that I couldn't get it to work, so I tried it all anyway.

TTTBoard

TTTBoard;

TTTBoard()

TTTBoard();

TTTBoard()
{
}

TTTBoard();
{
}

Thank you in advance for the help!!!

1 Answers1

4

Preprocessor macros are not called. Instead they are expanded, meaning the body of the macro is inserted in place of the macro invocation, with arguments replaced.

So if you have a macro like

#define FOO() { \
    bar();      \
}

And then using it

int main()
{
    FOO()
}

What the preprocessor creates and the compiler sees is

int main()
{
    { bar(); }
}

The above example also shows how to use a function-like macro.

Lastly, you don't need macros. In a case like yours it makes much more sense to use actual functions. In modern C++ there are seldom much needs for macros.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • **some programmer dude:** First, thank you for answering so quickly! Second, I inserted my code inside my main code just how you showed: 'TTTBoard()' and I received the following errors: Error (active) E0029 expected an expressionError C2059 syntax error: ')' Error C2143 syntax error: missing ';' before '{' Third, you said that I do not need macros, but I do not want to rewrite my board code over and over again. That is why I created the TTTBoard definition. Is there another way to insert code into my program without rewriting it over and over again? – Stephen R. Hayes Feb 20 '17 at 07:27
  • @StephenR.Hayes All compiler front-end programs will have an option to stop after the preprocessor step, so you can examine the preprocessed code. With GCC it's done with the `-E` option (as in `g++ -E source-file.cpp -o preprocessed-source.cpp`). And why would you need to "rewrite" your code if you made a function? There would not need to be any more "rewriting" than when using macros. – Some programmer dude Feb 20 '17 at 07:32
  • Regarding rewriting, I meant that I was using macros because I needed to keep calling the board up every time the player picks where to place there X or O, and I didn't want to keep rewriting it. Also I cannot find out how stop and check the preprocessed code using my program (Microsoft Visual Studio Community 2017 RC). I'm sorry to be a hassle and thank you for helping. I can usually figure out code easily; this is the first time I have ever had to ask a question to someone (outside my professors in college). So I'm getting annoyed with myself for not figuring this out. – Stephen R. Hayes Feb 20 '17 at 07:47
  • @StephenR.Hayes And that's exactly what functions are for. You write the function once, similar to the macro, and then you invoke the function as many times as you need. The function itself and its contents is written only once though. Functions are generally safer, can often be better optimized, and doesn't lead to code duplication. – Some programmer dude Feb 20 '17 at 07:49
  • Then if #define is called a macro, how would I write this as a function (would I just change '#define' to 'int' and also remove all of the '/' )? If this works, that is great, but I wish I could figure out how to run it as a preprocessor directive (I'm still not sure why it won't work for me). – Stephen R. Hayes Feb 20 '17 at 08:04
  • 1
    @StephenR.Hayes How about `void TTTBoard() { /* All the output here */ }`. Call as `TTTBoard();`. And if there's an error in the code then it will be easier to detect since now it will be different lines and the compiler can sow you the exact line the error is on. With a macro it's all one big single line which makes it hard to find errors. Also, the error might actually be *before* you invoke the macro. Like if you're missing a brace, parenthesis or a semicolon. – Some programmer dude Feb 20 '17 at 08:07
  • Thank you *some programming dude*, I still cannot figure how to get the #define to be called, but void TTTBoard(){....} worked perfectly! – Stephen R. Hayes Feb 20 '17 at 08:32
  • using a real function might be better in your case, but just to answer your question maybe look here to see how you can actually call a function macro http://stackoverflow.com/questions/163365/how-do-i-make-a-c-macro-behave-like-a-function – xander Feb 20 '17 at 08:37