Hello! I'm new here, so correct me if this is the wrong location... but I'm trying to make a program for the Minecraft server I'm on, and it requires that if someone wants to make a map that is used on the server, that there's a .xml file that shows all the information, like items the person spawns with, etc. I wrote the majority of the program recently, so I can remember almost all of what happens when :P
Here's my program:

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

int screen()
{
    cout << ".-------------------------------." << endl;
    cout << "|                               |" << endl;
    cout << "|  Welcome to the XML Builder!  |" << endl;
    cout << "|     Made by tratiikxt1t4n     |" << endl;
    cout << "|                               |" << endl;
    cout << "*-------------------------------*\n" << endl;
}

int output()
{
    int authornumber, filters, filter, shots;
    int helmet, chestplate, leggings, boots, sword, bow, arrows, enchantnum;
    string name, objective, authname1, authname2;
    string authname3, authname4, contrib1, contrib2, contrib3;
    string contrib4, team1, team2, color1, color2, playernum;
    string name1, name2, enchant1, enchant2;

    float version, velocity;

    cout << "Generating .xml file now..." << endl;
    fstream file;
    file.open("Your_XML_File.xml",ios::trunc|ios::out);
    file << "<?xml version=\"1.0\"?>" << endl;
    file << "<map proto=\"1.0.0\">" << endl;
    file << "<name>" << name << "</name>" << endl;
    file << "<version>" << version << "</version>" << endl;
    file.close();
}

int main()
{
    int authornumber, filters, filter, shots, output();
    int helmet, chestplate, leggings, boots, sword, bow, arrows, enchantnum;
    string name, objective, authname1, authname2;
    string authname3, authname4, contrib1, contrib2, contrib3;
    string contrib4, team1, team2, color1, color2, playernum;
    string name1, name2, enchant1, enchant2;

    float version, velocity;

    screen();
    cout << "Please Type the name of the map: ";
    cin >> name;
    cout << "Version of the map: ";
    cin >> version;
    system("clear");
    screen();
    cout << "Objective of the map: ";
    cin >> objective;
    system("clear");
    screen();
    cout << "Number of Authors: ";
    cin >> authornumber;

    if ( authornumber == 1 )
    {
        cout << "\nAuthor name: ";
        cin >> authname1;
        cout << "Author contribution: ";
        cin >> contrib1;
    }
    else if ( authornumber == 2 )
    {
        cout << "\nAuthor name: ";
        cin >> authname1;
        cout << "Author contribution: ";
        cin >> contrib1;
        cout << "\nAuthor name: ";
        cin >> authname2;
        cout << "Author contribution: ";
        cin >> contrib2;
    }
    else if ( authornumber == 3 )
    {
        cout << "\nAuthor name: ";
        cin >> authname1;
        cout << "Author contribution: ";
        cin >> contrib1;
        cout << "\nAuthor name: ";
        cin >> authname2;
        cout << "Author contribution: ";
        cin >> contrib2;
        cout << "\nAuthor name: ";
        cin >> authname3;
        cout << "Author contribution: ";
        cin >> contrib3;
    }
    else if ( authornumber >= 4 )
    {
        cout << "\nAuthor name: ";
        cin >> authname1;
        cout << "Author contribution: ";
        cin >> contrib1;
        cout << "\nAuthor name: ";
        cin >> authname2;
        cout << "Author contribution: ";
        cin >> contrib3;
        cout << "\nAuthor name: ";
        cin >> authname3;
        cout << "Author contribution: ";
        cin >> contrib3;
        cout << "\nAuthor name: ";
        cin >> authname4;
        cout << "Author contribution: ";
        cin >> contrib4;
    }
    system("clear");
    screen();

    cout << "Team colors: ";
    cin >> color1;
    cout << "\n             ";
    cin >> color2;
    system("clear");
    screen();
    cout << "Team names: ";
    cin >> name1;
    cout << "\n            ";
    cin >> name2;
    system("clear");
    screen();
    cout << "Max number of players on each team: ";
    cin >> playernum;
    system("clear");
    screen();

    blocks:
    cout << "1. Blocks can be placed/broken" << endl;
    cout << "2. Blocks cannot be placed/broken" << endl;
    cin >> filters;
    if ( filters == 1 )
    {
        filter = 1;
    }
    else if ( filters == 2 )
    {
        filter = 0;
    }
    else
    {
        cout << "Incorrect option!" << endl;
        goto blocks;
    }
    system("clear");
    screen();
    cout << "1. Shoot arrows" << endl;
    cout << "2. Shoot enderpearls" << endl;
    cin >> shots;
    cout << "Velocity: ";
    cin >> velocity;
    system("clear");
    screen();

    cout << "What items do you want the player to use?\nEnter '0' to skip that item.\nPlease use their IDs." << endl;
    cout << "Helmet: ";
    cin >> helmet;
    cout << "Chestplate: ";
    cin >> chestplate;
    cout << "Leggings: ";
    cin >> leggings;
    cout << "Boots: ";
    cin >> boots;
    cout << "Sword: ";
    cin >> sword;
    cout << "Number of Enchantments: ";
    cin >> enchantnum;
    if ( enchantnum == 1 )
    {
        cout << "Enchantment: ";
        cin >> enchant1;
    }
    if ( enchantnum == 2 )
    {
        cout << "Enchantment: ";
        cin >> enchant1;
        cout << "             ";
        cin >> enchant2;
    }
    cout << "Bow [1. Yes; 2. No]: ";
    cin >> bow;
    if ( bow == 1 )
    {
        cout << "Arrows: ";
        cin >> arrows;
    }

    output();
}

the only problem is that the output .xml file is incorrect. It's supposed to say <name>NAMEHERE</name>, but it only says <name></name>. Any help? Thanks!

Recommended Answers

All 7 Replies

Well it appears you never set a value to name so I'm guessing thats why that node is empty...

Oh wait I see, sorry didn't browse through everything, so you say name is the only problem you have? Thats odd, try passing the name variable into the function and see if that works.

okay. ... slight problem. idk what you mean by that o_O. this is my first year w/ C++, and I don't know all that much yet :P

You're variables are being filled in the scope of main(), so once you call output() those variables are no longer in scope.

The 'dirty' solution would be to declare the variables as global variables. A cleaner way would be to define a struct to hold the variables, fill the struct and pass either the struct instance or pointer to the output function.

wow... my mind was blown. I understand what you mean by the variables not being in the scope. so how would I do the second way?

EDIT scratch that! I get what you mean by that, so I just changed it now! THANK YOU SO MUCH!

I gather you did something like (just a simple example):

#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::endl;

struct XML_Params{
    string name;
    float version;
};

/* passing the instance of XML_Params */
int output(const XML_Params& params)
{
    // if something goes awry
    // return a non zero value to signify an error
    cout << params.name << " - " << params.version << endl;
    return 0;
}

int main()
{
    XML_Params xparams;
    memset(&xparams, 0, sizeof(XML_Params));
    xparams.name = "blooper";
    xparams.version = 3.142;

    int iret = output(xparams);
    if (iret != 0)
    {
        //handle error
    }

    getchar();

    return 0;
}

If there's nothing further, then mark the thread as solved. :)

yeah thx :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.