Jump to content


Photo

Getting back in to programming


  • Please log in to reply
4 replies to this topic

#1 Swordfish

Swordfish

    Advanced Member

  • Salty Members
  • 2902 posts

Posted 03 September 2011 - 12:42 PM

Right, so it's been a long while since I did any programming so I decided to try somthing simple and make a card dealing program and i'm getting an error I can't make heads nor tails of so could some one tell me what I'm doing wrong?

// Card dealing program.cpp : Defines the entry point for the console application.
//

#include <stdafx.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <math.h>
#include <string>


using namespace std;
int Rand_0toN(int n);
int Deck[3][13];
int Drawcard();
bool Checkdeck();
void ResetDeck();
int drawcardloop = 1;
int Cardsleft;

bool debug = true;
string suits[4] = {"Hearts","Diamonds","Spades","Clubs"};
string ranks[13] = {"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};

int main()
{
    Cardsleft = 52;
    int n = 0, i, d = 0;
    srand(time(NULL));
    ResetDeck();
    while (1)
    {
        cout << "How many cards would you like to draw? type 0 to exit" << endl;
        cin >>  n;
        if (n == 0)
        {
            break;
        }
        for (i = 1; i <=n; i++)
        {
                if (d == 1)
                {
                    break;
                }
                d = Drawcard();
                
            
        }
        if (d == 1)
        {
            break;
        }
        cout << "There are " << Cardsleft << "Card(s) left" << endl;
    }

    return 0;
}

int Drawcard()
{
    bool newcard = false;
    int r = 0;
    int s = 0;
    int input;

    int card;


    if (Cardsleft <= 0)
    {
        while(1)
        {
            cout << "There are no cards left, type 1 to shuffle the deck, type 0 to exit program" << endl;
            cin >>  input;
            if(input == 1)
            {
                ResetDeck();
                Cardsleft = 51;
                if (debug == true)
                {
                    drawcardloop = 1;
                }
                break;
            }
            if(input == 0)
            {
                return 1;
                break;

            }
            else
            {
                cout << "you did not enter a Valid number, please try again" << endl;
            }
        }
    }
    else
    {
        while (newcard == false)
        {
            card = Rand_0toN(52);
            s = card / 13;
            r = card % 13;
            
            if (Deck [s][r] == 1){
                newcard = false;

            }
            else
            {
                Cardsleft = Cardsleft - 1;
                Deck [s][r] = 1;
                break;
            }
        }
    }

    cout << ranks[r] <<" of " << suits [s] << endl;
    if (debug == true)
    {
        cout << drawcardloop << endl;
        drawcardloop = drawcardloop +1;
    }
    return 0;

}


int Rand_0toN(int n)
{
    return rand() % n;
}

void ResetDeck()
{
    int s,r;
    for (s = 1; s <=3; s++)
    {
        for (r = 1; r <=13; r++)
        {
            Deck[s][r] = 0;

        }

    }

}

the errors i get are


[pre]Compiling...
Card dealing program.cpp
c:\users\martin\documents\visual studio 2008\projects\card dealing program\card dealing program\card dealing program.cpp(20) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
Linking...
MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
C:\Users\Martin\Documents\Visual Studio 2008\Projects\Card dealing program\Debug\Card dealing program.exe : fatal error LNK1120: 1 unresolved externals
[/pre]


EDIT : i bet it somthing really simple.

#2 Vellfire

Vellfire

    TV people want to leave

  • Salty Members
  • 14593 posts
  • LocationKentucky

Posted 03 September 2011 - 01:11 PM

Not sure if this is causing the error but do #include <iostream> instead of with quotes.

I love this hobby - stealing your mother's diary
BRRING! BRRING!
Hello!  It's me, Vellfire!  FOLLOW ME ON TWITTER! ... Bye!  CLICK!  @gidgetnomates


#3 Swordfish

Swordfish

    Advanced Member

  • Salty Members
  • 2902 posts

Posted 03 September 2011 - 02:01 PM

For the record, when it gave me a pre-written header, the headers that where there were written with quotes, so I wrote the others with quotes but I'll change it any way and see what happens.

EDIT: no dice, same errors.
EDIT: maybe i should go back to C#. -_-

EDIT: I think I know what it is, I'm missing a header file or somthing, basicly, but I'm not sure what pre-requisit would be needed for the Time header file.

#4 Vellfire

Vellfire

    TV people want to leave

  • Salty Members
  • 14593 posts
  • LocationKentucky

Posted 04 September 2011 - 01:10 AM

For the record, when it gave me a pre-written header, the headers that where there were written with quotes, so I wrote the others with quotes but I'll change it any way and see what happens.


http://stackoverflow...ncluding-header

This discusses the difference between includes with quotes and includes with brackets.  Haven't taken the time to look through all your code (couldn't scroll through it when I posted earlier because it wouldn't work on my phone and cba right now because I'm about to go to bed) but I googled your error code and it looks like problems with VS's settings, see if anything in this link helps you:

http://social.msdn.m...2e-8cdf596926a6

I love this hobby - stealing your mother's diary
BRRING! BRRING!
Hello!  It's me, Vellfire!  FOLLOW ME ON TWITTER! ... Bye!  CLICK!  @gidgetnomates


#5 Swordfish

Swordfish

    Advanced Member

  • Salty Members
  • 2902 posts

Posted 05 September 2011 - 02:10 AM

I had a look at both topics, thanks for the info on the header thing, I had no idea there was a differance but I get the use now, For when you want to use a Header or a modifed header of one that exists, you place it in your project source folder and use "" so it doesn't use the system versions. I tried the linker thing but it was already set to console, I changed it to windows like one of them sugested but it didn't work so i set it back to console. On the bright side, it fixed the warnign that as comming up but the errors remain the same. I'm gonna post a code update since i worked out how to make it so the program knows how many card and what cards are left.

EDIT: posted code modification, now the program knows how many cards are in the deck and what cards haven't been drawn, also that warning is back but it's not important. However those errors are still continuing and the "fixes" mentioned in that thread don't work. also using string array instead of char *.

EDIT: Fixed, turns out i was using the wrong main.. which is strange as i did use Windows console application as my choice... still it works now, though i do get a major access vioaltion when i try to exit from the "no more cards left" loop... guess I'll have to try and fix it.

EDIT: seems that whole loop is screwed up.

edit: the program sort of works now... for some reason the program won't print hearts and diamonds, only spades and clubs.. and if you try to draw all cards you get smiley faces on some of them...

EDIT: I'll be honest, I'm getting a little annoyed tbh, I just wanted to get back into programming, I didn't expect a simple program to have so many problems...

EDIT: added debug switch alongwith a number variable to help work out whats wrong with it. also the program doesn't seam to like exiting fromwithin the "no more cards in deck" loop, so i think i might take it out and just either shuffle it or exit to main  and only allow exit from main instead of how i have it.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users