Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1] 2

Author Topic: Requesting C++ assistance!  (Read 2435 times)

Zaerosz

  • Bay Watcher
  • ☼sperm whale leather thong☼
    • View Profile
Requesting C++ assistance!
« on: August 15, 2011, 05:36:56 pm »

So thanks to another late-night inspiration from a guy on my forum, I've started learning C++ in order to build a booster pack generator for Yu-Gi-Oh cards. This is what I have so far:
Code: [Select]
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

int main ()
{
    int x;
    /*random seed*/
    srand ( time(NULL) );
    /*assign random number to x*/
    x = rand() % 127;
    do {
    printf ("You are opening a pack from: Legend of Blue-Eyes White Dragon");
    if (x<10) puts ("LOB-00", x);
    else if (9<x<100) puts ("LOB-0", x);
    else if (99<x) puts ("LOB-", x);
    } while (x=127);
    puts ("Only one card? What a ripoff.");
    return 0;
}
and these are the errors I'm getting:
Code: [Select]
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\stdio.h||In function 'int main()':|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\stdio.h|357|error: too many arguments to function 'int puts(const char*)'|
C:\Users\Ranson\Documents\C++ work\pack generator\main.cpp|16|error: at this point in file|
C:\Users\Ranson\Documents\C++ work\pack generator\main.cpp|17|warning: comparisons like 'X<=Y<=Z' do not have their mathematical meaning|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\stdio.h|357|error: too many arguments to function 'int puts(const char*)'|
C:\Users\Ranson\Documents\C++ work\pack generator\main.cpp|17|error: at this point in file|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\stdio.h|357|error: too many arguments to function 'int puts(const char*)'|
C:\Users\Ranson\Documents\C++ work\pack generator\main.cpp|18|error: at this point in file|
C:\Users\Ranson\Documents\C++ work\pack generator\main.cpp|19|warning: suggest parentheses around assignment used as truth value|
||=== Build finished: 6 errors, 2 warnings ===|
I have no idea what's going on, but the affected line n stdio.h appears to be
Code: [Select]
_CRTIMP int __cdecl __MINGW_NOTHROW puts (const char*);
Help please?
Logged
くコ:彡

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: Requesting C++ assistance!
« Reply #1 on: August 15, 2011, 05:56:59 pm »

My C is beyond rusty, but judging from the error messages only, it looks like it doesn't like having more than one parameter in your calls to puts.

Maybe try changing them to something like:

Code: [Select]
printf("LOB-00%i\n", x);
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

Zaerosz

  • Bay Watcher
  • ☼sperm whale leather thong☼
    • View Profile
Re: Requesting C++ assistance!
« Reply #2 on: August 15, 2011, 06:00:52 pm »

What would that do, exactly? It looks like it would just add the number on to the end of LOB-00, which would give us things like LOB-00108, which is wrong.
Logged
くコ:彡

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: Requesting C++ assistance!
« Reply #3 on: August 15, 2011, 06:02:01 pm »

Oh, I thought that was what you were trying to do. 

What are you hoping to achieve with this line?

puts ("LOB-00", x);

Edit:

Er, maybe I was misunderstood?  Here is the complete code:

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

int main ()
{
    int x;
    /*random seed*/
    srand ( time(NULL) );
    /*assign random number to x*/
    x = rand() % 127;
    do {
    printf ("You are opening a pack from: Legend of Blue-Eyes White Dragon");
    if (x<10) printf("LOB-00%i\n", x);
    else if (9<x<100) printf("LOB-0%i\n", x);
    else if (99<x) printf("LOB-%i\n", x);
    } while (x=127);
    puts ("Only one card? What a ripoff.");
    return 0;
}

Or, maybe even something like this:

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

int main ()
{
    int x;
    /*random seed*/
    srand ( time(NULL) );
    /*assign random number to x*/
    x = rand() % 127;
    do {
    printf ("You are opening a pack from: Legend of Blue-Eyes White Dragon");
    printf("LOB-%03d\n", x); 
    } while (x=127);
    puts ("Only one card? What a ripoff.");
    return 0;
}

« Last Edit: August 15, 2011, 06:09:22 pm by Levi »
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

Zaerosz

  • Bay Watcher
  • ☼sperm whale leather thong☼
    • View Profile
Re: Requesting C++ assistance!
« Reply #4 on: August 15, 2011, 06:09:44 pm »

if (x<10) puts ("LOB-00", x)
That's supposed to mean that if x is a single-digit number, i'll print LOB-00(x). Similarly, the other lines mean that if it has two digits (9<x<100) or three digits (99<x) it'll print LOB-0(x) or LOB-(x) respectively.

e.g. x=9, print LOB-009
x=27, print LOB-027
x=101, print LOB-101

EDIT: Your first correction doesn't change it at all, and your second one makes it work, but then it doesn't stop printing the first two lines and never prints the last text line. I suspect it involves the while function, but I don't know what to do with it.
« Last Edit: August 15, 2011, 06:14:35 pm by Zaerosz »
Logged
くコ:彡

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: Requesting C++ assistance!
« Reply #5 on: August 15, 2011, 06:12:42 pm »

