Hi, I am having some problems with a program I'm supposed to write: here is the program I am supposed to write:

Write a modular program that uses a structure to store the following information about a customer account:

                                Name
                                Address

                                City, state, and ZIP

                                Telephone number

                                Account Balance

                                Date of last payment

The program should declare a structure variable. It should let the user enter information into the variable, change the contents of its members, and display all information stored in the structure. The program should have a menu-driven user interface.

Input Validation: When the information is entered, be sure the user enters data for all the fields. No negative account balances should be entered.

I've been trying to write the program with no success , I think I'm not calling the account structure correctly (account information)

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

struct account
{
    string name;                                // Customer name
    int address;                                // Address
    int location;                               // City, state and zip.
    int phone;                                  // Phone number
    float balance;                              // Account balance
    int lastPayment;                            // last payment date

};

int main()
{
    account information;                      // information is an account structure

    // Get account information
    cout << "Enter the name of the account holder: ";
    cin  >> information.name;
    cout << "Enter the address: ";
    cin.ignore();
    getline(cin, information.address);
    cout << "Enter your City, State, and Zip: ";
    cin.ignore();
    getline(cin, information.location);
    cout << "Enter phone number: ";
    cin >> information.phone;
    cout << "Enter your account balance: ";
    cin << information.balance;
    cout << "When was your last payment date: ";
    cin >> information.lastPayment;

    // Display results

    cout << "\n The customers account information:\n";
    cout << "Name: " << information.name << endl;
    cout << "address: " << information.address << endl;
    cout << "City, State, Zip: " << information.location << endl;
    cout << "phone number: " << information.phone << endl;
    cout << "remaining balance: "<< information.balance << endl;
    cout << "Date of last payment: " << information.lastPayment << endl;
    return 0;
}

I would really appreciate it if someone can help me fix it... thanks again for the help.

Greetings wangstarr,

You have minor syntactical errors with your program.

Firstly, the members within your class can cause issues. For example:

int address; // Address

If your address is "123 N. 7th Ave." How can that be an integer?

To simply change these to strings you can make them character arrays. For example:

struct account {
	char name[50];
	char address[50];
	char location[100];
	char phone[15];
	float balance;
	char lastPayment[25];
};

That should look alot different, and it is. All a character array is is an array of characters.

Secondly, cin >> is very unsafe for its reasons. It has no buffer overflow detection, and can crash your program if your character array is to short for the incoming stream. Instead of using cin >> lets use cin.getline():

cin.getline(information.name, 50);

istream& getline(char* s, streamsize n);
> Extracts characters from the stream and stores them into successive locations in the array pointed by s. Characters are extracted until either (n - 1) characters have been extracted, the delimiter (parameter delim or '\n' if not specified) is found, or if the end of file or any error occurs in the input sequence.

Once you change all of those to your corresponding values, there is one other issue. So far when you retrieve balance, your cin call is backwards:

cin << information.balance;

That should be >> instead.

If you have further questions, please feel free to ask.


- Stack Overflow

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

struct account {

    char name[50];
    char address[50];
    char location[100];
    char phone[15];
    float balance;
    char lastPayment[25];
};


int main()
{
    account information;                      // information is an account structure

    // Get account information
    cout << "Enter the name of the account holder: ";
    cin.getline(information.name, 50);
    cout << "Enter the address: ";
    cin.ignore();
    cin.getline(information.address, 50);
    cout << "Enter your City, State, and Zip: ";
    cin.getline(information.location, 100);
    cout << "Enter phone number: ";
    cin.getline(information.phone, 15);
    cout << "Enter your account balance: ";
    cin >> information.balance;
    cout << "When was your last payment date: ";
    cin.getline(information.lastPayment, 25);

    // Display results

    cout << "\n The customers account information:\n";
    cout << "Name: " << information.name << endl;
    cout << "address: " << information.address << endl;
    cout << "City, State, Zip: " << information.location << endl;
    cout << "phone number: " << information.phone << endl;
    cout << "remaining balance: "<< information.balance << endl;
    cout << "Date of last payment: " << information.lastPayment << endl;
    return 0;
}

Thanks for the help... This indeed worked so much better. One question; what can I do so that No negative balances should be entered? would a statement before cin << information.balance such as

if (information.balance < 0)
  cout << "please enter a positive balance" ;

would that work?

Thanks again

Greetings,

> what can I do so that No negative balances should be entered?
Well, this is a simple task, and your code shown should indeed work. Though, you would call on this after the "cin >>". Why? Because so far information.balance has not been set until the user enters the value:

// Get value from user
cin >> information.balance;
// Time to check...
if (information.balance < 0)
	// Do whatever

That there should work.


Hope this helps,
- Stack Overflow

Hi, thanks for all the insights... I tried modifying the code but there might be some error in my if/else if statement because whether i enter a positive or negative account balance, my current program does not allow me to ask date. Also, how can I start this program with a menu driven interface? like a simple 1. enter new customer account , 2. display account, 3. exit.

I am assuming i need to use switch statements but I am so confused right now.... thanks for any inputs..

Glad to be of assistance,

> my current program does not allow me to ask date.
After you call "cin >>" some programs don't respond exactly to wait for the next input. Usually you have to call cin.get() after your "cin >>" call and before your next "cin.getline()".

I'm not quite familiar of what your new code looks like, so I'm not sure if this is the problem or nor; it's just an educated guess.

The switch statement
The switch statement is a multi-way decision that tests whether an expression matches one of a number or constant integer values, and branches accordingly:

switch (expression) {
	case const-expr:	statements
	case const-expr:	statements
	default:	statements
}

Each case is labeled by one or more integer-valued constant expressions. If a case matches the expression value, execution starts at that case. All case expressions must be different. The case labeled default is executed if none of the other cases are satisfied. A default is optional; if it isn't there and if none of the cases match, no action at all takes place. Cases and the default clause can occur in any order.

The break statement causes an immediate exit from the switch. Because cases serve just a labels, after the code or one case is done, execution falls through to the next unless you take explicit action to escape. break and return are the most common ways to leave a switch. A break statement can also be used to force an immediate exit from while, for, and do loops.

An example of a switch statement can be used as the following:

#include <iostream>
using namespace std;

int main() {
	char ch;
	ch = cin.get(); // get inputted character

	switch (ch) {
		case 'h':
			cout << "Hi" << endl;
		break;
		case 'b':
			cout << "Bye" << endl;
		break;
		default:
			cout << "You entered " << ch << endl;
	}

	return 0;
}

It is quite simple overall. If you want to create a menu, you can use the cout system. For example you could do:

int input;

cout << "1. Do this" << endl;
cout << "2. Do that" << endl;

cin >> input;

switch (input) { ...

If you have further questions, please feel free to ask.


- Stack Overflow

Hi Stack,

Thanks again for the tips. Sorry I forgot to include my code. This is what I have so far.

// This program allows a user to enter information and displays the information
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

struct account {

    char name[50];
    char address[50];
    char location[100];
    char phone[15];
    float balance;
    char lastPayment[25];
};


int main()
{
    int input;

cout << "1. Enter Customer information" << endl;
cout << "2. Exit" << endl;

cin >> input;

switch (input) { 

    account information;                      // information is an account structure

    // Get account information
    cout << "Enter the name of the account holder: ";
    cin.getline(information.name, 50);
    cout << "Enter the address: ";
    cin.ignore();
    cin.getline(information.address, 50);
    cout << "Enter your City, State, and Zip: ";
    cin.getline(information.location, 100);
    cout << "Enter phone number: ";
    cin.getline(information.phone, 15);
    cout << "Enter your account balance: ";
    cin >> information.balance;
    if (information.balance < 0)
        cout << "please enter a positive balance: ";
    else if (information.balance > 0)
    cout << "When was your last payment date: ";
    cin.get();
    cin.getline(information.lastPayment, 25);

    // Display results

    cout << "The customers account information:" << endl;
    cout << "Name: " << information.name << endl;
    cout << "address: " << information.address << endl;
    cout << "City, State, Zip: " << information.location << endl;
    cout << "phone number: " << information.phone << endl;
    cout << "remaining balance: "<< information.balance << endl;
    cout << "Date of last payment: " << information.lastPayment << endl;
    return 0;
}
}

I am getting the following errors which leads me to believe that I didn't code the "menu interface" correctly. Another problem is with my "else if" line. When I enter a negative balance it asks me to enter a positive balance so for that part it is right, however, after I enter a positive balance it does not proceed to ask me to enter the last payment date and instead it just displays "last payment date with all the other info". I hope I am making sense to you. Please excuse the newbie language as I am so new to this whole c++ coding. Thanks again for your support.

Greetings,

Sorry for not being clear when explaining what my three dots meant in my example. I meant to continue on, like to code something to this similar event:

switch (input) {
	case '1':
		// Do something if 1
	break;
	case '2':
		// Do something if 2
	break;
	default:
		// Say something if not 1 or 2
}

Sorry for any confusion.


- Stack Overflow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.