Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Poll

What programming topic would you want the next challenge to be about?  (It might be a good opportunity to focus on a subject you're not familiar with or to reinforce knowledge on one that you already know)

Control Flow
- 2 (2.2%)
Arrays, Strings, Pointers, and References
- 8 (9%)
Functions
- 4 (4.5%)
Basic object-oriented programming
- 30 (33.7%)
A bit more advanced OOP (Composition, Operator overloading, Inheritance, Virtual Functions)
- 18 (20.2%)
Templates
- 8 (9%)
Other (Explain)
- 4 (4.5%)
Working with files?  (Streams)
- 15 (16.9%)

Total Members Voted: 89


Pages: 1 ... 39 40 [41] 42 43 ... 78

Author Topic: Programming Challenges & Resources (#bay12prog) Initiative  (Read 96021 times)

Supermikhail

  • Bay Watcher
  • The Dwarf Of Steel
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #600 on: December 17, 2010, 06:56:27 am »

Well. That does not work. Terminal still creates the file in the home directory.
I don't know, it's like... I'm even starting to suspect that my compiler is buggy. Or my terminal... Or my whole system, for that matter.
Also, maybe someone could tell me what's wrong with this code:
Code: [Select]
void Element::readFromFile(ifstream &fin)
{
char temp[10];
fin.ignore(20, '[');
fin.getline(itsName, 21, ':');
fin.getline(temp, ':');
itsNumber = atoi(temp);
fin.getline(itsSymbol, 4, ':');
fin.getline(temp, ':');
itsElectronegativity = atof(temp);
fin.getline(temp, ':');
itsNofValencies = atoi(temp);
for(short i=0; i < itsNofValencies; i++)
{
if(i<(itsNofValencies-1))
{
fin.getline(temp, ',');
itsValencies[i] = atoi(temp);
}
else
{
fin.getline(temp, ']');
itsValencies[i] = atoi(temp);
}
}
}
As far as I can see, besides the weird path thing, it's this function that's bugging my code. The program writes data to file perfectly (I copied the format from Toady, and the output is like this: [Boron:5:B:2.01:1:3]), but doesn't want to read. Or it may be in this segment:
Code: [Select]
{ //If the file exists
fin.open(filename, ios::in);

cout << "\nElements data file good.\n"; //print a message

file_fresh = false;

fin.seekg(0, ios::beg);

for(short i=0; i==99; i++) //and read the data into the Elements_list
{
element_list[i].readFromFile(fin);
if(fin.peek()==fin.eof())
{
elements_available = i+1;
break;
}
}

if( elements_available>=ELEMENTS_ALLOWED ) //If there are 96 elements already
{ //(which is the number of elements naturally occurring on Earth)
cout << "Elements full!\n";  //print a message
}

cout << "\n";

fin.close();
}
Logged

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #601 on: December 17, 2010, 07:38:53 am »

I'm guessing `fin.open(filename, ios::in);` should be  `fin.open("./filename", ios::in);`.
Code: [Select]
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  // for text files
  ofstream file1 ("./file.txt");
  file1 << "Yeah...\n";
  file1.close();
  // for binary stuff
  char a = 'A';
  char b = '\n';
  ofstream file2 ("./file.bin", ios::binary);
  file2.write (&a, 1);
  file2.write (&b, 1);
  file2.close();
  return 0;
}

--------

ron@Tux ~/Media/tmp $ g++ -o test test.cpp
ron@Tux ~/Media/tmp $ ./test
ron@Tux ~/Media/tmp $ cat file.bin file.txt
A
Yeah...
ron@Tux ~/Media/tmp $
Logged

Supermikhail

  • Bay Watcher
  • The Dwarf Of Steel
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #602 on: December 17, 2010, 07:54:52 am »

In my code, filename is a char array with the actual filename as its value. Including "./" I think it should work the same as your suggestion.
Logged

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #603 on: December 17, 2010, 08:13:38 am »

That's odd. Try compiling outside of your IDE, or, if you like, post the whole file and I'll give it a try.
Logged

Supermikhail

  • Bay Watcher
  • The Dwarf Of Steel
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #604 on: December 17, 2010, 09:02:26 am »

Oh. I'd be much obliged.

... I hope you don't mind if I post it all here. I tried uploading to MediaFire, and it doesn't want to take it for some reason. And I condensed it from 2 files.

Yeah, and I tried installing Code::Blocks today. Well, it even doesn't want to get installed, somehow managed to screw up dependencies inside one version. And g++ from the terminal doesn't do anything. It looks more and more like my system could use a fresh install.

Code: [Select]
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;

const short ELEMENTS_ALLOWED = 96;

const char * filename = "./ElementsData.txt";

class Element
{
public:
Element() {}
~Element() {}

Element (short &, char * &, char * &, float &, short &, short (&)[7]);

void Display() const;
void AssignValues (short &, char (&)[20], char (&)[3], float &, short &, short (&)[7]);

short GetNumber() const { return itsNumber; }
char * GetName() const { return itsName; }
char * GetSymbol() const { return itsSymbol; }
float GetElectronegativity() const { return itsElectronegativity; }
short GetNofValencies() const { return itsNofValencies; }
short GetValencyNo(short &n) const { return itsValencies[n]; }
void DisplayValencies() const;

void readFromFile(ifstream &);
void writeToFile(ofstream &);

private:
short itsNumber;
char * itsName;
char * itsSymbol;
float itsElectronegativity;
short itsNofValencies;
short itsValencies[7];
};

Element::Element (short &number, char * &name, char * &symbol,
float &electronegativity, short &nofValencies, short (&valencies)[7])
{
itsNumber = number;
itsName = name;
itsSymbol = symbol;
itsElectronegativity = electronegativity;
itsNofValencies = nofValencies;

for(int i=0; i<nofValencies; i++)
{
itsValencies[i] = valencies[i];
}

}

void Element::Display() const
{
cout << "The element " << itsName << "'s number in the Periodical ";
cout << "Table is " << itsNumber << ".\nIts symbol is ";
cout << itsSymbol << ".\nIts electronegativity is ";
cout << itsElectronegativity << ".\n";
if (itsNofValencies==1)
{
cout << "It has a single valency which is ";
cout << itsValencies[0];
}
else
{
cout << "Its overall number of valencies is ";
cout << itsNofValencies << "and the valencies are ";

for(short i=0; i<itsNofValencies; i++)
{
cout << itsValencies[i];
if(i<(itsNofValencies-1))
cout << ", ";
else
cout << ".";
}
}
}

void Element::AssignValues (short &number, char (&name)[20], char (&symbol)[3],
float &electronegativity, short &nofValencies, short (&valencies)[7])
{
itsNumber = number;
itsName = name;
itsSymbol = symbol;
itsElectronegativity = electronegativity;
itsNofValencies = nofValencies;

for(int i=0; i<nofValencies; i++)
{
itsValencies[i] = valencies[i];
}

}

void Element::DisplayValencies() const
{
if (itsNofValencies==1)
{
cout << "Element " << itsName << " has a single valency which is ";
cout << itsValencies[0];
}
else
{
cout << "Element " << itsName << "'s overall number of valencies is ";
cout << itsNofValencies << "and the valencies are ";

for(short i=0; i<itsNofValencies; i++)
{
cout << itsValencies[i];
if(i<(itsNofValencies-1))
cout << ", ";
else
cout << ".\n\n";
}
}
}

void Element::readFromFile(ifstream &fin)
{
char temp[10];
fin.ignore(20, '[');
fin.getline(itsName, 21, ':');
fin.getline(temp, ':');
itsNumber = atoi(temp);
fin.getline(itsSymbol, 4, ':');
fin.getline(temp, ':');
itsElectronegativity = atof(temp);
fin.getline(temp, ':');
itsNofValencies = atoi(temp);
for(short i=0; i < itsNofValencies; i++)
{
if(i<(itsNofValencies-1))
{
fin.getline(temp, ',');
itsValencies[i] = atoi(temp);
}
else
{
fin.getline(temp, ']');
itsValencies[i] = atoi(temp);
}
}
}

void Element::writeToFile(ofstream &fout)
{
fout.put('[');
fout << itsName;
fout.put(':');
fout << itsNumber;
fout.put(':');
fout << itsSymbol;
fout.put(':');
fout << itsElectronegativity;
fout.put(':');
fout << itsNofValencies;
fout.put(':');
for(short i=0; i < itsNofValencies; i++)
{
fout << itsValencies[i];
if(i<(itsNofValencies-1))
fout.put(',');
}
fout << "]";
}

short menu(bool &file_fresh, short &elements_available)
{
short choice;

cout << "****Menu****\n\n";

if(elements_available>ELEMENTS_ALLOWED)
{
cout << "(-)Add an element.\n";
}
else
{
cout << "(1)Add an element.\n";
}

if(file_fresh)
{
cout << "(-)List the available elements by numbers.\n";
cout << "(-)List the available elements by names.\n";
cout << "(-)Choose an element to display, by number.\n";
cout << "(-)Choose an element to display, by name.\n";
}
else
{
cout << "(2)List the available elements by numbers.\n";
cout << "(3)List the available elements by names.\n";
cout << "(4)Choose an element to display, by number.\n";
cout << "(5)Choose an element to display, by name.\n";
}

cout << "(6)Quit.\n\n";
cout << ": ";
cin >> choice;
return choice;
}



int main(int argc, char** argv)
{
char name[20];
short number;
char symbol[3];
float electronegativity;
short nofValencies;
short valencies[7];
Element element_list[100];
short elements_available;
bool file_fresh;
ifstream fin;
ofstream fout;

fin.open(filename); //Checking if the file exists
fin.close();

if(fin.fail())
{
cout << "\nElements data file does not exist.\n"; //If the file does not exist,
fout.open(filename); //create it.
fout.close();
cout << "New file created.\n\n";

elements_available = 0;

file_fresh = true;
}
else
{ //If the file exists
fin.open(filename);

cout << "\nElements data file good.\n"; //print a message

file_fresh = false;

//fin.seekg(0, ios::beg);

for(short i=0; i==99; i++) //and read the data into the Elements_list
{
element_list[i].readFromFile(fin);
if(fin.peek()==fin.eof())
{
elements_available = i+1;
break;
}
}

if( elements_available>=ELEMENTS_ALLOWED ) //If there are 96 elements already
{ //(which is the number of elements naturally occurring on Earth)
cout << "Elements full!\n";  //print a message
}

cout << "\n";

fin.close();
}

short choice;
while((choice=menu(file_fresh, elements_available))!=6)
{
if ((choice == 1)&&(elements_available<ELEMENTS_ALLOWED))
{
cout << "Enter the desired element's name: ";
cin >> name;
cout << "\nEnter " << name << "'s number in the Periodical Table: ";
cin >> number;
cout << "\nEnter " << name << "'s symbol: ";
cin >> symbol;
cout << "\nEnter " << name << "'s electronegativity: ";
cin >> electronegativity;
cout << "\nEnter " << name << "'s overall number of valencies: ";
cin >> nofValencies;
for (short i = 0; i<nofValencies; i++)
{
cout << "Enter valency number " << i+1 << ": ";
cin >> valencies[i];
}
cout << "\n";

element_list[elements_available].AssignValues(number, name, symbol, electronegativity,
nofValencies, valencies);
elements_available++;

ofstream fout(filename, ios::app);
if( !fout )
{
cout << "Unable to open elements data for writing" << endl;
return 1;
}
fout.put('\n');
element_list[elements_available-1].writeToFile(fout);
cout << "Element saved.\n\n";

file_fresh = false;

fout.close();
}
else
if ((choice == 2)&&(!file_fresh))
{
cout << "Available elements' numbers are ";
for(short i=0; i<elements_available; i++)
{
cout << element_list[i].GetNumber();
if(i<(elements_available-1))
cout << ", ";
}
cout << ".\n\n";
}
else
if ((choice == 3)&&(!file_fresh))
{
cout << "Available elements are ";
for(short i=0; i<elements_available; i++)
{
cout << element_list[i].GetName();
if(i<(elements_available-1))
cout << ", ";
}
cout << ".\n\n";
}
else
if ((choice == 4)&&(!file_fresh))
{
cout << "Enter the desired element's number: ";
cin >> number;
cout << "\n";

for(short i=0; i<elements_available; i++)
{
if(element_list[i].GetNumber()==(number+1))
{
cout << "The data of the element No. ";
cout << number << ":\n\n";
element_list[i].Display();
cout << "\n";
break;
}
else
if(i==(elements_available-1))
cout << "The requested element not found.\n";
}
cout << "\n";
}
else
if ((choice == 5)&&(!file_fresh))
{
cout << "Enter the desired element's name: ";
cin >> name;
cout << "\n";

for(short i=0; i<elements_available; i++)
{
if(element_list[i].GetName()==name)
{
cout << "The data of the element ";
cout << name << ":\n\n";
element_list[i].Display();
cout << "\n\n";
break;
}
else
if(i==(elements_available-1))
cout << "The requested element not found.\n";
}
cout << "\n";
}
else
{
cout << "Option unavailable. Try again.\n\n";
}
}

return 0;
}
Logged

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #605 on: December 17, 2010, 09:21:55 am »

Works fine over here. Creates a file in the working directory and all.
Code: [Select]
ron@Tux ~/Media/tmp $ g++ -o mikhail mikhail.cpp -lm
ron@Tux ~/Media/tmp $ ls -a
.  ..  mikhail  mikhail.cpp
ron@Tux ~/Media/tmp $ ./mikhail

Elements data file does not exist.
New file created.

****Menu****

(1)Add an element.
(-)List the available elements by numbers.
(-)List the available elements by names.
(-)Choose an element to display, by number.
(-)Choose an element to display, by name.
(6)Quit.

: 6
ron@Tux ~/Media/tmp $ ls -a
.  ..  ElementsData.txt  mikhail  mikhail.cpp
ron@Tux ~/Media/tmp $
Sounds like your computer is pretty fucked up. Did you ever compile/download gcc, or did it come with your IDE?
Logged

Supermikhail

  • Bay Watcher
  • The Dwarf Of Steel
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #606 on: December 17, 2010, 09:39:34 am »

Hm. I might be wrong, but I believe GCC came with my Ubuntu distribution.

I tried to do it like this:
Code: [Select]
mikhail@misha:~$ g++ /home/mikhail/MyseriousConstructions/ChemicalLego/element_setup.cpp -o element_setup

What's -lm there?

Also, in case it works miraculously, could you put this data to the created file?
Code: [Select]

[Hydrogen:1:H:2.2:1:1]
[Boron:5:B:2.01:1:3]
[Nitrogen:7:N:3.04:5:1,2,3,4,5]

And then try the options that were crossed out previously?
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #607 on: December 17, 2010, 10:11:57 am »

"Chemical Lego"... Awesome :)
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

