Arrays, Strings, ConCat??? Please help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 4
Reputation: B_Acs is an unknown quantity at this point 
Solved Threads: 0
B_Acs B_Acs is offline Offline
Newbie Poster

Arrays, Strings, ConCat??? Please help

 
0
  #1
Oct 2nd, 2008
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.
  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.
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 16
Reputation: STUDENT#101 is an unknown quantity at this point 
Solved Threads: 1
STUDENT#101 STUDENT#101 is offline Offline
Newbie Poster

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

 
0
  #2
Oct 2nd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 4
Reputation: B_Acs is an unknown quantity at this point 
Solved Threads: 0
B_Acs B_Acs is offline Offline
Newbie Poster

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

 
0
  #3
Oct 2nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 4
Reputation: B_Acs is an unknown quantity at this point 
Solved Threads: 0
B_Acs B_Acs is offline Offline
Newbie Poster

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

 
0
  #4
Oct 2nd, 2008
post deleted
Last edited by B_Acs; Oct 2nd, 2008 at 11:26 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 429
Reputation: Denniz is on a distinguished road 
Solved Threads: 15
Denniz's Avatar
Denniz Denniz is offline Offline
Posting Pro in Training

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

 
0
  #5
Oct 2nd, 2008
You can use the function strtod() to convert string to double.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 4
Reputation: B_Acs is an unknown quantity at this point 
Solved Threads: 0
B_Acs B_Acs is offline Offline
Newbie Poster

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

 
0
  #6
Oct 2nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 16
Reputation: STUDENT#101 is an unknown quantity at this point 
Solved Threads: 1
STUDENT#101 STUDENT#101 is offline Offline
Newbie Poster

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

 
0
  #7
Oct 4th, 2008
k
The rest I still have to think about but for outputing
  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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC