链接.h和.ccp文件/传递参数

时间:2016-09-24 19:46:16

标签: c++

我正在开发我的第一个c ++项目。我被提供了一个.h文件,不得不写一个.cpp文件,但它不会编译。我一直收到错误消息:

g ++ -o proj1 proj1.cpp proj1.h /tmp/ccsK4pOL.o:在函数menuMain()': proj1.cpp:(.text+0x807): undefined reference to displayBalance(float)' proj1.cpp :(。text + 0x81b):未定义引用deposit(float&)' proj1.cpp:(.text+0x82f): undefined reference to撤销(浮动&)' collect2:ld返回1退出状态

这是我无法更改的.h文件:

#ifndef PROJ1_H
#define PROJ1_H

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

/*
Name: menuStart()
PreCondition: None
PostCondition: Returns the selected option (1, 2, or 3)
*/

int menuStart();

/*
Name: menuMain()
PreCondition: The user input has either been loaded from file or entered by user
PostCondition: Returns the selected option (1, 2, 3, 4, or 5)
*/
int menuMain();

/*
Name: displayAccountDetails
PreCondition: Relevant data (fName, lName, age, accountBalance) has been loaded/entered
PostCondition: None (void)
 */
void displayAccountDetails(string fName, string lName, int age, float accountBalance);

/*
Name: displayBalance
PreCondition: Relevant data (accountBalance) has been loaded/entered
PostCondition: None (void)
*/
void displayBalance(float accountBalance);

/*
Name: deposit
PreCondition: Variable accountBalance is passed-by-reference
PostCondition: accountBalance is permanently changed via pass-by-reference
*/
void deposit(float &accountBalance);

/*
Name: withdraw
PreCondition: Variable accountBalance is passed-by-reference
PostCondition: accountBalance is permanently changed via pass-by-reference
*/
void withdraw(float &accountBalance);

#endif

这是我的代码 - 任何帮助调试都会受到赞赏,因为我对此很陌生。

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "proj1.h"
using namespace std;



int main()
{

  menuStart();

  return 0;
}






// function definitions

int menuStart() //has user load txt file, enter info or exit
{
  string firstName;
  string lastName;
  int userAge;
  float balance;
  int userIn;
  cout << "Welcome to the UMBC ATM" << endl;
  cout << "Choose from below: " << endl;
  cout << "1. Load a User Profile from File" << endl;
  cout << "2. Enter a new User Profile" << endl;
  cout << "3. Exit" << endl;
  cout << "Enter your choice: " << endl;
 cin >> userIn;

  //make sure inupt is valid
  while (userIn < 1 || userIn > 3)
    {

      cout << "INVALID. Please choose 1, 2, or 3." << endl;

      cout << "Choose from below: " << endl;
      cout << "1. Load a User Profile from File" << endl;
      cout << "2. Enter a new User Profile" << endl;
      cout << "3. Exit" << endl;
      cout << "Enter your choice: " << endl;
      cin >> userIn;
      return 0;
    }

  if (userIn == 1)
    {

      //read text file proj1.txt
      ifstream profileTxt;
 profileTxt.open("proj1.txt");

      profileTxt >> firstName >> lastName >> userAge >> balance;

      cout << "Name: " << firstName << lastName << endl;
      cout << "Age: " << userAge << endl;
      cout << "Balance: " << balance << endl;

      profileTxt.close();
      return 0;
    }

  if (userIn == 2)
    {

      //gather info
      cout << "Please enter the following: " << endl;
      cout << "First Name: " << endl;
      cin >> firstName;
      cout << "Last Name: " << endl;
      cin >> lastName;
      cout << "Age: " << endl;
      cin >> userAge;
      cout << "Initial Deposit: " << endl;
      cin >> balance;
      cout << balance << "deposited." << endl;

      //write to text file to read later
      ofstream profileTxt;
      profileTxt.open("proj1.txt");
      profileTxt << firstName << lastName << userAge << balance << endl;
      profileTxt.close();

      menuMain();
      return 0;
    }

  else
    {

      //exit the program
      return 0;

    }
 }
