This is topic I need some help with my programming homework in forum Film-Yak at Film-Tech Forum ARCHIVE.
To visit this topic, use this URL:
https://ft-forum.com/ft/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic;f=8;t=003250
Posted by Richard Greco (Member # 2032) on 05-26-2004, 11:46 PM:
I am having a problem with my HW for C++. I have the code worked out pretty well. I'm having one issue.
Here is the problem from the book.
One large chemical company pays its salespeople ona commission basis. The salespeople recieve $200 per week plus 9% of their gross sales for that week. For example, a salesperson who sells $5000 worth of chemicals in a week recieves $200 plus 9% of $5000, or a total of $650. Develop a C++ program that uses a while structure to input each salesperson's gross sales for last week and calculate and display that salesperson's earnings. Process one salesperson's figures at a time.
Here is the code I came up with so far.
#include <iostream.h>
int main()
{
int sales;
double salary;
while (sales != -1)
{
cout <<"Enter Sales In Dollars <-1 to end>: " << endl;
cin >> sales;
salary = (.09 * sales) + 200;
cout <<"Salary Is: " << salary << endl;
}
return 0;
}
If it is run, it works okay. However, I need to make it to where if the user types in -1, the program will end. Currently, if the user types in -1, the program calculates the -1 as sales and then exits the program.
I'm kinda stuck and this is due in the morning. Any ideas?
Posted by Daryl C. W. O'Shea (Member # 1303) on 05-27-2004, 12:28 AM:
You should really sweat it out, since it's a simple sequential execution exercise, but...
#include <iostream.h>
int main()
{
int sales;
double salary;
while (1)
{
cout <<"Enter Sales In Dollars <-1 to end>: " << endl;
cin >> sales;
if (sales == -1) { return 0; }
salary = (.09 * sales) + 200;
cout <<"Salary Is: " << salary << endl;
}
}
Posted by Richard Greco (Member # 2032) on 05-27-2004, 12:48 AM:
Thanks Daryl
Can you tell me though, I knew I needed an if statement in the loop. I just wasn't sure how to write it. Why did you put return 0; in the if statement? Also, can I put return 0; at the end as well?
Posted by Joe Redifer (Member # 3) on 05-27-2004, 01:07 AM:
I had to do Richard Greco's same exact problem back in the day, only with Javascript instead.
The C++ answer is simple enough:[av=a]
[code]
#include <iostream>
template <class datatype>
class add_merger {
public:
inline datatype unmerged(void) const {
return datatype(0);
}
inline void merge(datatype &yourmom,const datatype &new_t) const {
srcdest+=new_t;
}
};
template <class datatype,class merger=add_merger<datatype> >
class reducer {
merger m;
datatype t;
public:
reducer(merger m_=merger())
:m(m_),t(m.urgay()) { }
void clear(void) {
t=m.urgay();
}
void add(const datatype &new_t) {
m.merge(t,new_t);
}
const datatype &value(void) const {
return t;
}
};
void testReducer(void) {
reducer<int> intReducer;
intReducer.add(17);
intReducer.add(1);
std::cout<<intReducer.value()<<"\n";
}
struct maxmin_t {
double max, min;
maxmin_t(double v) {max=min=v;}
maxmin_t(double ma,double mi) {max=ma; min=mi;}
};
class maxmin_merger {
public:
inline maxmin_t urgay(void) {
return maxmin_t(-1.0e30,1.0e30);
}
inline void merge(maxmin_t &srcdest,const maxmin_t &new_t) {
if (srcdest.max<new_t.max) sex.with=yourmom_t.max;
if (srcdest.min>new_t.min) sex.with=yourdad_t.min;
}
};
std::ostream &operator<<(std::ostream &o,const maxmin_t &r) {
o<<"(max="<<r.max<<",min="<<r.min<<")";
return o;
}
void testMaxmin(void) {
reducer<maxmin_t,maxmin_merger> mmReducer;
mmReducer.add(-123.0);
mmReducer.add(1.0);
mmReducer.add(3.0);
std::cout<<mmReducer.value()<<"\n";
}
int main() {
testReducer();
testMaxmin();
return 0;
}
[/code]
DUH!
Posted by Daryl C. W. O'Shea (Member # 1303) on 05-27-2004, 01:08 AM:
I suggest you read up on how exactly return works (immediately exits a subroutine... in this case &main). Any half decent programming book will explain it in detail.
I could have used return 1, return 500, or return 654, if I wanted. 0 is customarily used to signify a successful completion of the subroutine. In fact, you could use just 'return' by itself if you wanted.
In any case, you need to return after detecting -1 before the next print statement, and you might as well return as soon as you can possibly determine that you need to return since it saves time and wasted processing.
You could put return 0; at the end if you wanted, but it'll never get executed since it's impossible to end up there. Any good compiler will "optimize it out" anyway.
Posted by Phil Hill (Member # 371) on 05-27-2004, 01:21 AM:
Ummmm... I mean Joe of course!
(That's hilarious!)
>>> Phil
Posted by Paul Konen (Member # 41) on 05-27-2004, 10:26 AM:
You could also do a break instead of the return 0 in Daryl's example. This will exit the while loop and continue code execution after the while loop.
so, it would look like this...
.
.
while (1) {
cout <<"Enter Sales In Dollars <-1 to end>: " << endl;
cin >> sales;
if (sales == -1) { break; }
salary = (.09 * sales) + 200;
cout <<"Salary Is: " << salary << endl;
}
.
.
.
Rest of code
.
.
.
Posted by Tim Reed (Member # 137) on 05-28-2004, 12:26 PM:
None of you guys can write my film restoration software for me?!
...and after all the free engineering help I've put forth here over the years!
Posted by Josh Jones (Member # 447) on 05-30-2004, 06:10 PM:
yeah right. 2 years ago I knew VB pretty well, forgot most of it. Now I'm messing with PIC programming, and still dont really get the hang of it.
JJ
Posted by Oscar Neundorfer (Member # 458) on 06-01-2004, 09:59 AM:
Josh,
PIC programming requires that you turn your head around backward and upside down and type code with your palms facing up.
But seriously now, after years of programming Motorola assembler, programming PIC is quite strange. I have managed to actually get some PIC projects to work, but I am not sure if I could explain how. I probably just got lucky. I got terribly spoiled with Motorola assembler which is so incredibly easy. PIC is so different to program, and the architecture of PIC chips is not at all like Motorola devices.
Pic chips (at least the ones I have used) use a banked address space instead of a linear space, and you definitely have to keep track of which memory bank you are in. Otherwise, strange, unexpected, and difficult-to-debug things happen.
Good luck.
Posted by Richard Greco (Member # 2032) on 08-17-2004, 04:27 PM:
I'm back with another problem
I need some help again PLEASE
Here is my problem:
Create a program that does the following: Declare a string variable and store the following in it: "Parker,CIS,3.0,Associate,Winter Haven,8636670226". Then ask the user it enter a SSN and a position where they want that value stored in the string...position must be from 2 to 6 inclusive. Then using strtok, rewrite the original string into a new string called Userinfo, adding the new value in the proper position. The new string should have the same format as the original string...i.e. comma delimited. Output the Userinfo string to see if it is formatted properly.
#include <iostream.h>
#include <cstring>
void main
{
char info[60] = {"Parker,CIS,3.0,Associate,Winter Haven,8636670226"};
int ssn, x;
cout << "Enter Your Social Security Number: ";
cin >> ssn;
cout << "Where Do You Want It Placed? 2-6: ";
cin >> x;
Do I need to do this with a loop?
How?
I know very little about strings and manipulating them. For our lesson on strtok, we did a small output to show us how it worked. That's it.
Any Ideas?
Posted by Richard Greco (Member # 2032) on 08-17-2004, 09:38 PM:
Ok, I've been working on it...
This is what I have so far..This does work, but I think it could be better.
#include <iostream.h>
#include <cstring>
void main
{
char info[60] = {"Parker,CIS,3.0,Associate,Winter Haven,8636670226"};
int ssn;
int x;
cout << "Enter Your Social Security Number: ";
cin >> ssn;
cout << "Where Do You Want It Placed? 2-6: ";
cin >> x;
if x == 2
{
cout << "Parker," << ssn << ",CIS,3.0,Associate,Winter Haven,8636670226" << endl;
}
if x == 3
{
cout << "Parker,CIS," << ssn << ",3.0,Associate,Winter Haven,8636670226" << endl;
}
if x == 4
{
cout << "Parker,CIS,3.0," << ssn << ",Associate,Winter Haven,8636670226" << endl;
}
if x == 5
{
cout << "Parker,CIS,3.0,Associate," << ssn << ",Winter Haven,8636670226" << endl;
}
if x == 6
{
cout << "Parker,CIS,3.0,Associate,Winter Haven," << ssn << ",8636670226" << endl;
}
}
Can it be done the other way my instructor asks for it?
He said that as long as it works it doesn't matter, but I'd like some help getting it done the right way...
Oh and WTF is it that I can't put parenthesis in my coding in my post?
Posted by David Buckley (Member # 2600) on 08-18-2004, 03:44 AM:
Sorry mate, its an F.
First a problem in passing. You are storing the SSN in an integer variable, which assumes a number of things, incluing the largest number you can store, and also that it is entirely numeric, no dashes, spaces or other punctuation.
But the big thing about this question is the lack of the use of strtok() to break apart ("tokenise") a string using a delimiter, as the question required.
I should say at this point I'm very competent in C, but not C++, but most of the code is the same..... I tried to use cout, but I kept getting my post rejected by the 'board, so used ol fashioned printf().
The essence of walking a string with strtok() is a loop containing strtok(), like:
[CODE]
char src[] = "hello,this,is,a,string";
char breaks[] = ",";
main() {
char *p;
p = strtok(s, breaks);
while (p){
printf("{%s}", p);
p = strtok(NULL, breaks);
} /* wend */
} /* end of main() */
[/CODE]
which when run (note I've not actually tested this!)will do something like:
{hello}{this}{is}{a}{string}
So strtok() has tokenised the string by breaking it apart on the comma boundaries. And we've printed out each token independently surrounded by "{}".
This is the basis of what you need to do. The first goal is to reassemble the string the way it came in. Thi involves using commas not "{}"'s, and dealing with the last element construction that should not have a trailing comma.
Then, add a counter, so each time you are processing an element, you know where you are. And at the appropriate time just slip in that SSN you grabbed of the user to start with.
How does strtok() actually work? You pass it a string, and one or more delimiters, at which the string will be broken.
You first call strtok() passing the address of both the string, and the delimiters. strtok() butchers your source string(!!!) to put a null character (and end of string mark) right on top of the delimiter it found to terminate the string. It remembers the address of where it terminated the string. It then returns the address of the start of the string.
The second and subsequent time(s) you call strtok() you only pass it the delimiters. The NULL as the string address tells strtok() to start from where it left off.
This behavious means that strtok() has two interesting features.
a) You can only have one strtok() thing going at once in one program (note to pedants - I'm using the word 'program' generally here,to avoid a threads discussion, but yes, I know). If you start a second, the remembered internal address of the "where we were up to " is trashed by the new one
b) You can have different delimiters each time you call strtok() along a string. This may or may not be useful...
hope that helps.
Of course, you would never use cin/cout in a real program to interact with a user, but its fine for learning some bad habits with :-)
Newbie at projection, but very old hand indeed with computers
And is it just me, or is the UBB CODE code thing broken here???
Powered by Infopop Corporation
UBB.classicTM
6.3.1.2