Supermikhail

  • Bay Watcher
  • The Dwarf Of Steel
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #608 on: December 17, 2010, 10:40:39 am »

Er. The title is outdated, actually. Well, the name of the folder. And, I guess, without knowing for sure what and if I'm going to be able to make something of it, there's no point for a real title.

Edit: That's retarded. GCC from the terminal actually makes the executable in the home directory. Why?
« Last Edit: December 17, 2010, 10:43:49 am by Supermikhail »
Logged

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #609 on: December 17, 2010, 11:27:08 am »

Hm. I might be wrong, but I believe GCC came with my Ubuntu distribution.
That's fine then. Ubuntu's gcc works great.

I might have found the problem, you seem to be running everything from you're home directory. Open a terminal and type 'pwd' (Stands for print working directory), it should print something like /home/mikhail/. That's the directory you're in currently. Now type 'cd MyseriousConstructions/ChemicalLego'. You'll notice the prompt has changed, so has the output of 'pwd'. This means you are in a different directory. Most applications generate files in the directory the user is in (The output of pwd). Try compiling again from the project's directory, and then running it from it in there. It should work just fine.

What's -lm there?
-lm tells the compiler to include the math library.
Logged

Supermikhail

  • Bay Watcher
  • The Dwarf Of Steel
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #610 on: December 17, 2010, 11:47:35 am »