int menuMain() //main menu, user chooses 1-5
{
  int userIn;

  string firstName;
  string lastName;
  int userAge;
  float balance;

  //open user's profile info*
  ifstream profileTxt;
  profileTxt.open("proj1.txt");

  profileTxt >> firstName >> lastName >> userAge >> balance;

  cout << "********Please choose option from the menu******** " << endl;
  cout << "1. Account Balance" << endl;
  cout << "2. Deposit money" << endl;
  cout << "3. Withdraw money" << endl;
  cout << "4. Dislay your account details" << endl;
  cout << "5. Exit" << endl;
  cout << "Enter your choice: " << endl;
  cin >> userIn;

  //make sure inupt is valid
  while (userIn < 1 || userIn > 5)
    {

      cout << "INVALID. Please choose 1 - 5." << endl;

      cout << "********Please choose option from the menu******** " << endl;
      cout << "1. Account Balance" << endl;
      cout << "2. Deposit money" << endl;
      cout << "3. Withdraw money" << endl;
      cout << "4. Dislay your account details" << endl;
      cout << "5. Exit" << endl;
      cout << "Enter your choice: " << endl;
      cin >> userIn;
  }

  if (userIn == 1)
    {

      displayBalance(balance);

    }

  if (userIn == 2)
    {

      deposit(balance);

    }

  if (userIn == 3)
    {

      withdraw(balance);
    }

  if (userIn == 4)
    {

      displayAccountDetails(firstName, lastName, userAge, balance);

    }

  else
    {

      string save;
      cout << "Would you like to save your account information?: " << endl;
      cout << "yes or no" << endl;
      cin >> save;

      while ((save != "yes") || (save != "no"))
        {

          cout << "INVALID. Enter yes or no." << endl;
          menuMain();
        }

      if (save == "yes") //save info to text file
//code to save info to text file
          ofstream profileTxt;
          profileTxt.open("proj1.txt");
          profileTxt << firstName << lastName << userAge << balance << endl;
          profileTxt.close();
          cout << "Thank you for using the UMBC ATM";

          return 0;
        }

      else
        {

          cout << "Thank you for using the UMBC ATM";

          return 0;

        }



    }
  profileTxt.close();
  return 0;

}





void displayBalance(float& accountBalance) //displays account balance
{
  string firstName;
  string lastName;
  int userAge;
  float balance;

  //open user's profile info*
  ifstream profileTxt;
  profileTxt.open("proj1.txt");

  profileTxt >> firstName >> lastName >> userAge >> balance;
 cout << "Account Balance: " << &balance << endl;

  profileTxt.close();

  menuMain();

}




void deposit(float accountBalance) //deposits into account
{
  string firstName;
  string lastName;
  int userAge;
  float balance;

  //open user's profile info*
  ifstream profileTxt;
  profileTxt.open("proj1.txt");

  profileTxt >> firstName >> lastName >> userAge >> balance;

  float depAmount;
  cout << "Please enter the amount to be deposited" << endl;
  cin >> depAmount;

  if (depAmount < 0)
    {

      cout << "Amount should be greater than 0" << endl;
      profileTxt.close();
      menuMain();
    }

  else
    {

      balance = balance + depAmount;
      cout << depAmount << " deposited to your account";
      profileTxt.close();
      menuMain();

    }
void withdraw(float accountBalance) //withdraws from account
{
  string firstName;
  string lastName;
  int userAge;
  float balance;

  //open user's profile info*
  ifstream profileTxt;
  profileTxt.open("proj1.txt");

  profileTxt >> firstName >> lastName >> userAge >> balance;

  float withAmount;
  cout << "Please enter the amount to be withdrawn" << endl;
  cin >> withAmount;

  if ((withAmount > balance) || (withAmount <0) )
    {

      cout << "Amount should be greater than 0 and greater than current account balance" << e\
ndl;
      profileTxt.close();
      menuMain();

    }

  else
    {

      balance = balance - withAmount;
      cout << withAmount << " withdrawn from your account" << endl;
      profileTxt.close();
      menuMain();

    }
void displayAccountDetails(std::string fName, std::string lName, int age, float accountBalanc\
e)
{
  string firstName;
  string lastName;
  int userAge;
  float balance;

  //open user's profile info*
  ifstream profileTxt;
  profileTxt.open("proj1.txt");

  profileTxt >> firstName >> lastName >> userAge >> balance;


  cout << "First Name : " << firstName << endl;
  cout << "Last Name : " << lastName << endl;
  cout << "Age : " << userAge << endl;
  cout << "Account Balance: " << balance << endl;

  profileTxt.close();

  menuMain();

}

0 个答案:

没有答案
相关问题