-8

Why doesn't this code compile?

#include <iostream>

using namespace std;

#define $_ cout << "Use '$_' befor commands to execute them";
#define $_Help cout << "Type $_ befor each command to execute it, type the command without $_ to view information about the command. Commands: Help, Quiz";
#define $_Quiz int quiz(){int score = 0; std::string name; std::string quest1; cout << "What is your name? "; cin >> name; cout << "Hello, " << name << endl; score++; cout << "Is pi > 3.14159? yes or no? "; cin >> quest1; if (quest1 == "yes"){cout << "Correct!"; score++;}else if (quest1 == "no"){cout << "Incorrect, pi has more numbers that just 3.14159.";}cout << " Your score is, " << score << endl; return 0;};
#define Help cout << "Display all of the commands";
#define Quiz cout << "A simple quiz(only one question right now)";
#define testForInputComm if(termComms == "$_Help"){$_Help;}else if(termComms == "$_Quiz"){$_Quiz;}else if(termComms == "Help"){Help;}else if(termComms == "Quiz"){Quiz;};

int main() {
    while(true)
    {
         std::string termComms;
         std::cin >> termComms;
         testForInputComm;
         return 0;
    }
}

int initiation()
{
    cout << "Type a command!";
    cout << "Type: $_Help to view all of the commands";
    return 0;
}

The error message is:

main.cpp: In function 'int main()':                                                                                                                              
main.cpp:7:26: error: a function-definition is not allowed here before '{' token                                                                                 
 #define $_Quiz int quiz(){int score = 0; std::string name; std::string quest1; cout << "What is your name? "; cin >> name; cout << "Hello, " << name << endl; sc
                          ^                                                                                                                                      
main.cpp:10:91: note: in expansion of macro '$_Quiz'                                                                                                             
 #define testForInputComm if(termComms == "$_Help"){$_Help;}else if(termComms == "$_Quiz"){$_Quiz;}else if(termComms == "Help"){Help;}else if(termComms == "Quiz"
                                                                                           ^                                                                     
main.cpp:17:10: note: in expansion of macro 'testForInputComm'                                                                                                   
          testForInputComm;                                                                                                                                      
          ^                                                                                                                                                      
main.cpp:27:1: error: expected '}' at end of input                                                                                                               
 }                                                                                                                                                               
 ^                                                                                                                                                               
main.cpp:27:1: error: expected '}' at end of input                                                                                                               
main.cpp:27:1: error: expected '}' at end of input
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Surge12
  • 165
  • 2
  • 9

2 Answers2

2

You need to embed that code into a main() function:

#include <iostream>
#include <string>

int main() {
    while(true)
    {
         std::string termComms;
         std::cin >> termComms;
         // testForInputComm;
    }
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Thanks! Unfortunately, when I did that, It brought up a bunch more errors. I know how to fix them, but would you like to see them and the rest of the code? – Surge12 May 05 '16 at 15:19
  • 3
    @xoorath regarding your edit: a return statement is not required for main. an implicit `return 0;` will be generated. – NathanOliver May 05 '16 at 15:22
  • 2
    @Surge12 _"... but would you like to see them and the rest of the code?"_ Probably not. – πάντα ῥεῖ May 05 '16 at 15:24
1

You're forgetting:

1) entry point

2) include statements

3) variable declaration before you're using the variables.

4) namespace before cin

It looks like a bad idea on the whole, but this should compile for you at the very least.

#include <string>
#include <iostream>

int main() {
    while(true)
    {
        std::string termComms;
        std::cin >> termComms;
        // use termComms here
    }
    return 0;
}
xoorath
  • 425
  • 2
  • 15
  • I have a few errors to fix... – Surge12 May 05 '16 at 15:27
  • Your code is missing the namespace on `cin`, @Surge12. And sure, do fix your few errors. – xoorath May 05 '16 at 15:29
  • @πάντα ῥεῖ: In stack overflows infinite wisdom, I can't reply to your comment without 50 rep. So yeah: I didn't know you could omit the return there, thanks. Looks gross to me, but hey - to each their own. – xoorath May 05 '16 at 15:32
  • Yup, you'll have to fix your code. I'd suggest learning C++ before trying to paste that in there and hoping it works. – xoorath May 05 '16 at 17:00