![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2005
Posts: 8
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Newbie
Join Date: May 2005
Location: anonymous namespace
Posts: 6
Rep Power: 0
![]() |
cout << "Enter account number (-1 to end): "; cin >> accnumber; cout << "Enter account number (-1 to end): ";
cin >> accnumber;
if(accnumber == -1){
break;
} |
|
|
|
|
|
#3 | ||
|
Newbie
Join Date: Jun 2005
Posts: 8
Rep Power: 0
![]() |
Quote:
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:
|
||
|
|
|
|
|
#4 |
|
Programmer
Join Date: Feb 2005
Location: Limbo
Posts: 39
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#5 | |
|
Hobbyist Programmer
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4
![]() |
Quote:
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. Last edited by EverLearning; Jun 5th, 2005 at 2:59 PM. Reason: explaining sentinel |
|
|
|
|
|
|
#6 | |
|
Newbie
Join Date: May 2005
Location: anonymous namespace
Posts: 6
Rep Power: 0
![]() |
Quote:
edit: EverLearning: you just beat me there |
|
|
|
|
|
|
#7 | |
|
Newbie
Join Date: Jun 2005
Posts: 8
Rep Power: 0
![]() |
Quote:
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, |
|
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4
![]() |
One quick suggestion:
instead of doing this: int x; x = 0; int x = 0; @linuxman2k1: :p ![]() |
|
|
|
|
|
#9 | |
|
Newbie
Join Date: Jun 2005
Posts: 8
Rep Power: 0
![]() |
Quote:
I truly apprecaite all of your help. I am glad I found this place. |
|
|
|
|
|
|
#10 | ||
|
Hobbyist Programmer
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4
![]() |
Quote:
Quote:
![]() |
||
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|