Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 5th, 2005, 12:32 PM   #1
Supreme
Newbie
 
Join Date: Jun 2005
Posts: 8
Rep Power: 0 Supreme is on a distinguished road
C++ homework problems PLEASE help [solved]

I am a first year programming major. My teacher gave us a assignment where we have to have a prorgam use a sentinel-controlled reptition function. Now I get the loop to work properly no problem. My problem is with the sentinel value itself. When i type the sentinel value for the first statement it is supposed to end the loop however what ends up happening is i have to type the sentinel value for each of the input statements and then it will end. Please help

Here is the code:


#include

using std::cout;
using std::cin;
using std::endl;
using std::fixed;

#include

using std::setprecision;

int main()
{

int accnumber;
int counter;

double balance;
double credits;
double charges;
double limit;

balance = 0;
counter = 0;
credits = 0;


while ( accnumber != -1 ){

cout << "Enter account number (-1 to end): ";
cin >> accnumber;

cout << "Enter beginning balance: ";
cin >> balance;

cout << "Enter total charges: ";
cin >> charges;

cout << "Enter total credits: ";
cin >> credits;

cout << "Enter credit limit: ";
cin >> limit;

cout << fixed << setprecision( 2 );

balance = (balance + charges - credits);

cout << "Account: " << accnumber << endl;

cout << "Credit limit: " << limit << endl;

cout << "Balance: " << balance << endl;

if ( balance > limit )

cout << "Credit Limit Exceeded." << endl;

counter = counter + 1;

}




return 0;
}

Anyone who can figure out why this is doing this PLEASE explain to me what I did/ am doing wrong. I am trying to understand C++ to its fullest and don't want to cheat myself.

Last edited by Supreme; Jun 6th, 2005 at 10:47 AM.
Supreme is offline   Reply With Quote
Old Jun 5th, 2005, 2:02 PM   #2
linuxman2k1
Newbie
 
Join Date: May 2005
Location: anonymous namespace
Posts: 6
Rep Power: 0 linuxman2k1 is on a distinguished road
	cout << "Enter account number (-1 to end): ";
	cin >> accnumber;
try this
	cout << "Enter account number (-1 to end): ";
	cin >> accnumber;
	if(accnumber == -1){
		break;
	}
linuxman2k1 is offline   Reply With Quote
Old Jun 5th, 2005, 2:47 PM   #3
Supreme
Newbie
 
Join Date: Jun 2005
Posts: 8
Rep Power: 0 Supreme is on a distinguished road
Quote:
Originally Posted by EverLearning
In the flow of the routine, once "break" is reached the loop automatically terminates regardless whether the condition, that makes the loop going, is met or not.
For example (even though it's not making much sense to do exactly smth. like this), in following code x == 10 will not occur as the loop will be terminated once y == 0;
x = 0;
y = 3;
while (x != 10)
{
   x = x + 1;
   y = y - 1;
   if (y == 0)
   {
       break;
    }
}

"continue" is an opposite equivalent.

Okay I used a break and it worked. thx guys I truly appreciate it. However I have a question now. If the sentinal value was set in thei while statment then why wasnt the sentinal value working as I assumed it would? Maybe my understanding of sentinal is faulty? Here is the corrected code, tell me what you guys think

Quote:
#include <iostream>

using std::cout;
using std::cin;
using std::endl;
using std::fixed;

#include <iomanip>

using std::setprecision;

int main()
{

int accnumber; // Account Number for each account
int counter; // number of accounts

double balance; // number with decimal point for balance
double credits; // number with decimal point for credits
double charges; // number with decimal point for charges
double limit; // number with decimal point for limit

balance = 0; // initialize balance
counter = 0; // initialize counter
credits = 0; // initialize credits



while ( accnumber != -1 ){ // set sentinal value

cout << "Enter account number (-1 to end): "; // prompt for input
cin >> accnumber;

if ( accnumber == -1 ){
break;
}

cout << "Enter beginning balance: "; // prompt for input
cin >> balance;

cout << "Enter total charges: "; // prompt for input
cin >> charges;

cout << "Enter total credits: "; // prompt for input
cin >> credits;

cout << "Enter credit limit: "; // prompt for input
cin >> limit;

cout << fixed << setprecision( 2 ); // allows floating point values to be printed

balance = (balance + charges - credits); // calculates balance to be used later

cout << "Account: " << accnumber << endl; // prints account number

cout << "Credit limit: " << limit << endl; // prints Credit Limit value

cout << "Balance: " << balance << endl; // prints calculation of balance

if ( balance > limit ) // condition if balance is greater than limit

cout << "Credit Limit Exceeded." << endl; // prints this statment when if statment is true

counter = counter + 1; // increments counter

}




return 0; // indicate successful termination
}
Supreme is offline   Reply With Quote
Old Jun 5th, 2005, 2:53 PM   #4
Wraith Daquell
Programmer
 
Join Date: Feb 2005
Location: Limbo
Posts: 39
Rep Power: 0 Wraith Daquell is on a distinguished road
Not to be picky or anything, but the line:
[php]counter = counter + 1[/php]
would be better written:
[php]counter++[/php]
__________________
The meek will inherit the earth.

-WDaquell
Wraith Daquell is offline   Reply With Quote
Old Jun 5th, 2005, 2:55 PM   #5
EverLearning
Hobbyist Programmer
 
EverLearning's Avatar
 
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4 EverLearning is on a distinguished road
Quote:
Originally Posted by Supreme
Okay I used a break and it worked. thx guys I truly appreciate it. However I have a question now. If the sentinal value was set in thei while statment then why wasnt the sentinal value working as I assumed it would? Maybe my understanding of sentinal is faulty? Here is the corrected code, tell me what you guys think
If i understand you correctly: you are asking why it did not work without the break?
Then it's because the condition of while-loop is not checked until the loop is completed. "Break" passes the control immediately to the statement following the loop, i.e. none of the stuff inside while-loop after "break" is going to execute.
If you are asking something else, please clarify

[edit]
sentinel simply means that some particular condition is reached, therefore routine should take some "other" action. Sentinel is also called a flag if that helps.
__________________
got math? yumm...

"All men by nature desire to know" -Aristotle, Metaphysics

Last edited by EverLearning; Jun 5th, 2005 at 2:59 PM. Reason: explaining sentinel
EverLearning is offline   Reply With Quote
Old Jun 5th, 2005, 2:56 PM   #6
linuxman2k1
Newbie
 
Join Date: May 2005
Location: anonymous namespace
Posts: 6
Rep Power: 0 linuxman2k1 is on a distinguished road
Quote:
Originally Posted by Supreme
However I have a question now. If the sentinal value was set in thei while statment then why wasnt the sentinal value working as I assumed it would?
This is because while will execute all statments in the loop before checking the sentinal value again

edit: EverLearning: you just beat me there
linuxman2k1 is offline   Reply With Quote
Old Jun 5th, 2005, 3:01 PM   #7
Supreme
Newbie
 
Join Date: Jun 2005
Posts: 8
Rep Power: 0 Supreme is on a distinguished road
Quote:
Originally Posted by Wraith Daquell
Not to be picky or anything, but the line:
[php]counter = counter + 1[/php]
would be better written:
[php]counter++[/php]
wraith,

Does it help with performance or does it just look more professional? We just learned about counter++ in this chapter so I am not used to using it but I will from here on out.

Thank you,
Supreme is offline   Reply With Quote
Old Jun 5th, 2005, 3:02 PM   #8
EverLearning
Hobbyist Programmer
 
EverLearning's Avatar
 
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4 EverLearning is on a distinguished road
One quick suggestion:
instead of doing this:
int x;
x = 0;
you can just do this:
int x = 0;

@linuxman2k1: :p
__________________
got math? yumm...

"All men by nature desire to know" -Aristotle, Metaphysics
EverLearning is offline   Reply With Quote
Old Jun 5th, 2005, 3:03 PM   #9
Supreme
Newbie
 
Join Date: Jun 2005
Posts: 8
Rep Power: 0 Supreme is on a distinguished road
Quote:
Originally Posted by EverLearning
If i understand you correctly: you are asking why it did not work without the break?
Then it's because the condition of while-loop is not checked until the loop is completed. "Break" passes the control immediately to the statement following the loop, i.e. none of the stuff inside while-loop after "break" is going to execute.
If you are asking something else, please clarify

[edit]
sentinel simply means that some particular condition is reached, therefore routine should take some "other" action. Sentinel is also called a flag if that helps.
ever learning no you hit it right on the head I understand completely. Was I not using the proper terminology when i explained my question? I hope to get a job in the programming field once im done with this degree. Should take 2 yrs from this point. Anyway just trying to master c++

I truly apprecaite all of your help. I am glad I found this place.
Supreme is offline   Reply With Quote
Old Jun 5th, 2005, 3:10 PM   #10
EverLearning
Hobbyist Programmer
 
EverLearning's Avatar
 
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4 EverLearning is on a distinguished road
Quote:
Was I not using the proper terminology when i explained my question?
I just wanted to be sure.
Quote:
I am glad I found this place.
Stick around ...
__________________
got math? yumm...

"All men by nature desire to know" -Aristotle, Metaphysics
EverLearning is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:29 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC