I know you moved the case up a bit, just trying to understand location from your
"try defining the switch before you start reading into it, makes things a bit more clear at least the very least"
I am sure you now know I am a newbie..
I know you moved the case up a bit, just trying to understand location from your
"try defining the switch before you start reading into it, makes things a bit more clear at least the very least"
I am sure you now know I am a newbie..
Thanks so much,,, I figured it out with your help.....
I just moved
cout<< "Enter item ordered [A/B/C/D] or T to calculate total: ";
cin >> comboType;
orderTotal += comboPrice;
under the //end switch bracket....
many thanks for walking me through this!
Lol, you're making this very difficult on me, because i'm new the forum and know they don't like giving away the direct answers because this forum does not condone cheating on homework and I agree.. so I am going to re-phrase this the best I can.
Remember C++ reads top down.. as you either did not quite understand how to or did not open up a notepad and type out exactly what you saw....I am going to emulate this...I will not provide any further code instruction because from here on out I believe i WILL give you the answer if i do.. but I will show you the novice diagnosis technique I have been using when i get either get compile errors or segmentation faults 20 times in a row (I'm not even as far as you, I don't use classes yet, this is just math and logic, so you will get it )
The first segment of [code] here will be your while loop, the second will be my "notepad" illustration
Code:cout << "Enter item ordered [A/B/C/D] or T to calculate total: "; cin >> comboType; comboType = toupper(comboType); while (comboType != 'T') { cout << "Enter item ordered [A/B/C/D] or T to calculate total: "; cin >> comboType; orderTotal += comboPrice; switch (comboType) { case 'A': comboPrice = 4.25; break; case 'B': comboPrice = 5.75; break; case 'C': comboPrice = 5.25; break; case 'D': comboPrice = 3.75; break; //end switch } // end while }
Start -> Run -> Notepad
Code:"Enter item ordered[A/B/C/D] or T to calculate total:" Read what was typed in to comboType So long as comboType does not equal the letter 'T' do the following... "Enter item ordered[A/B/C/D] or T to caculate total:" Read what was typted in and store it in variable comboType ordertotal(0.0) = orderder(0.0) + comboprice(0.0) (or) add comboprice(0.0) to ordertotal(0.0) NOW what does A B C D mean? A4.25 B5.75 C5.25 D3.75 did they enter T as the input? Yeah? Ok, Tell me how much they owe ( Ordertotal)
The main thing I want you to look at here, is your first calculation, the first time that ordertotal is calculated, think of what it would be missing by you having this calculating this early..and think of the EXACT amount your current program result is short of what it should be.
.... Gods of this forum..please don't ban me...I know that was too much
Hey again,
Good job man, It's all about order , i'm learning that alot, C++ can be both forgiving and a cruel mistress at times, she'll call out your mistakes, but if it's the order or math you got wrong, then it's all downhill
as I always say,,, " You don't learn a thing if someone gives you the answer, However, a nudge in the right direction can only lead to learning the answer"
I agree MK... But I don't wish to be banned before day two..it's just not stylish
Do you just try to determine whether they're doing it for a class and make a judgement call or actually ask?
Since from his reply and the explaining quickly the way he did., i believe he did figure this out and I will demonstrate here the proper way and why it was shooting the wrong answer, for google searchs for the resource .
Before this begins, I would like to make a quick note to harley...i'm going to be changing something in my version..check out what it is..and see if you notice the redundancy removal.
Original:
and now, a bit cleaner...Code://declare variables char comboType = ' '; double comboPrice = 0.0; double orderTotal = 0.0; //input data cout << "Enter item ordered [A/B/C/D] or T to calculate total: "; cin >> comboType; comboType = toupper(comboType); while (comboType != 'T') { cout << "Enter item ordered [A/B/C/D] or T to calculate total: "; cin >> comboType; orderTotal += comboPrice; switch (comboType) { case 'A': comboPrice = 4.25; break; case 'B': comboPrice = 5.75; break; case 'C': comboPrice = 5.25; break; case 'D': comboPrice = 3.75; break; //end switch } // end while } //display output if (comboType = 'T') { cout << "Please pay this amount: " << orderTotal << endl; }
Code://declare variables char comboType = ' '; double comboPrice = 0.0; double orderTotal = 0.0; //input data cout << "Enter item ordered [A/B/C/D] or T to calculate total: "; cin >> comboType; while (comboType != 'T'){ comboType = toupper(comboType); switch (comboType){ case 'A': comboPrice = 4.25; break; case 'B': comboPrice = 5.75; break; case 'C': comboPrice = 5.25; break; case 'D': comboPrice = 3.75; break; //end switch } cin >> comboType; orderTotal += comboPrice; //end while } //display output if (comboType = 'T'){ cout << "Please pay this amount: " << orderTotal << endl;
In the first code, within the loop, comboType was being redefined as 0.0 before the cases were defined, so no matter what, he would be missing exactly 5.75...or the first entry he put in during the sequence of B B A D D D where B == 5.75... since the first
was ran before the actual defintion of "B" was run, ...just 0.0 = 0.0 + 0.0 and thus the first entry would always be missed, whether this be B B A D D or A B A D D or C B A D D etc..Code:orderTotal += comboPrice;
Hope this helps, if you have any questions, just ask. ( And for the expert vets here, please feel free to clarify)
EDIT: Sorry for the spaces instead of TABS..nano formats oddly(But they should be spaced to standard tabs )...
Last edited by Spazmotic; 03-06-2010 at 11:30 AM.
At the same time, it is a programming resource and sometimes the most helpful thing is example or demonstration code. Not everyone here is doing homework, and the needs and priorities of that community should be balanced with the need to avoid becoming a place where people come to get their homework done for free.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge