Help needed, code is not working

Goodmorning,

My code is not working in C++. It says: “function definition is not allowed here”.

This is the code:

#include<iostream>
#include<stdlib.h>
#include<string>
#include<istream>
#include<pthread.h>
using namespace std;

struct user_choice{
    int number;
    int prize[3];
    string name;
}product;

struct customer_order{
    int customer_id;
    string status;
}customer[4];

void Menu(){
    cout << "" << endl;
    cout << "Main Menu" << endl;
    cout << "Choose one of the options below:" << endl;
    cout << "1.Choose product" << endl;
    cout << "2.View order form" << endl;
    cout << "3.Check order status" << endl; //mutex becuase only one person can check at a time
    cout << "4.Exit" << endl;
}
void choose_product(){
    cout << "" << endl;
    cout << "What kind of product are you looking for? Shoes, tracksuit or shinguards?" << endl;
    cout << "" << endl;
    
    int kind_of_product;
    cin >> kind_of_product;
    
    int shoes = 0;
    if (kind_of_product == shoes){
    cout << "We have the new nike collection, Do you want to order them?" << endl;
    
    string order;
    cin >> order;
    if (order == "yes"){
        cout<< "Thank you for ordering the shoes!" << endl;
    }
    
    int tracksuit = 0;
    if (kind_of_product == tracksuit){
    cout << "We only have size S, do you want to order it?" << endl;
        
    string order;
    cin >> order;
    if (order == "yes"){
    cout<< "Thank you for ordering the tracksuit!" << endl;
    }
    
    int shinguards = 0;
    if (kind_of_product == shinguards){
        cout << "We only have a few left. Would you like to order it?" << endl;
    
    string order;
    cin >> order;
    if (order == "yes"){
        cout<< "Thank you for ordering the shinguards!" << endl;
    }
    }

    else{
        cout << "That is not an option." << endl;
    }
}
    
        void place_order(){
    cout << "Write down your name:" << endl;
    string name;
    cin >> name;
    product.name = name;
    cout << "We accepted your order. We will send your product as soon as possible!" << endl;
    cout << "You can check your order status in Main Menu." << endl;
}

        void order_form(){
    string shoes;
    string tracksuit;
    string shinguards;
    product.prize[shoes]=69,95;
    product.prize[tracksuit]=55;
    product.prize[shinguards]=15;

    if (kind_of_product == shoes){
    cout << "" << endl;
    cout << "You ordered the shoes" << endl;
    cout << "Prize to pay: " << product.prize[kind_of_product] << " Euro" << endl;
        }

    if (kind_of_product == tracksuit){
    cout << "" << endl;
    cout << "You ordered the tracksuit" << endl;
    cout << "Prize to pay: " << product.prize[kind_of_product] << " Euro" << endl;
    }

    if (kind_of_product == shinguards){
    cout << "" << endl;
    cout << "You ordered the shinguards" << endl;
    cout << "Prize to pay: " << product.prize[kind_of_product] << " Euro" << endl;
        }
    
    }
    


pthread_t request[4];
pthread_mutex_t locker;

// function to check status by id
void check_status(int id){
    for(int a=0;a<=3;a++){
        if(customer[a].customer_id == id){
            cout << customer[a].status << endl;
        }
    }
}

// demand of checking the status
void *demand(void* arg){
    pthread_mutex_lock(&locker);
    cout << " " << endl;
    cout << "Hello, I'm customer number " << pthread_self() -  1 << endl;
    int my_id = (customer[pthread_self()-2].customer_id);
    cout << "My ID is:" << my_id << endl;
    check_status(my_id);
    cout << " " << endl;
    pthread_mutex_unlock(&locker);
}

int main() {
    // Print Menu
    cout << "Welcome to the best online Soccer Store of the world!"<< endl;
    Menu();
    cin >> answer;
    int kind_of_product;
    switch(answer){
        case 1:
            choose_product();
            break;
        case 2:
            order_form();
            break;
        case 3: // simulation for checking status for 4 customers at the same time
            // creating 4 customers
            customer[0].customer_id = 999;
            customer[0].status = "Product has been sent";
            customer[1].customer_id = 888;
            customer[1].status = "We are preparing your shipping ";
            customer[2].customer_id = 555;
            customer[2].status = "Product has been sent";
            customer[3].customer_id = 111;
            customer[3].status = "We are preparing your shipping ";
            //creating mutex (locker so as not to mix the lines)
            pthread_mutex_init(&locker, NULL);
            // creating threads (customers demands of checking status)
            for(int i=0;i<=3;i++){
                pthread_create(&request[i],NULL,&demand,NULL);
                }
            pthread_join(request[0],NULL);
            pthread_join(request[1],NULL);
            pthread_join(request[2],NULL);
            pthread_join(request[3],NULL);
            pthread_mutex_destroy(&locker);
            break;
        case 4:
            // EXIT
            break;
    }
    }
}

}

Is there anyone who can help me out?

Best regards,
Abel

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

It looks like you probably have an issue with your brackets. I would put this into a tool that fixes your indentation. Then you can see where your brackets went wrong.

Thanks. Do you have any suggestions which kind of tool I should use?

Personally, I use astyle. I think repl.it also has an auto-format button.

Your compiler is probably telling you what line the error is occurring in, so you have a place to start counting opening and closing brackets. If you are using an editor it probably also has tools for identifying matching brackets. Even with tools though, it often comes down to looking at each block and asking yourself if it’s correct.

1 Like

Thanks for your helping. But there is nothing wrong with the brackets.

After every void there is an error that says: “function definition is not allowed here”.

Have you declared your functions before defining them?
Look at this Functions in C++ guide’s section “Declaring, Defining and Calling a Function”.

This is not valid C++. This is your problem.

You can’t define a struct-arry combo there. If you want an array of structs, you need to make that a separate variable like this:

customer customers[4];

See C - Structures

1 Like

@ArielLeslie As long as the definition comes before main it shouldn’t matter (declarations).

@JeremyLT I’m really not a C/C++ guy, and I’m sure you know more than me, but are you positive about that?

structures

Because structures are types, they can also be used as the type of arrays to construct tables or databases of them:

struct movies_t {
  string title;
  int year;
} films [3];

@adj1 Check the brackets in the choose_product function. But I believe you have more errors in order_form (the usage of product.prize seems incorrect and kind_of_product is not available in the function scope) and in main (answer is undeclared), plus you seem to have two extra brackets after the main definition.

Again, I’m not a C/C++ guy and it has been a long time since I have done anything in C or C++ so these are just some observations I made that might be wrong.

Huh, I’ve never seen a struct used like that (probably because global variables shouldn’t really be used like that!), but it looks like it is legal C++. I’m not sure that it’s legal C. (edit: its not! That explains my confusion. C++ has some extra “crap” in it!)

Is #include <string> valid C++? C expects #include <string.h>

Also, how much code can you remove and still see the error?

That is the understatement of the year, C++ is crazy with how much stuff they keep adding.

Should be <string> (part of the C++ standard library). The .h is C and the C++ equivalent has a “c” prefix.

C library

Each header file has the same name as the C language version but with a “c” prefix and no extension. For example, the C++ equivalent for the C language header file <stdlib.h> is <cstdlib>.

You mean the “function definition is not allowed here” error? Basically if I fix or comment out the choose_product function, it goes away.

1 Like

Yeah, I was trying to point adj1 towards removing stuff until he could target what the issue was. Tragically, repl.it doesn’t actually seem to auto format C++ whitespace reasonably. If it had, the problem would have been obvious.