943,783 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1437
  • C++ RSS
Oct 2nd, 2008
0

Arrays, Strings, ConCat??? Please help

Expand Post »
I am at the end of my first term in my C++ classes. I have managed well so far, but this program is completely frustrating me! Please help me figure what to do.
Okay my assignment is kind of complicated for me.....here is what I am supposed to do, followed by the code that I have so far. I know that I still have quite a bit to do, but I am completely stuck at this point, with no clue how to get to where I want to be.

My program should output:
Commissions are calculated using a 0.07 value.
C++ Syntax (Toggle Plain Text)
  1. Sales Name Commission
  2.  
  3. 2000.98 Abbott, Sue 140.07
  4. 950.12 Green, John 66.51
  5. 875.50 Jones, Mary 61.29
etc...

I am supposed to read the info from two input files (sales in one, and names in two) then create arrays for each. Determine commission randomly by generating percentage between .05 and .10 (Is there a way that I could .10 to print like that? In my current code it always reads as .1). Then I need to calculate the commission using sales and the generated number. I'm baffled by this, since I can't just multiply the numbers, I think I need to convert them, but am unsure how. My other BIG problem is how can I get the format to print the Sales, Name, and Commission on each line? There is a couple other things like sorting and printing to output file, but I'm pretty sure I can do that.
C++ Syntax (Toggle Plain Text)
  1. //
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <iomanip>
  6. #include <cstdlib>
  7. #include <ctime>
  8. #include <sstream>
  9.  
  10. using namespace std;
  11.  
  12. void Print();
  13. void ReadSales();
  14. void ReadNames();
  15. float getrand();
  16.  
  17. int main()
  18. {
  19. Print();
  20. ReadSales();
  21. ReadNames();
  22.  
  23. system("pause");
  24. return 0;
  25. }
  26.  
  27. void ReadSales()
  28. { char sales [5][15];
  29.  
  30. ifstream myfile("File one.txt");
  31.  
  32. if(myfile.is_open())
  33. {
  34. while(!myfile.eof())
  35. {
  36. for (int i = 0; i < 4 ; i++)
  37. {
  38. myfile.get(sales[i],8,'\0');
  39. cout << sales[i];
  40. }
  41. }
  42. cout << endl;
  43. }
  44. else cout << "Unable to load file!" << endl;
  45.  
  46. }
  47. void ReadNames()
  48. {
  49. char name [5][15];
  50.  
  51. ifstream myfile2("File two.txt");
  52.  
  53. if(myfile2.is_open())
  54. {
  55. while(!myfile2.eof())
  56. {
  57. int sizeOfFile = 0;
  58. for (int j = 0; j < 4 ; j++)
  59. {
  60. myfile2.get(name[j],15,'\0');
  61. sizeOfFile++;
  62. }
  63. for (int j = 0; j < sizeOfFile; j++)
  64. {
  65. cout << name[j];
  66. }
  67. }
  68. cout << endl;
  69. }
  70.  
  71. else cout << "Unable to load file!" << endl;
  72. }
  73. void Print()
  74. {
  75. cout << "Commissions calculated using a " << getrand() << " value." << endl << endl;
  76. cout << "Sales" << setw(16) << "Name" << setw(32) << "Commission" << endl << endl;
  77.  
  78. }
  79. float getrand()
  80. {
  81. srand(unsigned(time(0)));
  82.  
  83. float commission = (rand() % 6 + 5.0)/100;
  84.  
  85. return commission;
  86. }
Last edited by cscgal; Oct 2nd, 2008 at 10:54 am. Reason: Added code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
B_Acs is offline Offline
4 posts
since Sep 2008
Oct 2nd, 2008
0

Re: Arrays, Strings, ConCat??? Please help

I didn't really read your code as I am in a hurry.
this might be long and consume a lot of memory but you sho;uld try it anyway: why don't you take input from a file and store it into two parallel arrays. After storing that information I think you should use the random function to generate a number and use that number to point to whatever index will be displayed.

If this is not helpful sorry it's all i've got since you didn't highlight the keypoints and I don't have a lot of time.
goodluck
Reputation Points: 8
Solved Threads: 1
Newbie Poster
STUDENT#101 is offline Offline
16 posts
since Aug 2008
Oct 2nd, 2008
0

Re: Arrays, Strings, ConCat??? Please help

Sorry this is what I was having trouble with:

Then I need to calculate the commission using sales and the generated number. I'm baffled by this, since I can't just multiply the numbers, I think I need to convert them, but am unsure how. My other BIG problem is how can I get the format to print the Sales, Name, and Commission on each line?

I still need to create a third array that uses sales array and rand commission and since thier not same type I don't know how to convert. Then I think I need to combine my arrays somehow, possibly concat in order to get my final out put of columns.

I've spent a lot of hours trying to figure this out and just looking for some guidance.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
B_Acs is offline Offline
4 posts
since Sep 2008
Oct 2nd, 2008
0

Re: Arrays, Strings, ConCat??? Please help

post deleted
Last edited by B_Acs; Oct 2nd, 2008 at 11:26 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
B_Acs is offline Offline
4 posts
since Sep 2008
Oct 2nd, 2008
0

Re: Arrays, Strings, ConCat??? Please help

You can use the function strtod() to convert string to double.
Reputation Points: 118
Solved Threads: 15
Posting Pro in Training
Denniz is offline Offline
428 posts
since Sep 2008
Oct 2nd, 2008
0

Re: Arrays, Strings, ConCat??? Please help

I understand that I can, but I don't understand how.....
When I try, I keep getting errors saying:
cannot convert `char (*)[50]' to `const char*' for argument `1' to `double strtod(const char*, char**)'
or something similar to that.
Last edited by B_Acs; Oct 2nd, 2008 at 1:03 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
B_Acs is offline Offline
4 posts
since Sep 2008
Oct 4th, 2008
0

Re: Arrays, Strings, ConCat??? Please help

k
The rest I still have to think about but for outputing
C++ Syntax (Toggle Plain Text)
  1. cout<<"commision\t"<<"blah\t"<<"blah"<<endl;
  2. for(int n=0;n<blah;n++)
  3. {
  4. cout<<commision<<"\t"<<blah<<"\t"<<blah<<endl;
  5. }
NB blah stands for what ever other data you need to output
Last edited by Tekmaven; Oct 4th, 2008 at 6:59 pm. Reason: Code tags
Reputation Points: 8
Solved Threads: 1
Newbie Poster
STUDENT#101 is offline Offline
16 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: pls help!!!!!!! round robin algortihm
Next Thread in C++ Forum Timeline: Count sequence in char array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC