GIDForums

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Jul-2005, 07:36
redthestudent redthestudent is offline
New Member
 
Join Date: Jul 2005
Posts: 10
redthestudent is an unknown quantity at this point

[C++]Structures and Classes


Machine Problem 3 (MP3.cpp)
Budgets

Write a program that maintains a Household budget and it has 3 budget items: food, rent, and cell phone.

The following struct identifies what is in each record:

CPP / C++ / C Code:
struct BudgetItem {
	char name[20];
	double amount;
	double used;
	date lastExpense;
};

The date struct looks like this:

struct Date {
	int month;
	int day;
	int year;
};

Write a program that allows the user to review a budget item, add a new item, record an expense, change the budgeted amount of an item, print a report, or quit.

The name of the budget is to be used in your output labels.

When the user requests to review a budget item, display the list of budget items and allow the user to select which item they want to review. Once the user makes their selection, simply print the information about that budget item to the screen. Include the name, amount allocated, amount used, date of last expense, and amount remaining.

When the user wants to add a budget item, gather the name and amount budgeted from the user. Set the amount used to 0 and the date to 0 0, 0 and add the record at the end of the record. Also add the name to the list of names you are storing in the program. There is a maximum of 100 budget items. If the user wants to add another item when there are already 100, print a message and do not allow it to be added.

To record an expense, the user needs to select which item the expense is for. Then the user needs to enter the amount of the expense. If the new expense would put the total used over the budgeted amount for that item, ask the user if they really want to add the expense. If they indicate they want to add the expense even though it puts them over budget, go ahead and add it. If they decide not to add it, make no change. To add the expense, increase the amount used and get the date of the expense. Validate that the month is between1-12, the day is a valid day for that month (you do not need to worry about leap year. For month 2 simply validate that the day is from 1-28.) Make sure the year is in this century. After the input has been validated, the program is to update that individual record.

When the user requests to change the budgeted amount, display the list of budget items and allow the user to select which item they want to change. Display the information about this item and let the user enter the new budget amount.

When the user wants a report printed display a table with labels at the top and a row for each budget item. Include the name, amount budgeted, amount used, and amount remaining for each item. Remember to only store one item in the program at a time.

When the user selects quit, shut down the program.
Last edited by LuciWiz : 27-Jul-2005 at 07:52. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 27-Jul-2005, 10:41
QED's Avatar
QED QED is offline
Member
 
Join Date: Feb 2005
Location: Hudson Valley, NY
Posts: 231
QED is a jewel in the roughQED is a jewel in the roughQED is a jewel in the rough
Are you giving us homework?!? Sorry, but I have more important things to do than a homework assignment that isn't mine. I can only imagine how nice it would be to visit this forum and never see these kinds of posts...

Matthew
  #3  
Old 27-Jul-2005, 13:05
alcedo's Avatar
alcedo alcedo is offline
Member
 
Join Date: Jul 2005
Location: Singapore
Posts: 198
alcedo will become famous soon enough
Hello,

Please insert your C++ code between [.c++] & [./c++] tags.

Hope u seen that edited reason tag at the bottom do this before entering your code:

[.c] codes to be places here... [./c] w/o the full stop...

once again post what u have done. at least e basic stuff. ppl normally dont mind to edit codes and add in a few functions totally free for you, but not the entire program
__________________
People should read the rules and regulation before posting!

The Best is yet to be...
  #4  
Old 28-Jul-2005, 08:13
redthestudent redthestudent is offline
New Member
 
Join Date: Jul 2005
Posts: 10
redthestudent is an unknown quantity at this point

mind ur own business matther


sory I wasn't able to post what I had done coz it took me a whole day to figure this machine problem here's what I have accomplish so far..

CPP / C++ / C Code:
#include<iostream>
#include<string>
	using namespace std;
