mardi 9 octobre 2018

Is my make file wrong? or is my cpp file wrong?

CPP_FLAGS = -std=c++11 -Wall -Wextra -Werror

CC = g++
RM = rm -f

Main.exe: Vending.o Main.cpp
        $(CC) $(CPP_FLAGS) Vending.o Main.cpp -o Main.exe

Vending.o: Vending.h Vending.cpp
        $(CC) $(CPP_FLAGS) -c Vending.cpp -o Vending.o


clean:
        $(RM) *.o

cleanall: clean
        $(RM) *.exe

This is my make file ^^^ I am having problems when I run my code below is my header file, and my two cpp files. When I run the program it skips all of the code in my main until it reaches "chingones.receipt()" which outputs "Total amount of credits spent: 0". I'm not sure if its my code or if its my make file, because it works fine with out my makefile but I need to include a make file for my assignment.

#pragma once

#include <iostream>
#include <vector>
#include <iostream>

using std::cout;
using std::endl;
using std::vector;
using std::string;
using std::cin;

struct Machine {
  int number;
  string product;
  int price;
  int stock;
  int count;
  friend std::ostream& operator << (std::ostream &out, const Machine &m) {
    out << m.number << ".    " << m.product << "           " << m.stock << "             " << m.price;
    return out;
  }
};

class Vending    {
    public:
        void load();
        void display();
        void decrement(unsigned int i);
        char choice(unsigned int i);
        int pay(unsigned int i, unsigned int k);
        void changeCalculator(int change);
        void receipt();
    private:
        vector<Machine> items;
        vector<string> history;
};

#include "Vending.h"

void Vending::load() {
  items.push_back({1, "Aquafina", 31, 5, 0});
  items.push_back({2, "Pepsi   ", 20, 5, 0});
  items.push_back({3, "Lemonade", 10, 5, 0});
  items.push_back({4, "Horchata", 43, 5, 0});
  items.push_back({5, "Sprite  ", 18, 5, 0});
}
void Vending::display() {
  for(unsigned int i = 0; i < items.size(); i++){
    cout << items.at(i) << endl;
  }
}

void Vending::decrement(unsigned int i) {
  if(items.at(i-1).stock > 0) {
    items[i-1].stock--;
  }
}

char Vending::choice(unsigned int i) {
  i--;
  if(items.at(i).stock != 0){
    cout << "You chose " << items.at(i).product << endl;
    return 'c';
  }
  else {
    cout << "Sorry the product you chose is out of stock" << endl;
    cout << "If you want to continues shopping input (y) if your done input (n)" << endl;
    char returns;
    cin >> returns;
    return returns;
  }

}
int Vending::pay(unsigned int i, unsigned int k) {
  k = k - items.at(i-1).price;
  cout << k << endl;
  history.push_back(items.at(i-1).product);
  items.at(i-1).count++;
  return k;
}
void Vending::changeCalculator (int change) {
  int quarters;
  int dimes;
  int nickels;
  int pennies;

  quarters = (change / 25);
  dimes = (change % 25) / 10;
  nickels = ((change % 25) % 10) / 5;
  pennies = (((change % 25) % 10 ) % 5);

  cout << "This is your Change" << endl;
  cout << "Amount of quarters: " << quarters << endl;
  cout << "Amount of dimes: " << dimes << endl;
  cout << "Amount of nickels: " << nickels << endl;
  cout << "Amount of pennies: " << pennies << endl;
}
void Vending::receipt() {
  unsigned int total = 0;
  for(unsigned int i = 0; i < items.size(); i++) {
    if(items[i].count > 0) {
        total = total + items.at(i).price * items.at(i).count;
        cout << "You bought " << items[i].count << " " << items[i].product << endl;
    }
  }
  cout << "Total amount of credits spent: " << total << endl;
}
#include "Vending.h"

int main() {

  int userChoice = 0;
  int credits;
  int change;
  Vending chingones;
  chingones.load();
  while (userChoice != 0 ) {
  cout << "This is our stock:" << endl;
  cout << "[#]   [Product]     [Quantity]      [Price]" << endl;
  chingones.display();
  cout << "Exit program by inputing 0" << endl;
  cout << "What would you like to buy?" << endl;
    cin >> userChoice;
  if(userChoice == 0){
    break;
  }
  char x = chingones.choice(userChoice);
  if(x == 'c'){
    cout << "Enter the amount of credits your going to pay with" << endl;
    cin >> credits;
    change=chingones.pay(userChoice,credits);
    chingones.changeCalculator(change);
    chingones.decrement(userChoice);
    cout << endl;
  }
  else if (x == 'n'){
    break;
  }
  else{
    system("clear");
  }
}
  chingones.receipt();
  return 0;
}

Aucun commentaire:

Enregistrer un commentaire