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 21-Oct-2004, 18:50
brookeville brookeville is offline
Junior Member
 
Join Date: Oct 2004
Posts: 56
brookeville is on a distinguished road
Question

Repetition structure problem and question


Greetings, I am a beginner in C++ and new to GIDForums, and was wondering if anyone could help me with a programming problem I'm having. Any help would be greatly appreciated!

My professor gave the following instructions:

Write a program to calculate daily expense and produce a daily general ledger for a user. Input from user would be store name or expense category and dollar amount such as:

Food Lion

85.67

Gasoline

23.89

The output should look like this:

Item Amount
--------- -------
Food Lion 85.67
Gasoline 23.89
Dry Clean 5.56
----------------------------------------------------------
TOTAL: $115.02

That is the question presented...

I have programmed this so far:

CPP / C++ / C Code:
#include <iostream>
#include <cctype>
#include <cmath>
#include <cstring> 
#include <cstdlib>
#include <iomanip>
using namespace std;

int main()
{

const int size = 20;

char amount[size];

char category[size];
int total = 0, count = 0;

cout << "This program calculates your daily expenses!" << endl;

while (tolower(amount[0]) != 'q')
{	
	
   	cout << "\nEnter the name of the store or category: " << endl << endl;
	cin.getline(category, size);

	cout << "\nEnter the amount of money spent as a decimal:\n " << endl;
	cin.getline(amount, size);


	
	
	total += (amount[0]);
	count ++;


}


return 0;
}


And of course, I have this output format, but I don't know where to enter it in the above program:

CPP / C++ / C Code:
cout <<	"\n\nItem                                         Amount\n";
    cout << "------------                                 ------\n";
	cout <<  category << "                                           " << amount << endl;
 

    cout << "---------------------------------------------------\n";
	cout << "TOTAL:                                           ";
	cout << showpoint << fixed << setprecision(2) << total << endl;

Basically, my question is how can I get this program to 'work'? :)

In specific, my first question is how can I retrieve what the user types in for category and amount variables, and output them in the format listed above? (i.e. How do I print out all user input after all input statements have been entered?)

Another big question I have is: How do I program my statement, while (tolower(amount[0]) != 'q') to quit when user enters in a CATEGORY rather than AMOUNT? How can it quit when it prompts to enter category, rather than amount? I can't seem to get that working...

Finally, the "total" variable is not working and outputs an incorrect amount. Can anyone help me fix this?

Again, any help is greatly appreciated! Thank you!
Last edited by LuciWiz : 22-Oct-2004 at 05:26. Reason: Inserting code tags...
  #2  
Old 22-Oct-2004, 00:31
brookeville brookeville is offline
Junior Member
 
Join Date: Oct 2004
Posts: 56
brookeville is on a distinguished road
Quote:
Originally Posted by brookeville
Greetings, I am a beginner in C++ and new to GIDForums, and was wondering if anyone could help me with a programming problem I'm having. Any help would be greatly appreciated!

My professor gave the following instructions:

Write a program to calculate daily expense and produce a daily general ledger for a user. Input from user would be store name or expense category and dollar amount such as:

Food Lion

85.67

Gasoline

23.89

The output should look like this:

Item Amount
--------- -------
Food Lion 85.67
Gasoline 23.89
Dry Clean 5.56
----------------------------------------------------------
TOTAL: $115.02

That is the question presented...

I have programmed this so far:

CPP / C++ / C Code:
#include <iostream>
#include <cctype>
#include <cmath>
#include <cstring> 
#include <cstdlib>
#include <iomanip>
using namespace std;

int main()
{

const int size = 20;

char amount[size];

char category[size];
int total = 0, count = 0;

cout << "This program calculates your daily expenses!" << endl;

while (tolower(amount[0]) != 'q')
{	
	
   	cout << "\nEnter the name of the store or category: " << endl << endl;
	cin.getline(category, size);

	cout << "\nEnter the amount of money spent as a decimal:\n " << endl;
	cin.getline(amount, size);


	
	
	total += (amount[0]);
	count ++;


}


return 0;
}


And of course, I have this output format, but I don't know where to enter it in the above program:

CPP / C++ / C Code:
cout <<	"\n\nItem                                         Amount\n";
    cout << "------------                                 ------\n";
	cout <<  category << "                                           " << amount << endl;
 

    cout << "---------------------------------------------------\n";
	cout << "TOTAL:                                           ";
	cout << showpoint << fixed << setprecision(2) << total << endl;

