473,788 Members | 2,848 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string and class error

i have created a class which contains all the information needed for a
program based on accounts, this is shown below. When compiled the
string "word" (in function writetofile) which is initialised in the
constructor is apparently "undeclared ", yet it has been declared in
the constructor.

i am new to c++ class programming, do i need to also declare it, as
well as initialise? and if not, what is the problem and how can it be
solved?

thanks

wilson

#include <iostream>
#include <fstream>
#include <time.h>
#include <cstdlib>
#include <cstring>
using namespace std;
class Account
{
friend void new_account();
public:
Account(){strin g word = "hello";}//can eventually be user
inputted
void returnbalance() { std::cout << balance <<
std::endl; }
void writetofile()
{
string extension = ".txt";
ofstream writetofile;
writetofile.ope n((word + extension).c_st r());
writetofile << balance;
}
void deposit(float amount)
{
balance = balance + amount;
std::cout << "$" << amount << " Has been deposited
into your account";
std::cout << std::endl << "Your balance is now $" <<
balance;
}
virtual void withdraw(float amount)
{
if(amount < balance)
{
balance = balance - amount;
std::cout << "$" << amount << "has been
withdrawn from your account";
std::cout << std::endl << "Your balance is
now $" << balance;
}
else
{
std::cout << std::endl << "Insufficie nt funds,
your balance is $" << balance;
}
}
float balance;

private:
int pin_number;
int account_number;
};

Jun 29 '07
10 1517

Wilson <tp****@googlem ail.comwrote in message...
i have created a class which contains all the information needed for a
program based on accounts, this is shown below. When compiled the
string "word" (in function writetofile) which is initialised in the
constructor is apparently "undeclared ", yet it has been declared in
the constructor.
No, you didn't! see below
>
i am new to c++ class programming, do i need to also declare it, as
well as initialise? and if not, what is the problem and how can it be
solved?
thanks, wilson

#include <iostream>
#include <fstream>
#include <time.h // remove this, you are not using it.
#include <cstdlib // remove this, you are not using it.
#include <cstring // remove this, you are not using it.
using namespace std;
// if this is a header, remove that!!
class Account{
friend void new_account(); // ???
public:
Account(){strin g word = "hello";}
//can eventually be user inputted

Where did you declare/define 'string'?
Hint: #include <string>

Account( std::string mystr = "hello" ){
std::string word = mystr; // or: word( mystr );
} // 'word' disappears right here.
void returnbalance() {
std::cout << balance <<std::endl;
}
Bad name. If you say 'return', then return something.

float ReturnBalance() const { // should use 'double'
return balance;
}

// void writetofile(){
// string extension = ".txt";
// ofstream writetofile;
// writetofile.ope n((word + extension).c_st r());

Note: 'word' is NOT defined here (it's not *in* your class).

void WriteToFile( std::string const &word ) /* const */ {
std::string filename( word );
filename += ".txt";
std::ofstream ofile( filename.c_str( ) );
if( not ofile.is_open() ){ /* error */ return;}
// ofile << balance;

WriteTo( ofile ); // see below
WriteTo( std::cout ); // see below
}
void WriteTo( std::ostream &out ) /* const */ {
if( not out ){ /* error */ return;}
out << balance;
} // now you can 'print' to any std ostream obj.
void deposit(float amount){
balance = balance + amount;
std::cout << "$" << amount
<< " Has been deposited into your account";
std::cout << std::endl
<< "Your balance is now $" << balance;
}
virtual void withdraw(float amount){
if(amount < balance){
balance = balance - amount;
std::cout << "$" << amount
<< "has been withdrawn from your account";
std::cout << std::endl
<< "Your balance is now $" << balance;
}
else{
std::cout << std::endl
<<"Insufficie nt funds, your balance is $"<<balance;
}
}
float balance;
private:
int pin_number;
int account_number;
};
Don't hesitate to ask if you need more help.

[corrections always welcome.]
--
Bob R
POVrookie
Jun 29 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
8702
by: David Carter-Hitchin | last post by:
Hi, I'm not very experienced with c++ so I'm sure this is something obvious that is easily solved, but for the life of me I can't seem to figure it out. I'd be very grateful for some knowledgeable insight into this. My class Position has a string (private, public - doesn't seem to matter which), which I construct in a member function and wish to return calls from main(). However, although
2
3284
by: Javier | last post by:
Hi All I'm in a nightmare trying to do a simple c++ program... I want to read a file that will have strings in a vector. The path where is the file is in a ini file. I did:
22
13335
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; char c; std::string str;
13
11026
by: M | last post by:
Hi, I've searched through the previous posts and there seems to be a few examples of search and replacing all occurrances of a string with another string. I would have thought that the code below would work... string gsub(const string & sData, const string & sFrom,
9
7135
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but when I tried the program on the Win98 system it will be running on, I get the following error: Cast from string "2076719" to type 'Long' is not valid I am not sure why I only get this error on the Win98 system or how to go about correcting...
1
3757
by: Maxwell | last post by:
Hello, I having having oodles of trouble using the std lib in my MC++ (VS.NET 2003) Class library. I figured out a simple sample to reproduce the errors I am having. Create a MC++ (VS.NET 2003) class library and type in the following code below: #include <map> #include<string>
5
7248
by: Martin Jørgensen | last post by:
Hello again, Sorry to bother but I guess my C++ book isn't very good since it obviously contains errors so the program code doesn't work with g++. However I don't understand what the problem is. Last time the problem was that "using namespace std" apparently had a "link" class/object defined already. Now I get: error: no matching function for call to 'String::String(String)'
8
1713
by: Jose Cintron | last post by:
Hello all. Newbie question here... I have a program that needs to do 1 of 2 things 1. declare a global System::String (which I think can't be done, because VS 2005 complains) 2. modify the System::String in one function An example may be easier to understand... The question is how would I get the second option to work???
9
2099
by: jerry.upstatenyguy | last post by:
I am really stuck on this. I am trying to write a string array containing a "word" and a "definition" to a class called Entry. Ultimately this will end up in another class called dictionary. No, this is not a homework assignment. In fact it was a question on my class midterm that everyone bombed. Unfortunately we never covered this in class prior to the midterm. Anyway, please help if you would be so kind. Here is what I have. I...
0
2789
by: =?Utf-8?B?Y2luZHk=?= | last post by:
I know I wrote before a week ago when I knew even less than now but I am getting better please anyone give me a clue or an example. Am I completely off track? I have a datarow in a table with the fields to map to an xsd. Then i call a web service upload method that wants the xml strongly typed to the xsd in a string. so I have generated a class from a xsd newcust.cs in .net 1.1 c# to start with just to see what it means to use an...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10364
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.