struct Date {
	int month;
	int day;
	int year;
};
struct BudgetItem {
	string name;
	double amount;
	double used;
	Date lastExpense;
};
int main()
{
	string c;
	int x=3, i, z=3;
	double temp;
	BudgetItem item[100]; item[0].name="food"; item[1].name="rent"; item[2].name="CellPhone";
	item[0].amount=0; item[1].amount=0; item[2].amount=0; item[0].used=0; item[1].used=0;
	item[2].used=0;
	cout<<"(R)eview a budget item\n(A)dd a new item\n(RE)cord an expense\n";
	cout<<"(C)hange the budgeted amount of an item\n(P)rint a report\n(quit)...";
	while(true)
		{
		cout<<endl; cin>>c; 
		if(c=="quit"||c=="QUIT") // program terminator
			return false;
		else if(c=="R"||c=="r")  // Review a budget item
			{ cout<<endl; for(i=0; i<x; i++) { cout<<item[i].name<<"\n"; }
			cout<<endl<<"Select and enter: "; cin>>c; cout<<endl;
			for(i=0; i<x; i++) { if(c==item[i].name) { cout<<item[i].name;
			cout<<endl<<item[i].amount<<endl<<item[i].used;} } }
		else if(c=="A"||c=="a") // Add a new item
			{ for(i=x; i<100; i++) { cout<<endl<<"enter name: "; cin>>item[i].name;
			cout<<"enter amount: "; cin>>item[i].amount; item[i].used=0;
			item[i].lastExpense.day=0; item[i].lastExpense.month=0; 
			item[i].lastExpense.year=0; x++; cout<<endl<<"Do you want to add more items?(Y/N): ";
			cin>>c; if(c=="n"||c=="N") { break; } } }
		else if(c=="RE"||c=="re") // Record an expense
			{ cout<<endl; for(i=0; i<x; i++) { cout<<item[i].name<<"\n"; }
			cout<<endl<<"Select and enter: "; cin>>c; cout<<endl;
			//Under construction
			}
		else if(c=="C"||c=="c") // Change the budgeted amount of an item
			{ cout<<endl; for(i=0; i<x; i++) { cout<<item[i].name<<"\n"; }
			cout<<endl<<"Select and enter: "; cin>>c; cout<<endl;
			cout<<"Enter the budget amount: "; cin>>temp;
			for(i=0; i<x; i++) { if(c==item[i].name) { item[i].amount=temp; } } }
		else if(c=="P"||c=="p") // Print a report
			{
			//under construction
			}
		else
		{ cout<<endl<<"Invalid command"; }

	}
	return 0;
}

did I do this right? P.S. I would appreciate if only people who are interested to program view my post to critics, please bear with me as I said Im only a beginner If you still couldn't contain yourselves go find some other program to criticize get my drift?
Last edited by LuciWiz : 30-Jul-2005 at 07:29. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #5  
Old 28-Jul-2005, 14:31
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,230
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by redthestudent
P.S. I would appreciate if only people who are interested to program view my post to critics, please bear with me as I said Im only a beginner If you still couldn't contain yourselves go find some other program to criticize get my drift?
Well, we would appreciate you using code tags as requested 7 times and read the guidelines as requested at least twice. We have no problem "bearing with you" as a beginner, that's what we're here for, but if you can't be curteous and do as requested, most of us will simply ignore your requests.

As for the note to your critics, I find this a rude request since you can't be bothered to do as requested. Code tags help us read your code -- it's unreadable as is. The Guidelines help you ask good questions... Get my drift?

Please read the guidelines and refrain from sniping at us.
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
  #6  
Old 28-Jul-2005, 20:03
alcedo's Avatar
alcedo alcedo is offline
Member
 
Join Date: Jul 2005
Location: Singapore
Posts: 198
alcedo will become famous soon enough
Hmm maybe he doesnt know how to include those tags ?

Red, can you type -->[.c] (without the dot) and [./c] (without e dot) first before inserting your code ? so it woud be like:

[.c]
#include <iostream>
//your code here.
[./c]

it would change your code into something more readable and colorful
__________________
People should read the rules and regulation before posting!

The Best is yet to be...
  #7  
Old 29-Jul-2005, 06:23
redthestudent redthestudent is offline
New Member
 
Join Date: Jul 2005
Posts: 10
redthestudent is an unknown quantity at this point

sorry I'll always remember the format from now on


CPP / C++ / C Code:
#include<iostream>
#include<string>
using namespace std;
struct Date {
int month;
int day;
int year;
};
struct BudgetItem {
string name;
double amount;
double used;
Date lastExpense;
};
int main()
{
string c;
int x=3, i, z=3;
double temp;
BudgetItem item[100]; item[0].name="food"; item[1].name="rent"; item[2].name="CellPhone";
item[0].amount=0; item[1].amount=0; item[2].amount=0; item[0].used=0; item[1].used=0;
item[2].used=0;
cout<<"(R)eview a budget item\n(A)dd a new item\n(RE)cord an expense\n";
cout<<"(C)hange the budgeted amount of an item\n(P)rint a report\n(quit)...";
while(true)
{
cout<<endl; cin>>c; 
if(c=="quit"||c=="QUIT") // program terminator
return false;
else if(c=="R"||c=="r") // Review a budget item
{ cout<<endl; for(i=0; i<x; i++) { cout<<item[i].name<<"\n"; }
cout<<endl<<"Select and enter: "; cin>>c; cout<<endl;
for(i=0; i<x; i++) { if(c==item[i].name) { cout<<item[i].name;
cout<<endl<<item[i].amount<<endl<<item[i].used;} } }
else if(c=="A"||c=="a") // Add a new item
{ for(i=x; i<100; i++) { cout<<endl<<"enter name: "; cin>>item[i].name;
cout<<"enter amount: "; cin>>item[i].amount; item[i].used=0;
item[i].lastExpense.day=0; item[i].lastExpense.month=0; 
item[i].lastExpense.year=0; x++; cout<<endl<<"Do you want to add more items?(Y/N): ";
cin>>c; if(c=="n"||c=="N") { break; } } }
else if(c=="RE"||c=="re") // Record an expense
{ cout<<endl; for(i=0; i<x; i++) { cout<<item[i].name<<"\n"; }
cout<<endl<<"Select and enter: "; cin>>c; cout<<endl;
//Under construction
}
else if(c=="C"||c=="c") // Change the budgeted amount of an item
{ cout<<endl; for(i=0; i<x; i++) { cout<<item[i].name<<"\n"; }
cout<<endl<<"Select and enter: "; cin>>c; cout<<endl;
cout<<"Enter the budget amount: "; cin>>temp;
for(i=0; i<x; i++) { if(c==item[i].name) { item[i].amount=temp; } } }
else if(c=="P"||c=="p") // Print a report
{
//under construction
}
else
{ cout<<endl<<"Invalid command"; }

}
return 0;
}

I was not talking about you people who are helping me it's just that you have some members who are rude, this forum is created in order to help people who have problems in creating programs in C++ is it not?
  #8  
Old 29-Jul-2005, 06:37
alcedo's Avatar
alcedo alcedo is offline
Member
 
Join Date: Jul 2005
Location: Singapore
Posts: 198
alcedo will become famous soon enough
at least thats better, but what problems are you having with that program?

To kick start things, i would say that you used too much if else statements. i suggest u use a switch statement to make your program easier to managed, and less bugs.

eg:

CPP / C++ / C Code:
//With loads of white spacing, so that code is readable
//White spaces are one of the most impt part of programming 
//for it to be readable
char choice[2];
cin.getline(choice, 2 , '\n');

//takes in only the first character.
switch(tolower(choice[0])){

case 'r':

   break;
case 'a': 
   
   break;
default:
   cerr<<"Pls enter correct input in the given range"<<endl;
   break;
}
__________________
People should read the rules and regulation before posting!

The Best is yet to be...
  #9  
Old 29-Jul-2005, 08:42
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,102
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Red, you still need to deal with your code formatting style a bit. This can be a personal topic and you just kind of need to find one that suits you. Then stick to it. When you look at Acedo's snip of code don't you find it easier to follow?

Consider the two versions of your code here,

CPP / C++ / C Code:
// AS POSTED
else if(c=="R"||c=="r") // Review a budget item
{ cout<<endl; for(i=0; i<x; i++) { cout<<item[i].name<<"\n"; }
cout<<endl<<"Select and enter: "; cin>>c; cout<<endl;
for(i=0; i<x; i++) { if(c==item[i].name) { cout<<item[i].name;
cout<<endl<<item[i].amount<<endl<<item[i].used;} } }

