C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 29th, 2005, 10:59 PM
SeanJA101 SeanJA101 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 6 SeanJA101 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 27 m 43 sec
Reputation Power: 0
Strange Problem

I am getting the error:

C:\Program Files\Microsoft Visual Studio\MyProjects\COSC 104\Assignment 4\Account.cpp(12) : error C2440: '=' : cannot convert from 'char []' to 'char [21]'
There are no conversions to array types, although there are conversions to references or pointers to arrays

for the files:

Account.cpp
Code:
#include <iostream>
  #include <Account.h>
  
  using namespace std;
  
  Account::Account( int accountID, char accountName[21], float initialBalance )
  // A new account is created with the given ID, name, and balance.  
  // If initial balance is negative then balance is zero.
  {
  
  	id = accountID;
  	name = accountName;
  
  	if ( initialBalance >= 0.0 )
  		balance = initialBalance;
  	else
  		balance = 0.0;
  }
  
  int x = 0, w = 0, d = 0;
  char y[21];
  float z;
  main()
  {
  	cout << "Enter ID Name and Initial Balance\n";
  	cin >> x, y, z;
  	Account joe(x, y, z);
  
  	
  	// Test accessor methods
  
  	cout <<y <<"\'s ID is " << joe.getID() << "\n";
  	cout <<y <<"\'s name is " << joe.getName() << "\n";
  	cout <<y <<"\'s balance is " << joe.getBalance() << "\n";
  
  
  	// Test withdraw method 
  	cout	<< "Withdraw how much?";
  	cin	>> w;
 	cout	<< "\nAttempting to withdraw $"<< w << " from" << y << endl;
  	joe.withdraw(50);
 	cout	<<y <<"\'s balance is " << joe.getBalance() << "\n";
  
     
  	// Test deposit method
  	cout << "Deposit howmuch?";
  	cin >> d;
 	cout	<< "\nAttempting to deposit $"<< d <<" into "<< y <<"...\n";
  	joe.deposit(100);
 	cout	<< y <<"\'s balance is " << joe.getBalance() << "\n";
  
  	return 0;	// normal exit code
  }
  
  
  void Account::deposit( float amount )
  //Adding money to the account, if it is positive, 
  //if it is negative, then error message
  {
  	balance = balance + amount;
  }
  
  void Account::withdraw( float amount )
  // If the amount to withdraw is not negative, and
  // the account has sufficient money, then the balance is
  // decreased by that amount; otherwise, an error message is displayed.
  {
  	balance = balance - amount;
  }
  
  float Account::getBalance()
  // Returns the current balance of the account
  {
  	return balance;
  }
  
  int Account::getID()
  //Returns the account's ID
  {
  	return id;
  }
  
  char Account::getName()
  //Returns customer's name
  {
  	return name[21];
  }


and

Account.h
Code:
#ifndef ACCOUNT_H
    #define ACCOUNT_H
    
    using namespace std;
    
    class Account
    
    {
    public:
    
    	Account(int accountID, char accountName[21], float initialBalance);
    //Creates a new account
    	void deposit( float amount );
    //Adds money to the account
    	void withdraw( float amount );
    //The account has money taken away from it
    	float getBalance();
    //How much money is in the account
    	int getID();
    //Shows the account Number
    	char getName();
    //Shows the owner of the account
    private:
    
 	int		id;		 // account id
    	char	name[21];	   // customer name
    	float	balance;	// current balance of the account
    };
    
    #endif
    


and I was just wondering what would the cause of this error would be, I have looked through it many times trying to figure it out with no avail.

Thanks in advance for any help on this,

--Sean Sandy

I know that there are probably other things wrong with it as well, I am trying to get this to work from another example, and I am learning as I go

Reply With Quote
  #2  
Old March 30th, 2005, 08:04 AM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
I guess "name" is string / array of characters. The Account constructor takes second argument as single character.

The statement
Code:
 name = accountName 
is wrong,as L-value and R-Value are incompatible.

To remove this , the second parameter to constructor should be arrayname or pointer to character.

I think you are putting accountName[21] to limit the name entered, this restriction can be put inside constructor by
if( accountName.lenght > 21) throw error [This is hypothetical syntax].

Reply With Quote
  #3  
Old March 30th, 2005, 09:59 AM
SeanJA101 SeanJA101 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 6 SeanJA101 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 27 m 43 sec
Reputation Power: 0
Quote:
The statement
Code:
 name = accountName 
is wrong,as L-value and R-Value are incompatible.


I can't figure out why they are incompatible, any ideas? I have tried going through and changing both to:

Code:
char suchandsuch[21];


but that doesnt seem to work?

--Sean Sandy

Reply With Quote
  #4  
Old March 30th, 2005, 01:29 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
name = accountID is wrong because they are not of same category or not convertible.

By convertible I mean, one quantity can have same domain and range as other.
Illustration:
int a ;
and float b;
when you write b = a , it works because, 'a' is then treated as type float.
If you do other way round , ther e will be a warning message citing loss of data.

With character and string the case is different,
An array represents group of values of identical class/types.

Thus you can not comapre an individual character to group of characters.

In
Quote:
char suchandsuch[21];


if you write name = suchandsuch , it is correct since name is of type pointer to char, whereas suchandsuch is an array OR constant pointer to character.
Thus its vice-versa is wrong.

Reply With Quote
  #5  
Old March 30th, 2005, 01:31 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
In Account::Account, put the second parameter in any of the following form:

A) char *accountID
B) char accountID[] { Do not write number inside }

Reply With Quote
  #6  
Old April 3rd, 2005, 02:49 PM
SeanJA101 SeanJA101 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 6 SeanJA101 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 27 m 43 sec
Reputation Power: 0
Talking

Ok, so I did some more tinkering with the program, and it is almost there... I just have one problem, I don't know how to make it so that it allows the creation of new accounts (or just the replacement of the first account created)?

Here is what I have now:
file: Account.h
Code:
//
   
 #ifndef ACCOUNT_H		//If the identifier has not been defined define the identifier as.
   #define ACCOUNT_H		//Define the identifier as.
   
   #include <string>		// STL string class used for the account name
   using namespace std;	//Used to define members within this namespace.
   
   class Account
   // The Account class defines a simple bank account object that contains the
   // Account ID, the Account owner's name and the Account balance.
   // The class provides functions to access the ID, owner's name and balance.
   // The class also provides deposit and withdraw functions that modify
   // the state of the class by changing the balance.
   
   {
   public:
   
   	Account( int accountID, string accountName, double initialBalance );
   	// Parameterized constructor
   	// POST: A new account is created with the given ID, name, and balance.  If
   	// initial balance is negative then balance is zero.
   
   	
   	void Account::PreDeposit ();
   	//Mutator
   	//Post:  Calls deposit with amount entered by the user.
   	void deposit( float amount );
   	// Mutator
   	// POST: the account's balance is increased by amount.
   	void PreWithdraw();
   	//PreMutator
   	//Post: Calls withdraw with amount entered by user.
   	void withdraw( float amount );
   	// Mutator
   	// POST: the account's balance is decreased by amount
   
   	double getBalance();
   	// Accessor
   	// POST:  The current balance of the account is returned.
   
   	int getID();
   	// Accessor
   	// POST:  The account ID is returned.
   
   	string getName();
   	// Accessor
   	// POST:  The customer name is returned.
   
   	void getInfo();
   	//Accessor
   	//Post:   The account's info is returned via getName, getID, and get Balance.
   
   private:
   
   	int	id;		 // account id
   	string name;	   // customer name
   	double balance;	// current balance of the account
   };
   
   #endif
   


file: Account.cpp

