Problems in function assignment

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 2471 Views - Last Post: 15 October 2012 - 09:39 PM Rate Topic: -----

#1 Mr2012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 14-October 12

Problems in function assignment

Posted 14 October 2012 - 01:30 PM

Hello. I am having trouble with a lab assignment. My instructor has asked us to create a program that displays an account owners balance that is due each month for 10 months. Here is the assignment problem:

The No Interest Credit Company provides zero-interest loans to customers. (It makes a profit by selling advertising space in its monthly statements and selling its customer lists.) Design an application that gets customer account data that includes an account number, customer name, and balance due. For each customer, display the account number and name; then print the customerís projected balance each month for the next 10 months. Assume that there is no finance charge on this account, that the customer makes no new purchases, and that the customer pays off the balance with equal monthly payments, which are 10 percent of the original bill.
Name the file as lab6a.

You are required to implement the following function:

void dipslayBalance(int account_number, char name[ ], float balance)
The function displays the account number and name; then print the customerís projected balance each month for the next 10 months


Sample output

Enter Account Number (-1 to terminate the input):
123
Enter name: John Nguyen
Enter balance due:
10000.00

Account Number: 123
Name: John Nguyen

MONTH BALANCE DUE
1 9000.00
2 8000.00
3 7000.00
4 6000.00
5 5000.00
6 4000.00
7 3000.00
8 2000.00
9 1000.00
10 0.00

Enter Account Number (-1 to terminate the input):
456
Enter name: Jose Hernandez
Enter balance due:
25000.00

Account Number: 456
Name: Jose Hernandez

MONTH BALANCE DUE
1 22500.00
2 20000.00
3 17500.00
4 15000.00
5 12500.00
6 10000.00
7 7500.00
8 5000.00
9 2500.00
10 0.00

Enter Account Number (-1 to terminate the input):
-1
Thank you.


My code

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

void displayBalance(int, char, float);

int main()
{
	int accntNum;			//Variable account number.
	char name;		        //Name of account owner.
	float bal;			//Balance owner of account owes each month.
	double balanceDue;	
	
	cout << "Enter Account Number (-1 to terminate the input):\n";
	cin >> accntNum;
	
		if(accntNum==-1)
		cout << "Thank you.\n";
		else
		
	{
	cout << "Enter name:";
	cin >> name;
	cout << "Enter balance due:\n";
	cin >> bal;
	
	displayBalance(accntNum, name, bal);
	
	bal=bal/10;
	balanceDue=bal-balanceDue;
	
	}
	
	return 0;
}

	void displayBalance(int accntNum, char name, float bal)
	{
		cout << "Account Number: " << accntNum << endl;
		cout << "Name: " << name << endl;
		
		cout << left << setw(12) << "\nMONTH" << right << "BALANCE DUE" << setw(4) << setprecision(2) << fixed << endl;
		cout << left << setw(12) << "1" << right << bal << setw(4) << endl;
		cout << left << setw(12) << "2" << right << bal << setw(4) << endl;
		cout << left << setw(12) << "3" << right << bal << setw(4) << endl;
		cout << left << setw(12) << "4" << right << bal << setw(4) << endl;
		cout << left << setw(12) << "5" << right << bal << setw(4) << endl;
		cout << left << setw(12) << "6" << right << bal << setw(4) << endl;
		cout << left << setw(12) << "7" << right << bal << setw(4) << endl;
		cout << left << setw(12) << "8" << right << bal << setw(4) << endl;
		cout << left << setw(12) << "9" << right << bal << setw(4) << endl;
		cout << left << setw(12) << "10" << right << bal << setw(4) << endl;
	}

Questions:

1. Calculation. Every time I do the calculation noted in my code, I always get 10000.00 in my output for "bal." What am I doing wrong? Is the "double balanceDue" variable neccessary?

2. Every time I enter a name (i.e., John Nguyen), I always get "J" when the output displays instead of the full name. This is my first assignment and time using the "char" datatype, so what I am doing wrong?

Any help is appreciated. I don't expect anyone to do my homework for me, but rather to try to give me pieces to the puzzle.

Is This A Good Question/Topic? 0
  • +

Replies To: Problems in function assignment

#2 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Problems in function assignment

Posted 14 October 2012 - 02:07 PM

char name;              //Name of account owner.

See, character sequences1 are char arrays, so the type would be char[] or char*, not char. The latter keeps only one character, and usually that's not going to be enough to keep a name in it. (Btw, your assignment requires you to use c-strings, and you should learn about them, certainly. In real life though, you usually don't want to dabble with them all that much. Get to know std::string2, rather sooner than later. ;) )

Now, for the bal variable and displayBalance(), well, you need to get to know loops3, immediately. ;) Then, apply your new gained knowledge in the form of a for-loop, and all will be simpler and shorter at the same time.
______
1) http://www.cplusplus.../tutorial/ntcs/
2) http://www.cplusplus...ference/string/
3) http://www.cplusplus...torial/control/ (search for "loops")

This post has been edited by Xupicor: 14 October 2012 - 02:13 PM

Was This Post Helpful? 0
  • +
  • -

#3 Mr2012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 14-October 12

Re: Problems in function assignment

Posted 14 October 2012 - 03:17 PM

Hello. I actually do know about loops, haha, but didn't think they would be needed for this problem. I'm gonna try out using the for loop for my assignment.

As for the "char" datatype, that reference link is a little confusing. But, am I suppose to include a number in between the brackets char[]? Or, should I turn the

cout << "Enter the account number:" into a char?

Sorry, again, this is my first time using the char datatype, as well as working on an assignment using functions.
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Problems in function assignment

Posted 14 October 2012 - 03:25 PM

Quote

am I suppose to include a number in between the brackets char[]

The number between the brackets in the char declaration determines how many characters (1 byte data type) that you would like to reserve for that variable. Reserve enough memory that you believe is suitable for your variable.

Note: There are exceptions when you "don't" need to give the number of bytes you would like to allocate, but that is when dealing with string constants that are placed in the string table.

This post has been edited by jjl: 14 October 2012 - 03:26 PM

Was This Post Helpful? 0
  • +
  • -

#5 Mr2012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 14-October 12

Re: Problems in function assignment

Posted 14 October 2012 - 04:02 PM

jjl said:

The number between the brackets in the char declaration determines how many characters (1 byte data type) that you would like to reserve for that variable. Reserve enough memory that you believe is suitable for your variable.

Note: There are exceptions when you "don't" need to give the number of bytes you would like to allocate, but that is when dealing with string constants that are placed in the string table.


Understandable. Thank you.

I added 16 to the char datatype, but I am getting two errors. I don't understand.

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

void displayBalance(int, char[], float);

int main()
{
	int accntNum;	//Variable account number.
	char name[16];	//Name of account owner.
	float bal;		//Balance owner of account owes each month.
	double balDue;	//Variable balanceDue.
		
	cout << "Enter Account Number (-1 to terminate the input):\n";
	cin >> accntNum;
	
		if(accntNum==-1)
		cout << "Thank you.\n";
		else
	{
	cout << "Enter name:";
	cin >> name;
	cout << "Enter balance due:\n";
	cin >> bal;
	
	displayBalance(accntNum, name[16], bal);
	}
	return 0;
}

	void displayBalance(int accntNum, char name[], float bal)
{
		cout << "Account Number: " << accntNum << endl;
		cout << "Name: " << name << endl;
		
		cout << left << setw(12) << "\nMONTH" << right << "BALANCE DUE" << setw(4) << setprecision(2) << fixed << endl;
		cout << left << setw(12) << "1" << right << bal << setw(4) << endl;
		cout << left << setw(12) << "2" << right << bal << setw(4) << endl;
		cout << left << setw(12) << "3" << right << bal << setw(4) << endl;
		cout << left << setw(12) << "4" << right << bal << setw(4) << endl;
		cout << left << setw(12) << "5" << right << bal << setw(4) << endl;
		cout << left << setw(12) << "6" << right << bal << setw(4) << endl;
		cout << left << setw(12) << "7" << right << bal << setw(4) << endl;
		cout << left << setw(12) << "8" << right << bal << setw(4) << endl;
		cout << left << setw(12) << "9" << right << bal << setw(4) << endl;
		cout << left << setw(12) << "10" << right << bal << setw(4) << endl;
		
	}


The errors are:
error: invalid conversion from 'char' to 'char*' [-fpermissive]
error: initializing argument 2 of 'void displayBalance(int, char*, float)' [-fpermissive]
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Problems in function assignment

Posted 14 October 2012 - 04:32 PM

Quote

initializing argument 2 of 'void displayBalance(int, char*, float)

You second argument accepts character pointer, however name[16] is only a character at index 16, not a pointer. To make matters worse, name[16] is outside of name's memory, since arrays start indexing at 0.

displayBalance(accntNum, name[16], bal);



You don't need to add the brackets when passing the character array. Change the function call to
displayBalance(accntNum, name, bal);



When you pass an array of data, you don't actually pass the entire chunk of memory to the function. You simply pass a pointer (the address) to the first element of the array. The next elements follow that address sequentially. Thinking of an array an address of the first element, rather than the entire chunk itself will be beneficial when learning pointers.

This post has been edited by jjl: 14 October 2012 - 04:35 PM

Was This Post Helpful? 2
  • +
  • -

#7 Mr2012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 14-October 12

Re: Problems in function assignment

Posted 14 October 2012 - 04:51 PM

Took out the brackets in the call function. That gets rid of the errors; now I can enter the name without it getting truncated. The only problem I have now is that I only get the first name entered, not the first and last name. Instead of "John Nguyen" or "Jose Hernandez," I only get "John" or "Jose" when it outputs instead of first and last.

