944,166 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4607
  • C++ RSS
Apr 28th, 2006
0

C++ class file that uses an array

Expand Post »
Hi,
Can anyone help with a project I'm working on I'm new to classes
and I got really confused while trying to convert a simple console application in to a class file. I would appreciate some help if anyone willing to.
Following is the code I am trying to convert
C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2. const int MAX = 15;
  3. void get_values(int []);
  4. void print_values (int []);
  5. void sort_values (int [], int);
  6. void search_values (int []);
  7. int main()
  8. {
  9. int values[MAX];
  10. get_values(values);
  11. sort_values(values, MAX);
  12. print_values(values); //This will allow user to see the sorted order of the values entered if enabled.
  13. search_values(values);
  14. return 0;
  15. }
  16. void get_values(int value[])
  17. {
  18.  
  19. for (int i=0; i < MAX; i++)
  20. {
  21. cout << "Please enter value #" << i+1 <<" : ";
  22. cin >> value[i];
  23. }
  24. }
  25.  
  26. void print_values (int value[])
  27. {
  28. cout<<endl;
  29. for (int i=0; i<MAX; i++)
  30. {
  31. cout << value [i] << endl;
  32. }
  33. cout<<endl;
  34. }
  35. void sort_values (int value[], int maxval)
  36. {
  37. int i, j, temp, flag;
  38. for(i=maxval-1; i>0; i--)
  39. {
  40. flag = 1;
  41. for(j=0; i>j; j++)
  42. {
  43. if(value[j]>value[j+1])
  44. {
  45. flag = 0;
  46. temp = value[j];
  47. value[j] = value[j+1];
  48. value[j+1] = temp;
  49. }
  50. }
  51. if(flag)
  52. break;
  53. }
  54. }
  55. void search_values (int value[])
  56. {
  57. int check;
  58. cout << endl <<"Please enter the value you want to search for: ";
  59. cin >> check;
  60. for (int i=0; i<MAX; i++)
  61. {
  62. if (value[i] == check)
  63. {
  64. cout << "The value you searched for found in position #" << i+1 << endl;
  65. }
  66. }
  67. }

I come up with the following code while working on this project but I couldn't figure out what I did wrong.

#include <iostream.h>
#include "p6.h"
project::project()
{
value[15];
}
int project::get_value()
{
return value[15];
}
void project::sort_values(int value[])
{
int i, j, temp, flag;
int maxval = 15;
for(i=maxval-1; i>0; i--)
{
flag = 1;
for(j=0; i>j; j++)
{
if(value[j]>value[j+1])
{
flag = 0;
temp = value[j];
value[j] = value[j+1];
value[j+1] = temp;
}
}
if(flag)
break;
}
}
void project::search_values(int value[])
{
int check;
cout << "Please enter the value you want to search for: ";
cin >> check;
for (int i=0; i<15; i++)
{
if (value[i] == check)
{
cout << "The value you searched for found in position #" << i+1 << endl;
}
}
}
******************************************
#ifndef P6_h
#define P6_h
class project
{
public:
project();
int get_value();
void sort_values(int value[]);
void search_values(int value[]);
private:
int value[15]; 
};
#endif
******************************************
 
#include <iostream.h>
#include "p6.h"
void get_values (int []);
void print_values (int []);
 
int main()
{ 
project myProject;
int count = 15;
int value[15]; 
get_values(value[]);
return 0;
}
void get_values(int values[])
{
for (int i=0; i<15; i++)
{
project.get_value (values[i]);
cout <<"Enter Values: ";
cin >> values[i];
}
}


I really appreciate if someone can point me a way out.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sargorath is offline Offline
1 posts
since Apr 2006
Apr 28th, 2006
0

Re: C++ class file that uses an array

The declatrion of class is good.
C++ Syntax (Toggle Plain Text)
  1. class project
  2. {
  3. public:
  4. project();
  5. int get_value();
  6. void sort_values(int value[]);
  7. void search_values(int value[]);
  8. private:
  9. int value[15];
  10. };

So, why did declare "int value[15]" again in function main.

And what is this :
C++ Syntax (Toggle Plain Text)
  1. project::project()
  2. {
  3. value[15];
  4. }
what does that do ?
Reputation Points: 12
Solved Threads: 0
Light Poster
dude543 is offline Offline
26 posts
since Apr 2006

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: Change text colour in console
Next Thread in C++ Forum Timeline: How to put the random integer between 1-100 into the 2 dimensions array (5x5 metrix)





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


Follow us on Twitter


© 2011 DaniWeb® LLC