Basically, my question is how can I get this program to 'work'?

In specific, my first question is how can I retrieve what the user types in for category and amount variables, and output them in the format listed above? (i.e. How do I print out all user input after all input statements have been entered?)

Another big question I have is: How do I program my statement, while (tolower(amount[0]) != 'q') to quit when user enters in a CATEGORY rather than AMOUNT? How can it quit when it prompts to enter category, rather than amount? I can't seem to get that working...

Finally, the "total" variable is not working and outputs an incorrect amount. Can anyone help me fix this?

Again, any help is greatly appreciated! Thank you!

I apologize for not using the color coding technique in my earlier post. Since the thread no longer gives me the option to edit my post, I have quoted it and used the color coding technique to make it easier to read. Again, I am new to the forum and sorry for this inconvenience.
  #3  
Old 22-Oct-2004, 05:24
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 889
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by brookeville
I apologize for not using the color coding technique in my earlier post. Since the thread no longer gives me the option to edit my post, I have quoted it and used the color coding technique to make it easier to read. Again, I am new to the forum and sorry for this inconvenience.

That's OK, since you are new the forum it is understandable. I actually really appreciate you posting back with your formatted code - I will format your previous code as well.
Also, I am sorry I can't answer to your post just now - promiss to come back with a solution when I get the time, if nobody else does.

Kind regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #4  
Old 22-Oct-2004, 14:13
brookeville brookeville is offline
Junior Member
 
Join Date: Oct 2004
Posts: 56
brookeville is on a distinguished road
Quote:
Originally Posted by LuciWiz
That's OK, since you are new the forum it is understandable. I actually really appreciate you posting back with your formatted code - I will format your previous code as well.
Also, I am sorry I can't answer to your post just now - promiss to come back with a solution when I get the time, if nobody else does.

Kind regards,
Luci

Thanks for being so kind and sincere! :-)


So far, I have modified my program a little, but I still haven't gotten the results I want. Here is my modified program:

CPP / C++ / C Code:
//Purpose: To calculate daily expenses and produce a daily ledger for a person.
//         Calculates total amount of money spent on daily expenses. 

//input: Store name (i.e. Food Lion) or category of expense (i.e. gasoline) and
//       the amount of money spent on the expense.

//output: A list of the input entered, and how much money was spent on each item
//        entered by user. Also, the total amount spent for the day. 

//process: 

#include <iostream>
#include <cctype>
#include <cmath>
#include <cstring> 
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;

int main()
{

const int size = 20;
float amount;
string category;
float total = 0;
int count = 0;


	
cout << "This program calculates your daily expenses!" << endl;

while (amount != 'q' || 'Q')
{	
	
            cout << "\nEnter the name of the store or category: " << endl << endl;
	cin >> category;

	cout << "\nEnter the amount of money spent as a decimal:\n " << endl;
	cin >> amount;

	if (amount == 'q' || 'Q')

	{
	total = (total + amount);
	count ++;

	cout << "\n\nItem                                         Amount\n";
	cout << "------------                                 ------\n";
	cout << category << "                                            " << amount << endl;


	cout << "---------------------------------------------------\n";
	cout << "TOTAL:                                        ";
	cout << fixed << showpoint << setprecision(2) << total << endl;
	}
}



return 0;
}

Now, what I am having trouble with is it only accepts ONE category and ONE amount, and automatically displays the output without accepting any more input. I have corrected the 'total' variable, which is a plus. However, I would like for the program to repeat the two statements asking for category and amount, and to output all input entered instead. Again, any help with this program is really appreciated!

P.S. Leave the formatting to me (the setw() and other iomanip functions), I just need to know how to get the body of this program right!
  #5  
Old 22-Oct-2004, 17:58
small_ticket small_ticket is offline
Junior Member
 
Join Date: May 2004
Posts: 45
small_ticket is on a distinguished road
i am not over sure but i remember stg like that the line
CPP / C++ / C Code:
while (amount != 'q' || 'Q')
should be like
CPP / C++ / C Code:
while (amount != 'q' ||amount != 'Q')
and also the if line should be like this.
i hope this helps you...
  #6  
Old 22-Oct-2004, 19:33
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
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 small_ticket
i am not over sure ...

That's for sure

amount is defined as a float. therefore you cannot type in a 'Q', big or small. You might have better luck with 0. But because floating point is not an exact value, you might try
CPP / C++ / C Code:
while (amount < 0.001 && amount > -0.001)
so your program will exit when a nearly zero value is entered.
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
  #7  
Old 22-Oct-2004, 20:27
brookeville brookeville is offline
Junior Member
 
Join Date: Oct 2004
Posts: 56
brookeville is on a distinguished road
Quote:
Originally Posted by WaltP
That's for sure ;)

amount is defined as a float. therefore you cannot type in a 'Q', big or small. You might have better luck with 0. But because floating point is not an exact value, you might try
CPP / C++ / C Code:
while (amount < 0.001 && amount > -0.001)
so your program will exit when a nearly zero value is entered.

Thank you both very much for your time and help! Thanks for correcting me on the logical || (OR) operator. I forgot that it needed to be repeated on both sides. I still have some questions unfortunately...

I have decided to do something completely different in my program. Instead of the user quitting by entering 'q' or 'Q' in category or amount-- (not possible because it's a float), I have named another char variable, "quit." This will allow the user to keep entering in categories and amounts until the user presses 'N' or 'n.' I like this approach better because it allows for a switch point when the user has finished entering both categories and amounts.

However, I am still not achieving the desired output. How can I retrieve all user input (categories and amounts) and output them under the "Item / Amount" output format? For instance, if the user enters Food Lion, Gasoline, Dry Clean and perhaps numerous other categories and 50.00, 34.00, 5.00, and other amounts, how can I output all of these under the format I have at the end of my program? (Please keep in mind, it's not just three separate inputs, it can be infinitely many inputs, until the user presses 'n').

Here is my modified program:

CPP / C++ / C Code:
//Purpose: To calculate daily expenses and produce a daily ledger for a person.
//         Calculates total amount of money spent on daily expenses. 

//input: Store name (i.e. Food Lion) or category of expense (i.e. gasoline) and
//       the amount of money spent on the expense.

//output: A list of the input entered, and how much money was spent on each item
//        entered by user. Also, the total amount spent for the day. 

//process: 


#include <iostream>
#include <cctype>
#include <cmath>
#include <cstring> 
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;

int main()
{

float amount;
string category;
float total = 0;
int count = 0;
char quit;


cout << "This program calculates your daily expenses!" << endl;

while (quit != 'y' || quit != 'Y')
  {  
  
  cout << "\nEnter the name of the store or category: " << endl << endl;
  cin >> category;

  cout << "\nEnter the amount of money spent as a decimal:\n " << endl;
  cin >> amount;

  cout << "\nWould you like to enter in another category and amount? (Y or N) ";
  cin >> quit;

  if (quit == 'n' || quit == 'N') 

  {
  total = (total + amount);
  count ++;
 //this is the output format (below):
  cout << "\n\nItem                                         Amount\n";
  cout << "------------                                 ------\n";
  cout << category << "                                           " << amount << endl;


  cout << "---------------------------------------------------\n";
  cout << "TOTAL:                                        ";
  cout << fixed << showpoint << setprecision(2) << total << endl;
  }
}

return 0;
}

Again, I really appreciate your help!
  #8  
Old 23-Oct-2004, 06:41
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi brookeville. Have you learned about arrays yet? You need to place the input into an array for later retreival. For now using a static array would be the easiest, but you will need to place a "maximum" on the amount of items that can be entered.

I have modified your code, but haven't tested it..

CPP / C++ / C Code:
//Purpose: To calculate daily expenses and produce a daily ledger for a person.
//         Calculates total amount of money spent on daily expenses. 

//input: Store name (i.e. Food Lion) or category of expense (i.e. gasoline) and
//       the amount of money spent on the expense.

//output: A list of the input entered, and how much money was spent on each item
//        entered by user. Also, the total amount spent for the day. 

//process: 


#include <iostream>
#include <cctype>
#include <cmath>
#include <cstring> 
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;

#define MAX 100  //This way we can easily change the maximum value.
int main()
{

float amount[MAX];
string category[MAX];
float total = 0;
int count = 0;
int loop;
char quit;


cout << "This program calculates your daily expenses!" << endl;

while (quit != 'y' || quit != 'Y')
  {  
  
  cout << "\nEnter the name of the store or category: " << endl << endl;
  cin >> category[count];

  cout << "\nEnter the amount of money spent as a decimal:\n " << endl;
  cin >> amount[count];

  cout << "\nWould you like to enter in another category and amount? (Y or N) ";
  cin >> quit;
  count++;   //Index counter...
}

for(loop=0;loop<count;loop++){
  total = (total + amount[loop]);
 //this is the output format (below):
  cout << "\n\nItem                                         Amount\n";
  cout << "------------                                 ------\n";
  cout << category[loop << "                                           " << amount[loop] << endl;
}

  cout << "---------------------------------------------------\n";
  cout << "TOTAL:                                        ";
  cout << fixed << showpoint << setprecision(2) << total << endl;
}

return 0;
}

That should get you pointed in the right direction. You may want to add some limit checking if the user tries to enter more than 100 items.

HTH
  #9  
Old 23-Oct-2004, 14:25
brookeville brookeville is offline
Junior Member
 
Join Date: Oct 2004
Posts: 56
brookeville is on a distinguished road
Quote:
Originally Posted by dsmith
Hi brookeville. Have you learned about arrays yet? You need to place the input into an array for later retreival. For now using a static array would be the easiest, but you will need to place a "maximum" on the amount of items that can be entered.

I have modified your code, but haven't tested it..

That should get you pointed in the right direction. You may want to add some limit checking if the user tries to enter more than 100 items.

HTH

Hello and thank you very much for your help! My professor tought the basics of arrays, so I know a little about them... Before I attempted the program, I wrote out pseudocode basically stating that all user input should be printed out right after the input time. All should be in the loop body-- ask for category, ask for dollar amount, print out category entered, print out dollar amount, sum dollar amount. But getting that into code was somewhat difficult!

I have adjusted the modified program you showed me in the following way:

CPP / C++ / C Code:
#include <iostream>
#include <cctype>
#include <cmath>
#include <cstring> 
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;

#define MAX 100  //This way we can easily change the maximum value.
int main()
{

float amount[MAX];
string category[MAX];
float total = 0;
int count = 0;
int loop;
char quit;


cout << "This program calculates your daily expenses!" << endl;

while (quit != 'y' || quit != 'Y')
	{  
  
	cout << "\nEnter the name of the store or category: " << endl << endl;
	cin >> category[count];

	cout << "\nEnter the amount of money spent as a decimal:\n " << endl;
	cin >> amount[count];

	cout << "\nWould you like to enter in another category and amount? (Y or N) ";
	cin >> quit;
	count++;   //Index counter

	if (quit == 'n' || quit == 'N')
	break;
	}

cout << "\n\nItem                                         Amount\n";
cout << "------------                                 ------\n";
for(loop=0; loop < count;loop++)
{
  total = (total + amount[loop]);
 
  cout << category[loop] << "                                           " << amount[loop] << endl;


}
cout << "---------------------------------------------------\n";
cout << "TOTAL:                                        ";
  cout << fixed << showpoint << setprecision(2) << total << endl;
return 0;
}

The only problem I'm having now is allowing the user to input a category with spaces. It endlessly repeats the loop if a space is entered for category. I was thinking along the lines of using cin.getline(), but that didn't work. What I have here is a string type for category. I thought that would allow for spaces, but there is still something I'm doing wrong. Can someone please instruct me on this final question? Again, thanks so much for your help.
  #10  
Old 23-Oct-2004, 22:17
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
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 brookeville
The only problem I'm having now is allowing the user to input a category with spaces. It endlessly repeats the loop if a space is entered for category. I was thinking along the lines of using cin.getline(), but that didn't work. What I have here is a string type for category. I thought that would allow for spaces, but there is still something I'm doing wrong. Can someone please instruct me on this final question? Again, thanks so much for your help.
CPP / C++ / C Code:
cin >> str;
only reads up to the first whitespace. Look into the getline() method
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
 
 

Recent GIDBlogNARMY by crystalattice

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
Insert problem in Linked list Kay Chan C Programming Language 1 03-Sep-2004 17:06
a C input question.. tmike C Programming Language 2 19-Sep-2003 02:39
a C input question tmike C Programming Language 1 16-Sep-2003 02:31
Code problem (a newbie question) monkster87 CPP / C++ Forum 3 11-Aug-2003 12:46

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

All times are GMT -6. The time now is 15:56.


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