Quantcast

Maximum PC

It is currently Fri Dec 26, 2014 5:52 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Can anyone help me figure out this c++ bug?
PostPosted: Sun Oct 12, 2008 1:11 pm 
Team Member
Team Member

Joined: Mon Jul 25, 2005 4:12 pm
Posts: 1273
Location: AL
These are the errors I am getting. I am new to c++ and just starting to use classes so I'm kinda lost.

Lab6.cpp(69) : error C2065: 'bank1' : undeclared identifier
.\Lab6.cpp(69) : error C2228: left of '.Withdraw' must have class/struct/union
type is ''unknown-type''
.\Lab6.cpp(74) : error C2065: 'bank1' : undeclared identifier
.\Lab6.cpp(74) : error C2228: left of '.GetBalance' must have class/struct/union
type is ''unknown-type''



Code in Main

Code:
// Lab6.cpp : main project file.

#include "stdafx.h"
#include <string>
#include <iostream>
#include "BankAccount.h"

//using namespace System;
using namespace std;


int main()
{
    //Begin Main
   int k,m,n=1,ini_acc;
   double money,x,ini_bal;
   char choice;

   

//Main Welcome prompt
   cout<<"------------------------------------------------------------------\n\n";
   cout<<"Welcome to AUFederal\n\n";
   
   
//Loops the main program to allow multipe runs to be tested without needing to rerun.

while(n==1){
   k=1;
   while (k==1){

//This is for account one which uses the stack
//BankAccount bank1;

//BankAccount bank1 (ini_acc, ini_bal);  //account on the stack

   cout<<"\nPlease enter the account number you wish to access.\n\n";
   cin>>ini_acc; //set initial account number

   cout<<"\n\nPlease enter the your starting balance.\n\n";
   cin>>ini_bal;

   BankAccount bank1 (ini_acc, ini_bal);

   cout<<"\nPlease enter the amount you wish to deposit in account 1 $xxxx.xx\n\n";
   cin>>money;
   //read in deposit amount

   bank1.Deposit(money); //deposit tbe money
   
   cout<<"Do you wish to make another deposit? y/n \n";
   cin>>choice;

   //deposit again?

      if (choice == 'y'){
      k=1;
      }
      else{
         k=0;
      }
   }

   cout<<"\nPlease enter the amount you wish to withdraw $xxx.xx\n\n";
   cin>>money;

   //how much to withdraw?

   x=bank1.Withdraw(money);
   cout<<"\nYou were able to withdraw: "<<x<<"\n\n";

   //get the balance

   x= bank1.GetBalance();

   cout<<"Your balance is: "<<x<<"\n\n";



//Now run for account which uses the heap
   BankAccount^ bank2 = gcnew BankAccount(ini_acc, ini_bal); //acount on the heap
   m=1;
   while (m==1){

   //repeat for account 2.

   cout<<"\nPlease enter the amount you wish to deposit in account 2 $xxxx.xx\n\n";
   cin>>money;
   
   bank2->Deposit(money);

   
   cout<<"Do you wish to make another deposit? y/n \n";
   cin>>choice;

      if (choice == 'y'){
      m=1;
      }
      else{
         m=0;
      }
   }

   cout<<"\nPlease enter the amount you wish to withdraw $xxx.xx\n\n";
   cin>>money;

   x=bank2->Withdraw(money);
   cout<<"\nYou were able to withdraw: "<<x<<"\n\n";

   x=bank2->GetBalance();

   cout<<"Your balance is: "<<x<<"\n\n";

//*/


   cout<<"Do you wish to rerun the program? y/n \n";
   cin>>choice;

      if (choice == 'y'){
      n=1;
      }
      else{
         n=0;
      }

}
}


Code in header file
Code:
#include "stdafx.h"

ref class BankAccount

{
   private:
      double balance;
      int account_Number;

   public:
      BankAccount():balance(0.0),account_Number(0)
      {
      }
      BankAccount(int ini_account, double ini_balance)
      {
         balance=ini_balance;
         account_Number=ini_account;
      }
      void Deposit(double amt);
      double  GetBalance();
      double Withdraw(double amt);
      int Acc_Num();

};



Code in function cpp file
Code:
#include "stdafx.h"
#include "BankAccount.h"



void BankAccount::Deposit(double amt)
{
balance+= amt;
}

double BankAccount::GetBalance()
{
   return balance;

}

double BankAccount::Withdraw(double amt)
{
   double total;
   if(balance <= amt)
   {
      total=balance;
      balance=0;
         return total;
   }
   else
   {
      total=amt;
      balance -= amt;
      return total;
   }
}

int BankAccount::Acc_Num()
{
   return account_Number;
}




Top
  Profile  
 
 Post subject:
PostPosted: Mon Oct 13, 2008 8:00 am 
Million Club - 5 Plus*
Million Club - 5 Plus*
User avatar

Joined: Sun Sep 12, 2004 6:37 pm
Posts: 4745
Location: In the monkey's litterbox
How did you create your C++ project? Was it a CLR project or a Win32 project?


"ref class" looks wrong -- try "class".


Top
  Profile  
 
 Post subject:
PostPosted: Mon Oct 13, 2008 8:08 am 
Team Member
Team Member

Joined: Mon Jul 25, 2005 4:12 pm
Posts: 1273
Location: AL
Project is a CLR in visual studio.

ref class is supposed to make it run on the managed heap.

The first half of the program is supposed to run using the stack, the second half is supposed to run using the managed heap. Both are supposed to perform the exact same function.

If you comment out the first half of the code in main, the bottom half runs fine, but the first half will not compile.


Top
  Profile  
 
 Post subject:
PostPosted: Mon Oct 13, 2008 11:33 am 
Little Foot
Little Foot

Joined: Thu Jun 21, 2007 11:00 am
Posts: 102
Far as I can see, bank1 is declared inside of while(k==1) block, which makes this block its scope.

Withdraw and GetBalance methods are both accessed outside the block, no longer within the name scope of bank1.

Either extend the inner while to end after GetBalance, or move bank1 declaration to the outside while.

EDIT - looking at 2nd half of code, moving bank1 declaration above "while(k==1)" is what you want to do.


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.