Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
funster code help

 
Post new topic   Reply to topic Printable version
 View previous topic  just a wierd question about continuum ... Post :: Post Ani's Summer Programming Projects  View next topic  
Author Message
hellzlaker
Registered Cap Buster
Popping men in the ass since Oct 2005


Gender: NEVER ENOUGH!
Joined: Oct 27 2005
Posts: 34
Offline

PostPosted: Fri Apr 04, 2008 10:09 pm   Post maybe stupid    Post subject: funster code help Reply to topic Reply with quote

well I am trying to genorate a random file name and ofstream that file.. this is what i got so far
Code: Show/Hide

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <windows.h>
#include <cstdlib>   
#include <time.h>
#include <sstream>    // needed to convert int to string
using namespace std;

const int LOW = 1;
const int HIGH = 1000;

void randomfilename(string a, string b, string c, string d, string e)
{
     
    time_t seconds;
    time(&seconds);
    srand((unsigned int) seconds);
   
    int random = rand() % (HIGH - LOW + 1) + LOW;
   
    string s;
    std::stringstream out;
    out << random;
    s = out.str();
   
    ofstream goods;
    goods.open(a+s+".txt");
    goods<<random;
    goods.close();
}

int main()
{
    randomfilename("hi","hi","hi","hi","hi");
    return 0;
}
and it compiles with those errors

Code: Show/Hide
Compiler: Default compiler
Building Makefile: "C:\Documents and Settings\Owner\Desktop\Programing\c++\VIRUSES\360 mod\Makefile.win"
Executing  make...
make.exe -f "C:\Documents and Settings\Owner\Desktop\Programing\c++\VIRUSES\360 mod\Makefile.win" all
g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"  -I"C:/Dev-Cpp/include/c++/3.4.2/backward"  -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32"  -I"C:/Dev-Cpp/include/c++/3.4.2"  -I"C:/Dev-Cpp/include"   

main.cpp: In function `void randomfilename(std::string, std::string, std::string, std::string, std::string)':
main.cpp:36: error: no matching function for call to `std::basic_ofstream<char, std::char_traits<char> >::open(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
C:/Dev-Cpp/include/c++/3.4.2/fstream:695: note: candidates are: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]

make.exe: *** [main.o] Error 1

Execution terminated
Back to top
View users profile Send private message Add User to Ignore List Send email AIM Address MSN Messenger
Bak
?ls -s
0 in


Age:26
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Fri Apr 04, 2008 10:15 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

Code: Show/Hide

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

int main()
{
  int namelength = 10;
  char buf[2] = {0, 0};
  string name;

  srand((int)time(0));

  for (int x = 0; x < namelength; ++x)
  {
    buf[0] = 'a' + rand() % 26;
    name += buf;
  }

  name += ".txt";

  ofstream fout(name.c_str());

  if (fout.good())
    fout.close();

  return 0;
}


_________________
SubSpace Discretion: A Third Generation SubSpace Client
Back to top
View users profile Send private message Add User to Ignore List AIM Address
Dr Brain
Flip-flopping like a wind surfer


Age:39
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 3502
Location: Hyperspace
Offline

PostPosted: Fri Apr 04, 2008 11:23 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

Lame. Use sprintf.
_________________
Hyperspace Owner

Smong> so long as 99% deaths feel lame it will always be hyperspace to me
Back to top
View users profile Send private message Add User to Ignore List AIM Address Yahoo Messenger MSN Messenger
Bak
?ls -s
0 in


Age:26
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Sat Apr 05, 2008 12:04 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

I thought about it. If I was gonna use sprintf I would have used snprintf, but depending on which compiler he's using it might be snprintf or _snprintf, which was not worth the trouble of explaining.
Back to top
View users profile Send private message Add User to Ignore List AIM Address
Dr Brain
Flip-flopping like a wind surfer


Age:39
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 3502
Location: Hyperspace
Offline

PostPosted: Sat Apr 05, 2008 10:29 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

For a situation like this, sprintf is ok, since you're not using user input (or strings at all) as part of the print.
Back to top
View users profile Send private message Add User to Ignore List AIM Address Yahoo Messenger MSN Messenger
hellzlaker
Registered Cap Buster
Popping men in the ass since Oct 2005


Gender: NEVER ENOUGH!
Joined: Oct 27 2005
Posts: 34
Offline

PostPosted: Sat Apr 05, 2008 6:24 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

wait can you explain this 'a' + rand() % 26; ? (i know creates random name but how?)
Back to top
View users profile Send private message Add User to Ignore List Send email AIM Address MSN Messenger
k0zy
Server Help Squatter


Gender:Gender:Male
Joined: Jan 11 2003
Posts: 571
Location: Germany
Offline

PostPosted: Sat Apr 05, 2008 8:09 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

Characters aren't really stored as characters, but as their equivalent ascii code.
So what it does is, it takes the ascii code of the letter a and adds a number between 0 and 26 to it.