However, when I don't space JohnNguyen, I get both the first and last name. Putting a space also skips the user entering the balance due.
Was This Post Helpful? 0
  • +
  • -

#8 Mr2012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 14-October 12

Re: Problems in function assignment

Posted 14 October 2012 - 06:23 PM

Sorry about double posting, but I cannot find the edit post tab.

I was in a rush while typing the last post (was in a library). Basically, when I enter "first name," space, "last name," my program skips the next step for the user entering in the "balance." for balance due. Also, in addition to the above, I only get the first name displayed, not first and last, as I should, in my output. What is wrong with my code?
Was This Post Helpful? 0
  • +
  • -

#9 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Problems in function assignment

Posted 14 October 2012 - 07:12 PM

std::cin will stop reading it's input at a white space, therefore skipping the last name. You need to use getline to read the entire line entered (including the space).

example
char name[50];
std::cout<<"Enter name";
std::cin.getline(name, 50); 



Edit: fixed getline

This post has been edited by jjl: 14 October 2012 - 07:41 PM

Was This Post Helpful? 0
  • +
  • -

#10 Mr2012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 14-October 12

Re: Problems in function assignment

Posted 14 October 2012 - 07:32 PM

Do I need to use "std::" instead of using "getline"? I'm getting a weird error now:

cout << "Enter name:";
	getline(name, 26);
	cout << "Enter balance due:\n";
	cin >> bal;


LAB 6 A.cpp:31:17: error: no matching function for call to 'getline(char [26], int)'
LAB 6 A.cpp:31:17: note: candidates are:
basic_string.tcc:1070:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&, _CharT)
basic_string.h:2734:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&)
Was This Post Helpful? 0
  • +
  • -

#11 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Problems in function assignment

Posted 14 October 2012 - 07:41 PM

My apologies, you need to use std::cin.getline. When given the luxury of using C++, I tend to use std::string rather an char arrays.

This post has been edited by jjl: 14 October 2012 - 07:42 PM

Was This Post Helpful? 1
  • +
  • -

#12 Mr2012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 14-October 12

Re: Problems in function assignment

Posted 14 October 2012 - 07:47 PM

That's okay. It's just for this assignment, my instructor wants us to use char. I would rather use string though, as it would make the the user input for name easier. Maybe I am missing something?
Was This Post Helpful? 0
  • +
  • -

#13 Mr2012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 14-October 12

Re: Problems in function assignment

Posted 14 October 2012 - 07:53 PM

I hate double posting... what does this error mean?

error: a function-definition is not allowed here before '{' token

I have been stuck on it for a good while.
Was This Post Helpful? 0
  • +
  • -

#14 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Problems in function assignment

Posted 14 October 2012 - 08:37 PM

post your current code
Was This Post Helpful? 0
  • +
  • -

#15 Mr2012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 14-October 12

Re: Problems in function assignment

Posted 14 October 2012 - 09:58 PM

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

void displayBalance(int, char[], float);

int main()
{
	int accntNum;	//Variable account number.
	char name[26];	//Name of account owner.
	float bal;		//Balance owner of account owes each month.
	int balDue;		//Variable balanceDue.
		for(bal=10000.00;bal<=balDue;bal-=1000.00)
		{
	cout << "Enter Account Number (-1 to terminate the input):\n";
	cin >> accntNum;
	
		if(accntNum==-1)
		cout << "Thank you.\n";
		else
	{
	cout << "Enter name:";
	cin >> name;
	cout << "Enter balance due:\n";
	cin >> bal;
	
	displayBalance(accntNum, name, bal);
	
	return 0;
}

	void displayBalance(int accntNum, char name[], float bal)
	{
		if(bal==10000.00)
		{
		bal=bal-1000.00
		cout << "Account Number: " << accntNum << endl;
		cout << "Name: " << name << endl;
		cout << left << setw(12) << "\nMONTH" << right << "BALANCE DUE" << setw(4) << setprecision(2) << fixed << endl;
		cout << left << setw(12) << "1" << right << bal << setw(4) << endl;
		cout << left << setw(12) << "2" << right << bal << setw(4) << endl;
		cout << left << setw(12) << "3" << right << bal << setw(4) << endl;
		cout << left << setw(12) << "4" << right << bal << setw(4) << endl;
		cout << left << setw(12) << "5" << right << bal << setw(4) << endl;
		cout << left << setw(12) << "6" << right << bal << setw(4) << endl;
		cout << left << setw(12) << "7" << right << bal << setw(4) << endl;
		cout << left << setw(12) << "8" << right << bal << setw(4) << endl;
		cout << left << setw(12) << "9" << right << bal << setw(4) << endl;
		cout << left << setw(12) << "10" << right << bal << setw(4) << endl;
		bal=bal-10000.00;
	}]

This post has been edited by GunnerInc: 16 October 2012 - 03:54 PM
Reason for edit:: Fixed broken code tags

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2