Code:
   #include <iostream>	 // cout object and operator<< used in deposit()
 					 // and withdraw()
   #include <string>	   // STL string class used in Account() and getName()
   #include "Account.h"	// Used for declarations
   
   using namespace std;	//Used to define members within this namespace.
   
   // Class Invariants:
   // : 1000 <= id <= 9999
   // : balance >= 0
   
   Account::Account( int accountID, string accountName, double initialBalance )
   // Parameterized constructor
   // POST: A new account is created with the given ID, name, and balance.  If
   // initial balance is negative then balance is zero.
   {
   	id = accountID;
   	name = accountName;
   
   	if ( initialBalance >= 0.0 )
   		balance = initialBalance;
   	else
   		balance = 0.0;
   }
   
   void Account::PreDeposit ()
   {
   	float d;
   	do{
   		cout << "Enter an amount to deposit into the account:";
   		cin >> d;
   		if (d < 0)
   			cout << "Please reenter amount";
   	}while (d < 0);
   	if (d > 5000)
   		d = (d * 0.01) + d;
   	if ((d < 5000) && (d > 2000))
   		d = (d * 0.02) + d;
   	else
   		d = d;
   	
 	cout << "\nAttempting to deposit $"<< d <<" into "<< getName() <<"\'s account\n";
   	deposit(d);
   	cout <<getName() <<"'s balance is $" << getBalance() << "\n";
   }
   
   void Account::deposit( float amount )
   // Mutator
   // POST: If the amount to deposit is not negative,
   //	   then the account's balance is increased by that amount;
   //	   otherwise, an error message is displayed.
   {
   	balance = balance + amount;
   }
   
   void Account::PreWithdraw ()
   {
   	float w;
   	do{
   		cout << "Enter an amount to withdraw from the account:";
   		cin >> w;
   		if ((w < 0) || (w > balance))
   			cout << "Please reenter amount";
   	}while ((w < 0) || (w > balance));
   	
 	cout << "\nAttempting to withdraw $"<< w <<" from "<< getName() <<"\'s account\n";
   	withdraw(w);
   	cout <<getName() <<"'s balance is $" << getBalance() << "\n";
   }
   
   void Account::withdraw( float amount )
   // Mutator
   // POST: If the amount to withdraw is not negative, and
   //	   the account has sufficient money, then the balance is
   //	   decreased by that amount; otherwise, an error message is displayed.
   {
   	balance = balance - amount;
   }
   
   
   double Account::getBalance()
   // Accessor
   // POST:  The current balance of the account is returned.
   {
   	return balance;
   }
   
   
   int Account::getID()
   // Accessor
   // POST:  The account ID is returned.
   {
   	return id;
   }
   
   
   string Account::getName()
   // Accessor
   // POST:  The customer name is returned.
   {
   	return name;
   }
   
   void Account::getInfo()
   //Accessor
   //Post:   The account's info is returned via getName, getID, and get Balance.
   {
   	cout << "The account number is "<< getID() << endl;
   	cout << "The account's owner is "<<getName() << endl;
   	cout << "The balance is $"<<getBalance() << endl;
   }
   


file: Account-driver.cpp

Code:
   #include <iostream>	 // cout object and operator<< used in main() 
   #include "Account.h"	// Used for declarations
   #include <ctype.h>		// toupper object, used in main()
   using namespace std;	//Used to define members within this namespace.
   string name;
   int id;
   float balance, w, d;
   int main()
   // Library facilities used: cout object and operator<< from stream.h
   {
   	char choice; //Used as the choice in the menu
   	cout << "Creating a new account\n";
   	cout << "Enter a name:";
   	cin >> name;
   	cout << "Enter an ID number:";
   	cin >> id;
   	cout << "Enter an initial Balance:";
   	cin >> balance;
   	Account account(id, name, balance);
   	do{
   		cout << "C - Create Account" << endl;
   		cout << "W - Withdraw" << endl;
   		cout << "D - Deposit" << endl;
   		cout << "I - Account info" << endl;
   		cout << "Q - Quit" << endl;
   		cout << "Enter Choice:";
   		cin >> choice;
   		
   		if (toupper(choice) == 'C')
   		{
   			
   			cout << "Creating a new account\n";
   			cout << "Enter a name:";
   			cin >> name;
   			cout << "Enter an ID number:";
   			cin >> id;
   			cout << "Enter an initial Balance:";
   			cin >> balance;
   			Account account(id, name, balance);
   		}
   		
   		if (toupper(choice) == 'W')
   		{
   			account.PreWithdraw ();
   		}
   
   		if (toupper(choice) == 'D')
   		{
   			account.PreDeposit ();
   		}
   		if (toupper(choice) == 'I')
   		{
   			account.getInfo();
   		}
   	}while (toupper(choice) != 'Q'); 
   
   	return 0;	// normal exit code
   }
   


Thanks for the help so far though, but I had to do it this way because using pointers didn't work for some reason, probably cause I did it wrong...

--Sean Sandy

Reply With Quote
  #7  
Old April 4th, 2005, 11:51 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
In file Account-driver.cpp,

"account()" function is recursive. I think you are trying to call constructor of Account class here , hoping it will create an account.

Well, Here you need to put statements:
Code:
   Account account( <list of params>) {
    
        Account newAccount = new Account(<paramaters>) ; 
 
 return newAccount; //return it
   }
   
  

The statement inside account() creates an object of Account class and calls on constructor Account(<parmas>) that create your account.

Instead, you were calling account() inside account() , this is recursiive and won't give you results.

Reply With Quote
  #8  
Old April 7th, 2005, 08:47 AM
mail2virag mail2virag is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 3 mail2virag User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 31 sec
Reputation Power: 0
Talking

use
Code:
strcpy(str1,str2) 

to copy one character array to other one.

Thanks
V..

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Strange Problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT