Click here to Skip to main content
16,017,650 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is part of mine program but why the random number cant follow the file processing number?

this is mine card.txt

1   Abyss Devolos        F0647    Balance      NA      SpeedStorm
2   Ace Dragon           E7609    Attack       NA      HyperSphere
3   Anubion A2           E1057    Defense      NA      Dual-Layer
4   Balar B4             E4726    Attack       NA      SlingShock
5   Crystal Dranzer      F0217    Balance      NA      Burst
6   Cyclone Belfyre      F3965    Stamina    Attack    QuadDrive
7   Dark-X Nepstrius     E4749    Defense      NA      SlingShock
8   Diomedes D2          E1062    Attack       NA      Dual-Layer
9   Doomscizor           E1033    Attack       NA      SwitchStrike
10  Vatryek Wing Accel   B9492    Attack       NA      Burst 


What I have tried:

#include <iostream>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;


//Global variable
const string cardfile=("cards.txt");


//Function Prototypes
void readData(string,int);
int RandomCardP1 (int);
int RandomCardP2 (int);
int CheckSystem (int , int , int , int );
void ShowCardP1(int , string [], string [], string [], string[] , string []);
void ShowCardP2(int, string [], string [], string [], string [], string []);
int FinalScore();

//main program
int main(){

    srand(time(0));
    string nameP1,nameP2;
    char respond='Y';
    int scP1=0,scP2=0;

    //parallel arrays for card
    const int SIZE=10;
    int ID [SIZE]={1,2,3,4,5,6,7,8,9,10}; //Card ID
    string beybladeName[SIZE]={"Abyss Devolos","Ace Dragon","Anubion A2","Balar B4","Crystal Dranzer","Cyclone Belfyre","Dark-X Nepstrius","Diomedes D2","Doomscizor","Vatryek Wing Accel"};//Card name
    string pCode[SIZE]={"F0647", "E7609", "E1057", "E4726", "F0217", "F3965", "E4749", "E1062", "E1033", "B9492 "};//Product code
    string type[SIZE]={"Balance", "Attack", "Defense", "Attack", "Balance", "Stamina", "Defense", "Attack", "Attack", "Attack"};//Type
    string plusMode [SIZE]={"NA", "NA", "NA", "NA", "NA", "Attack", "NA", "NA", "NA", "NA"};
    string system [SIZE]={"SpeedStorm", "HyperSphere", "Dual-Layer", "SlingShock", "Burst", "QuadDrive", "SlingShock", "Dual-Layer", "SwitchStrike", "Burst" };


   
//card information
void readData(string Data){
	
    string line;
    ifstream infile;
    infile.open("cards.txt");

    if (!infile.is_open())
        cout << "Failure to open the file";

    while (getline(infile, line))
    {
        cout << line << '\n';
    }

    infile.close();

}

//Generate a random card number
int RandomCardP1 (int Ran)
{
    unsigned int IDcardP1 = 0;
    IDcardP1 = rand()%9 + 1;

    return IDcardP1;
}

int RandomCardP2 (int Ran)
{
    unsigned int IDcardP2= 0;
    IDcardP2 = rand()%9 + 1;

    return IDcardP2;
}


//check system precedence
int CheckSystem (int systemP1, int systemP2, int scP1, int scP2)
{
    if (systemP1>systemP2)
        {
            cout << "Player 1" <<endl;
            scP1+=10;
        }
        else
        {
            cout << "Player 2" <<endl;
            scP2+=10;
        }
    return scP1,scP2;
}


//detail card and score in each round
void ShowCardP1(int IDcardP1, string beybladeName[], string pCode[], string type[], string plusMode[], string system[]) {
    cout<< "Product code: " << pCode[IDcardP1] << "  " << endl;
    cout<< beybladeName[IDcardP1] << "  " << endl ;
    cout<< "Type: " << type[IDcardP1] << "  " << endl;
    cout<< "PlusMode:" << plusMode[IDcardP1] << endl;
    cout<< "System: " << system[IDcardP1] << endl;
}


void ShowCardP2(int IDcardP2, string beybladeName[], string pCode[], string type[], string plusMode[], string system[]) {
    cout<< "Product code: " << pCode[IDcardP2] << "  " << endl;
    cout<< beybladeName[IDcardP2] << "  " << endl ;
    cout<< "Type: " << type[IDcardP2] << "  " << endl;
    cout<< "PlusMode:" << plusMode[IDcardP2] << endl;
    cout<< "System: " << system[IDcardP2] << endl;
}
Posted
Updated 17-Jul-22 14:25pm

I recommend that you drop the individual arrays for the data and use a class for each item and have an array of those class objects. Here is a class that could be used :
C++
class Card
{
public:
    int             ID;
    std::string     Name;
    std::string     Code;
    std::string     Type;
    std::string     Mode;
    std::string     System;

    void Show();
};
then your display function would be a member of the class and it could look this :
C++
void Card::Show()
{
    cout<< "ID    : " << ID   << endl;
    cout<< "Code  : " << Code << endl;
    cout<< "Name  : " << Name << endl;
    cout<< "Type  : " << Type << endl;
    cout<< "Mode  : " << Mode << endl;
    cout<< "System: " << System << endl;
};
 
Share this answer
 
v2
Comments
merano99 18-Jul-22 2:06am    
That's exactly what I meant when I suggested classes. Other classes in a hierarchy would make sense, such as deck, player, and game.The original question what's wrong with this code, see my solution below. (+5 for you)
Quote:
Whats the problem of this code?

1. C2601: "readData": Local function definitions are not allowed
Just like the prototypes, all functions should also be defined outside of main.

2. The prototype of void readData() differs from the definition.

3. The FinalScore() function doesn't exist at all.

4. The include should be named <string> not <cstring>

5. The two functions ShowCardP1() and ShowCardP2() with the same content do not appear to make sense.

Quote:
This is part of mine program but why the random number cant follow the file processing number?

Since the actual program is still completely missing, it is not clear how the parts belong together. The question cannot be answered because there are no random numbers or files used anywhere in the main program. It is striking, however, that a concept with classes should probably be better considered here.
 
Share this answer
 
Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900