Server Help

Non-Subspace Related Coding - something wierd in c++

hellzlaker - Thu Jul 24, 2008 7:46 pm
Post subject: something wierd in c++
well I started making a program "quick server set up" that would set up subgame server very quickly (also will help me learn about pointers sa_tongue.gif )

so here is what program does, user puts it in the server folder, opens it and just types few values into the program that are needed to start server running.

here is the problem, when the program asks you to enter the name of the DefaultLevelFile I enter it, but for some reason it only saves to server.ini the first letter of what ever you typed, so if you typed in map.lvl it will say in server ini
Quote:
DefaultLevelFile=m
why is that?

Here is the source code
Code: Show/Hide
#include <windows.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

void c()
{
     system("cls");
}

void p()
{
     system("pause");
}

void writeINI(char *section, char *key, char *writeThis, char *path)
{
              WritePrivateProfileStringA(section,key,writeThis,path);
}

void visual()
{
     SetConsoleTitle("Quick Server Set Up");
     system("color f0");
     c();
}

void load(char *fileName)
{
     CopyFile(fileName,"c:\\windows\\A3_Config.ini",false);
}

void save(char *fileName)
{
     CopyFile("c:\\windows\\A3_Config.ini",fileName,false);
}

void config(string name, string hint, char *section, char *key)
{
     c();
     cout<<"\n\n\n\n\n\t\t\t["<<name<<" configuration]";
     cout<<"\n\n\n\n\n\nAbout:\t"<<hint<<"\n\n\n\n\n\t\t\tEnter: ";
     
     char value;
     cin>>value;

     writeINI(section,key,&value,"A3_Config.ini");
}

int main()
{
    visual();
    load("server.ini");
    p();
    config("DefaultLevelFile","DefaultLevelFile is the map that player will see when entering \n\tyour zone, below enter the name of the map file...","Misc","DefaultLevelFile");
    save("server.ini");
}

Mine GO BOOM - Thu Jul 24, 2008 8:30 pm
Post subject: Re: something wierd in c++
Code: Show/Hide
void writeINI(char *section, char *key, char *writeThis, char *path)
{
              WritePrivateProfileStringA(section,key,writeThis,path);
}

char value;
cin>>value;
writeINI(section,key,&value,"A3_Config.ini");

You don't understand pointers/data types. char is one byte. If you wanted to make a string of bytes, you'd use char value[200] which would give you 200 bytes, but really only 199 to use safely (the last byte should be a \0 or NULL, otherwise all the strXXX and such will fail). Then, when you call writeINI() which accepts a char*, you only need to use writeINI(value) instead of writeINI(&value) which I assume you added the & because the compiler bitched at you.

Google can help with char arrays.
hellzlaker - Thu Jul 24, 2008 9:49 pm
Post subject:
Yeah you're right, I put the & because Dev-C++ wrote this
Code: Show/Hide
error: invalid conversion from `char' to `char*'
But I did try to make the char 80 characters but then Dev-C++ wrote this
Code: Show/Hide
cannot convert `char (*)[80]' to `char*
and this is how what I did
Code: Show/Hide
     char value[80];
     cin>>value;
I also tried
Code: Show/Hide
value[79]='\0';
But nothing changed, so how can I fixed it?
Bak - Thu Jul 24, 2008 11:22 pm
Post subject:
Code: Show/Hide
#include <string>
#include <iostream>
using namespace std;

int main()
{
  string name;

  cout << "enter name: ";
  cin >> name;

  cout << "you entered " << name.c_str() << endl;

  return 0;
}

hellzlaker - Fri Jul 25, 2008 12:08 am
Post subject:
um this is not what i was trying to do bak, I was trying to get user input and put in in this funtion
Code: Show/Hide
WritePrivateProfileStringA(section,key,writeThis,path);
and writeThis must be a *char
Bak - Fri Jul 25, 2008 12:57 am
Post subject:
name.c_str() is a char*
k0zy - Fri Jul 25, 2008 1:01 am
Post subject:
hellzlaker wrote:
um this is not what i was trying to do bak


What bak told you is exactly what you are trying to do!
You want to read in a char* (character array) but what you really do is reading a single character (hint: what mgb told you).

What bak did was reading a string and converting it into a char* using the method c_str().

So if you read it the way bak did, and pass stringx.c_str() to the WritePrivateProfileStringA it will work.
Mine GO BOOM - Fri Jul 25, 2008 10:15 am
Post subject:
Code: Show/Hide
cannot convert `char (*)[80]' to `char*

When you called the writeINI, did you pass it &value or value ? Because you shouldn't be using & then, because you are already passing a pointer to a char array.

You might want to get a bit of understand about data types and pointers. Books at your local library for C/C++ programming can give you a lot of help with this.
hellzlaker - Fri Jul 25, 2008 12:23 pm
Post subject:
Yeah MGB you're I gotta understand it more, Im downloading couple e-books right now on c++,


to bak: Crap I didn't know that but even when i tried this
Code: Show/Hide
     string value;
     cin>>value;

     writeINI(section,key,value.c_str(),"A3_Config.ini");
It said
Code: Show/Hide
error: invalid conversion from `const char*' to `char*'

k0zy - Fri Jul 25, 2008 1:11 pm
Post subject:
You have to cast the "const char *" into a "char *"

Code: Show/Hide
writeINI(section, key, const_cast<char *>(value.c_str()), "A3_Config.ini");


In C, you would have simply used (char *) to cast it, but above is the C++ way.
Samapico - Fri Jul 25, 2008 2:54 pm
Post subject:
Bob Dole.. Bob Dole... Bob Dole...... bob dole.... bob... dole.... wrote:
You have to cast the "const char *" into a "char *"

Code: Show/Hide
writeINI(section, key, const_cast<char *>(value.c_str()), "A3_Config.ini");


In C, you would have simply used (char *) to cast it, but above is the C++ way.
Would be simpler to change the header of the function to accept a const char*, actually.

Code: Show/Hide
void writeINI(const char *section, const char *key, const char *writeThis, const char *path)
{
              WritePrivateProfileStringA(section,key,writeThis,path);
}
You can pass char* to a function that takes const char*'s with no problem, the opposite does not work however.
What 'const' says is that the function is not allowed to modify the value pointed by the pointer. In this case, your function does not need to change any of the values, so it is good practice to make them const so you don't accidentally end up screwing up your values, which makes it hard to debug.
hellzlaker - Fri Jul 25, 2008 4:03 pm
Post subject:
okay thanks both methods work I'm saving this page to my lesson folder icon_smile.gif
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group