This makes it very difficult for someone to follow your logic (and find silly typos) then something along the lines of,

CPP / C++ / C Code:
// Same code, formatted differently
else if(c=="R"||c=="r") { // Review a budget item
  cout<<endl; 

  for(i=0; i<x; i++) {
    cout<<item[i].name<<"\n";
  }
  cout<<endl<<"Select and enter: ";
  cin>>c;
  cout<<endl;

  for(i=0; i<x; i++) { 
    if(c==item[i].name) {
      cout<<item[i].name;
      cout<<endl<<item[i].amount<<endl<<item[i].used;
    }
  }
}

Now when I look at it, I get a better sense of what is happening. That is the style like to use, many others like to do something like,

CPP / C++ / C Code:
// Same code, formatted differently
else if(c=="R"||c=="r")  // Review a budget item
{
  cout<<endl; 

  for(i=0; i<x; i++) 
  {
    cout<<item[i].name<<"\n";
  }
  cout<<endl<<"Select and enter: ";
  cin>>c;
  cout<<endl;

  for(i=0; i<x; i++) 
  { 
    if(c==item[i].name) 
    {
      cout<<item[i].name;
      cout<<endl<<item[i].amount<<endl<<item[i].used;
    }
  }
}

They accomplish the same thing though. You can find some opinions on the matter in this thread and this thread here at GIDForums™ or many, many other places using a Google search. The best thing though, just find something readable that you are comfortable with and stick with it. Troubleshooting will become infinitely easier.

Your code compiles giving me only the warning,
Code:
$ g++ -Wall red_the_student_01.cpp -s -o RedTheStudent red_the_student_01.cpp: In function `int main()': red_the_student_01.cpp:21: warning: unused variable 'z'

In the (formatted) code it refers to this line,
CPP / C++ / C Code:
int main()
{
  string c;
  int x=3, i, z=3;  // this is line 21
  double temp;

When I run it I find it very difficult to tell just what you want me to enter. You need to clarify your inputs a bit.

As for this last bit,
Quote:
Originally Posted by redthestudent
I was not talking about you people who are helping me it's just that you have some members who are rude, this forum is created in order to help people who have problems in creating programs in C++ is it not?

Yes it is for learning. Yes, you might perceive some people to be rude. What you have to take into consideration is that,
  • People volunteer to help
  • Lots of people just read and don't comment
  • Many won't help if you don't help yourself
  • We have a simple set of easy to follow rules to make the rest possible.

If you think this is rude, well, I don't know what to say. Try out some of the forums with members that come in with a flamethrower all ready to go. I don't think that is the case here. If you ask for help and follow some very simple rules we have, everyone (not just you) can benefit.

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #10  
Old 29-Jul-2005, 08:49
alcedo's Avatar
alcedo alcedo is offline
Member
 
Join Date: Jul 2005
Location: Singapore
Posts: 198
alcedo will become famous soon enough
Yeap,
Quote:
int x=3, i, z=3; // this is line 21

like for that, line 21 refers to 3 different thing, isit x or i or z that is giving the problems. same goes for other bugs that could be found oso when pointed out by the debugger. easier if there is only a complete sentence for each line of code.
__________________
People should read the rules and regulation before posting!

The Best is yet to be...
 

Recent GIDBlogNew Corolla Altis, 10th Generation - Part I by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Assistance with classes... Bravebird CPP / C++ Forum 7 27-Apr-2005 13:17
Help using const arrays in c++ classes FlipNode CPP / C++ Forum 5 31-Mar-2005 01:16
Fairly simple classes help please sammacs CPP / C++ Forum 0 30-Nov-2004 09:58
Using map<> with classes crystalattice CPP / C++ Forum 3 14-Nov-2004 16:23
ClassView not showing my classes (VC++, namespace, headers) ?!?!? djovanov CPP / C++ Forum 1 13-Jan-2004 04:54

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:35.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.