-1

I want to make sure i'm calling functions correctly. The functions that are in another .cpp are makeDeck() and shuffle(). I also do not know how to make a relevant header file or when they are helpful. Could someone write or walk me through the syntax of my deck.cpp and deck.h files that will work with the following: main.cpp

#include <iostream>
#include "deck.h"
using namespace std;


int main() {
  int size;
  do {
    cout << "Enter size of deck (5-50): " << endl;
    cin >> size;
  } while (size<5 || size>50);

  int** deck[size] = makeDeck(size);
  shuffle(deck);

  int score = 0;
  char guess = NULL;

  for (int** i = **deck; *i != NULL; i++) {
    cout << "Score: " + score << endl;
    cout << "Current card: " + *i<< endl;
    cout << "Will the next card be higher (high) or lower (low) than " + *i + "?" << endl;
    cin << guess;
    if (guess == "higher" || guess == "high") {
      if (*(i + 1) > *i)
        score++;
      else
        score--;
    }
    if (guess == "lower" || guess == "low") {
      if (*(i + 1) < *i)
        score++;
      else
        score--;
    }
  }
}
  • 2
    Too broad. Read some C++ tutorials and come back again when you have a specific problem. – user202729 Feb 27 '18 at 03:29
  • My problem is i don't know how to make the deck.cpp file or the deck.h file. I know those are easy for experienced programmers so it would be more efficient to just get a directly relevant answer. I've looked at a lot of tutorials and they're different enough that I don't think I could use them exactly. – ace of base Feb 27 '18 at 03:35
  • It's efficient for you, but not for them. – user202729 Feb 27 '18 at 03:36
  • 1
    Not reading a tutorial and just ask on StackOverflow implies lack of researching effort. On StackOverflow we require everyone to research about their question before asking. – user202729 Feb 27 '18 at 03:40
  • (learning about the syntax while you've known all about how to programming is going to be very easy, consider that C++ and Java are both procedural programming languages) – user202729 Feb 27 '18 at 03:42
  • I don't even know what that means. I've done research, how do you think I got this far ffs? – ace of base Feb 27 '18 at 03:44
  • Possible duplicate of [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Jalpesh Patel Feb 27 '18 at 04:32
  • Jalpesh I wouldn't have known how to ask that question to find that result. – ace of base Feb 27 '18 at 05:16

1 Answers1

0

In general, .h files are used when you have classes, but you can use them also for functions if you think you might use them in another project or you want to have a cleaner main file. You have the right file #included, but for the actual .h and .cpp files, you will want something like the following:

deck.h
#ifndef DECK_H
#define DECK_H

    int** makedeck(const int&);
    void shuffle(int**);

#endif    //DECK_H

deck.cpp
#include "deck.h"
    int** makedeck(const int& size)
    {
        //Do something
    }

    void shuffle(int** deck)
    {
        //Do something else
    }

For any functions you don't want in main() you would do the declaration in the .h file, then define it in the .cpp. There is no real difference between having functions declared separately in main and separating them into multiple files when it comes to syntax, just make sure you include the .h file in any file that will need to use the functions you declare in the .h.

Hawkeye5450
  • 672
  • 6
  • 18
  • This was all I ever wanted. – ace of base Feb 27 '18 at 03:57
  • So should I declare `int size;` as `const int size;` in `main.cpp`? – ace of base Feb 27 '18 at 04:34
  • No, if you did, you wouldn't be able to assign it to what the user reads in, What I have just means that the function can't change that variable. [Here](http://www.cplusplus.com/doc/tutorial/functions/) is a pretty good explanation of const and functions in general – Hawkeye5450 Feb 27 '18 at 04:46