This results is a new ascii code, which can be interpreted as a character.
_________________
It's a shark! Oh my god! Unbelievable!
Back to top
View users profile Send private message Add User to Ignore List
Samapico
No, these DO NOT look like penises, ok?


Joined: May 08 2003
Posts: 1252
Offline

PostPosted: Sat Apr 05, 2008 8:56 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

Bob Dole.. Bob Dole... Bob Dole...... bob dole.... bob... dole.... wrote:
Characters aren't really stored as characters, but as their equivalent ascii code.
So what it does is, it takes the ascii code of the letter a and adds a number between 0 and 26 to it.

This results is a new ascii code, which can be interpreted as a character.
Between 0 and 25 actually... but yeah, pretty much right tongue.gif
Bob Dole lol...
_________________
(Insert a bunch of dead links here)
Back to top
View users profile Send private message Add User to Ignore List
Bak
?ls -s
0 in


Age:26
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Sun Apr 06, 2008 2:33 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

http://www.asciitable.com/

if you look there you see that 'a' is equal to 97 (decimel) so you can do

Code: Show/Hide
if ('a' == 97)
   cout << "it's true!" << endl;
else
   cout << "lies!" << endl;


and it'll show you that it's true. So % is the modulo operator which is basically remainder after you divide. So if I divide rand() (which is a number between zero and 32767) by 26 the remainder will be between 0 and 25. Thus I will get number between 97 and 122, which are the letters 'a' to 'z'.
Back to top
View users profile Send private message Add User to Ignore List AIM Address
hellzlaker
Registered Cap Buster
Popping men in the ass since Oct 2005


Gender: NEVER ENOUGH!
Joined: Oct 27 2005
Posts: 34
Offline

PostPosted: Sun Apr 06, 2008 1:05 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

oh this isnt complicated at all, cuz first time i saw that didnt get anything :\
Back to top
View users profile Send private message Add User to Ignore List Send email AIM Address MSN Messenger
tcsoccerman
Server Help Squatter


Age:33
Gender:Gender:Male
Joined: Jan 15 2007
Posts: 694
Location: Atlantis
Offline

PostPosted: Sun Apr 06, 2008 5:40 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

which is a reason to use number values (int) instead of char values if possible to save memory icon_smile.gif
Back to top
View users profile Send private message Add User to Ignore List Send email AIM Address
Bak
?ls -s
0 in


Age:26
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Sun Apr 06, 2008 6:02 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

ac_withstupid.gif

where's the talking out of ass smiley?

sizeof(char) < sizeof(int)
Back to top
View users profile Send private message Add User to Ignore List AIM Address
tcsoccerman
Server Help Squatter


Age:33
Gender:Gender:Male
Joined: Jan 15 2007
Posts: 694
Location: Atlantis
Offline

PostPosted: Sun Apr 06, 2008 6:13 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

really? i read that somewhere?!?!
Back to top
View users profile Send private message Add User to Ignore List Send email AIM Address
CypherJF
I gargle nitroglycerin


Gender:Gender:Male
Joined: Aug 14 2003
Posts: 2582
Location: USA
Offline

PostPosted: Sun Apr 06, 2008 6:40 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

don't believe everything you read then?
_________________
Performance is often the art of cheating carefully. - James Gosling
Back to top
View users profile Send private message Add User to Ignore List
tcsoccerman
Server Help Squatter


Age:33
Gender:Gender:Male
Joined: Jan 15 2007
Posts: 694
Location: Atlantis
Offline

PostPosted: Sun Apr 06, 2008 6:45 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

touche. i dunno it said that char's get turned into int values or something.
Back to top
View users profile Send private message Add User to Ignore List Send email AIM Address
Animate Dreams
Gotta buy them all!
(Consumer whore)


Age:37
Gender:Gender:Male
Joined: May 01 2004
Posts: 821
Location: Middle Tennessee
Offline

PostPosted: Mon Apr 14, 2008 10:12 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

I think the idea is that an int can store the number 4 000 000 000 in 4 bytes, whereas 4 000 000 000 in a char array takes up 10 chars, and therefore 10 bytes.
Back to top
View users profile Send private message Add User to Ignore List Send email Visit posters website AIM Address MSN Messenger
Samapico
No, these DO NOT look like penises, ok?


Joined: May 08 2003
Posts: 1252
Offline

PostPosted: Mon Apr 14, 2008 11:34 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

... you never store numbers as characters anyway...


What tcsoccerman is talking about is probably that any char value is actually a number between 0 and 255, and you can add 'a' to 800, for example, and it would give you 897 or something
Back to top
View users profile Send private message Add User to Ignore List
tcsoccerman
Server Help Squatter


Age:33
Gender:Gender:Male
Joined: Jan 15 2007
Posts: 694
Location: Atlantis
Offline

PostPosted: Tue Apr 15, 2008 9:36 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

yeh pretty much.
Back to top
View users profile Send private message Add User to Ignore List Send email AIM Address
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> Trash Talk All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 188 page(s) served in previous 5 minutes.

phpBB Created this page in 0.744589 seconds : 42 queries executed (76.5%): GZIP compression disabled