Ah okay, use the second one I posted then.  It autopads to three digits.
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: Requesting C++ assistance!
« Reply #6 on: August 15, 2011, 06:16:24 pm »

EDIT: Your first correction doesn't change it at all, and your second one makes it work, but then it doesn't stop printing the first two lines and never prints the last text line. I suspect it involves the while function, but I don't know what to do with it.

Yeah, the while loop you have will go on forever as far as I can see.  You are assigning 127 to x instead of comparing it to something.  (= vs ==).  I'm not sure how many times you want it to print though.
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

Zaerosz

  • Bay Watcher
  • ☼sperm whale leather thong☼
    • View Profile
Re: Requesting C++ assistance!
« Reply #7 on: August 15, 2011, 06:17:49 pm »

I want it to print once, then move on to line 18, then finish.

EDIT: Fixed it. It was the = in the while loop, but I made it ==.
Logged
くコ:彡

Zaerosz

  • Bay Watcher
  • ☼sperm whale leather thong☼
    • View Profile
Re: Requesting C++ assistance!
« Reply #8 on: August 15, 2011, 06:26:50 pm »

Next question: How would I create a list of the cards in the pack and have the RNG reference this list when it picks the number?

EDIT: And also, is there any other method of random seeding besides linking it to the system clock?
« Last Edit: August 15, 2011, 06:56:02 pm by Zaerosz »
Logged
くコ:彡

Zaerosz

  • Bay Watcher
  • ☼sperm whale leather thong☼
    • View Profile
Re: Requesting C++ assistance!
« Reply #9 on: August 15, 2011, 07:31:40 pm »

Hmm. There's a new problem with my code - now that I've gotten it to do the above, I can't get it to do anything else. I've made changes to the code and the flavour text, but it's still printing the original stuff.
Logged
くコ:彡

Zaerosz

  • Bay Watcher
  • ☼sperm whale leather thong☼
    • View Profile
Re: Requesting C++ assistance!
« Reply #10 on: August 15, 2011, 07:47:17 pm »

Okay, so I have no idea what's going on. I've copy-pasted the code to a new file, closed it, reopened the new file, restarted the program three times, and it's still using code that no longer exists.

help meeeeeeee

EDIT: oh hey i fixed it.
Logged
くコ:彡

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: Requesting C++ assistance!
« Reply #11 on: August 15, 2011, 09:00:43 pm »

There are many ways to give it a list. C++ probably has a convenient data structure already, or you could use arrays.

In general, you would either want to load the data from a file, or define it in the code itself. For the in-code version, it could be something as simple as

char *cardNames[] = {
/*000*/ "Some card",
/*001*/ "Some other card",
...
/*125*/ "The second last card",
/*126*/ "The 127th card"
};

(the /*ddd*/ is a comment, and thus entirely optional, but would help make it readable)


Generally, though, you may want to store more than just a name, so it would be best to create a struct or a class to store the data, and loading from a file would likely be the most flexible and readable option. All of this, however, is somewhat complicated advanced stuff (well, if not advanced, certainly not beginner, either. At the very least, file parsing can be quite tricky in a lower level language like C or C++), at least to do somewhat right. It certainly would be possible to put each card as a seperate option in a massive long chain of else if()s, but that would be the ugliest way, not to mention the excessive typing required, and poor readability later.
Logged
Eh?
Eh!

Zaerosz

  • Bay Watcher
  • ☼sperm whale leather thong☼
    • View Profile
Re: Requesting C++ assistance!
« Reply #12 on: August 15, 2011, 09:41:07 pm »

Well, I have a list of all the cards in numerical order in a .txt file. Is there any way I could get it to read that?
Logged
くコ:彡

Normandy

  • Bay Watcher
    • View Profile
Re: Requesting C++ assistance!
« Reply #13 on: August 15, 2011, 09:47:44 pm »

If you have #include <iostream>, there's really no need to use C functions like printf or puts. Learn how to use streams (such as std::stringstream, which does the job of puts, and std::cout, which does the job of printf). Streams are also the basis of file-reading in C++. There's a tutorial on reading files here: http://www.cplusplus.com/doc/tutorial/files/

Also, don't use char* if you don't know better. Use std::string until you're more familiar with C, it'll save you a lot of unnecessary hassle.
Logged

Zaerosz

  • Bay Watcher
  • ☼sperm whale leather thong☼
    • View Profile
Re: Requesting C++ assistance!
« Reply #14 on: August 16, 2011, 01:03:31 am »

Yeah, so I decided to get rid of my patchwork monstrosity and work with my friend on the same code together. His code uses cout for display, and it's generally more organized than mine. However, we're still working on linking RNG results to text.
Generally, though, you may want to store more than just a name, so it would be best to create a struct or a class to store the data, and loading from a file would likely be the most flexible and readable option.
How would we go about doing this?
Logged
くコ:彡
Pages: [1] 2