Hm. I might be wrong, but I believe GCC came with my Ubuntu distribution.
That's fine then. Ubuntu's gcc works great.

I might have found the problem, you seem to be running everything from you're home directory. Open a terminal and type 'pwd' (Stands for print working directory), it should print something like /home/mikhail/. That's the directory you're in currently. Now type 'cd MyseriousConstructions/ChemicalLego'. You'll notice the prompt has changed, so has the output of 'pwd'. This means you are in a different directory. Most applications generate files in the directory the user is in (The output of pwd). Try compiling again from the project's directory, and then running it from it in there. It should work just fine.
Hm. That's great. So, to give access to the database to a user on a different computer, I'll need a way to put the ElementsData into their home directory...
What's -lm there?
-lm tells the compiler to include the math library.
Oh... Why? Does it need it? The math library required by "math.h"?

Edit: Google gives a lot of answers for Windows, using Windows libraries and functions. >:(
« Last Edit: December 17, 2010, 02:01:12 pm by Supermikhail »
Logged

lordnincompoop

  • Bay Watcher
  • Allusionist
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #611 on: December 17, 2010, 03:30:39 pm »

Well, I couldn't figure out how you guys did those return statements, because apparently the function type and the return type had to be the same and I couldn't figure out how to pass the entire array at once. What I did instead was pass the parameter by address. The tokenised input is in the form of an array inside the room function, and input is given by calling the IO function with the input array as a parameter using the address-of operator.

What it's telling me though is that the parameter/argument types are incompatible. Something about a char** and a char***. Any idea how to make this work?

Code: [Select]
#include <string.h>
#include <iostream>

using namespace std;

int IO(char** cReturnValue[]);
int testRoom();

int main()
{
    return 0;
}

int testRoom()
{
    cout << "This is the test room! You find yourself surroinded by gray walls." << endl;
    cout << "There is a single, naked LIGHT bulb on the ceiling, and a CRATE on the floor next to you." << endl;
    cout << endl;
   
    char *cInputTokens[10];
    do
    {
IO(&*cInputTokens);
if (cInputTokens[0] == "get")
           {
               if (cInputTokens[1] == "crate")
                   cout << "The crate is too heavy to lift." << endl;
               else if (cInputTokens[1] == "light")
                   cout << "Ouch! The light bulb is too hot to touch." << endl;
           };
    }while(0 == 0); //Make an exit condition.
}
       
int IO(char** cReturnValue[])
{
    char* cIOline;
    cin.getline(cIOline, 80);
    int nFinger = 0;
    *cReturnValue[nFinger] = strtok(cIOline, "   ,.;:");
    do
    {
        *cReturnValue[++nFinger] = strtok(NULL, "  ,.;:");
    }while(nFinger == 8);
return 0;
}
Logged

Shades

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #612 on: December 17, 2010, 04:07:14 pm »

IO(&*cInputTokens);

should be

IO(&cInputTokens);

Otherwise your dereferencing the array first then getting a pointer back to it.
Logged
Its like playing god with sentient legos. - They Got Leader
[Dwarf Fortress] plays like a dizzyingly complex hybrid of Dungeon Keeper and The Sims, if all your little people were manic-depressive alcoholics. - tv tropes
You don't use science to show that you're right, you use science to become right. - xkcd

lordnincompoop

  • Bay Watcher
  • Allusionist
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #613 on: December 17, 2010, 04:11:51 pm »

Removed the thing, got his error:

"cannot convert parameter 1 from 'char *(*)[10]' to 'char **[]"
Logged

Shades

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #614 on: December 17, 2010, 04:27:09 pm »

Been a while since I passed data like that :) Try

int IO(char** cReturnValue[10]) for your function declaration.
Logged
Its like playing god with sentient legos. - They Got Leader
[Dwarf Fortress] plays like a dizzyingly complex hybrid of Dungeon Keeper and The Sims, if all your little people were manic-depressive alcoholics. - tv tropes
You don't use science to show that you're right, you use science to become right. - xkcd
Pages: 1 ... 39 40 [41] 42 43 ... 78