| | |
C++ class file that uses an array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2006
Posts: 1
Reputation:
Solved Threads: 0
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
I come up with the following code while working on this project but I couldn't figure out what I did wrong.
I really appreciate if someone can point me a way out.
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)
#include <iostream.h> const int MAX = 15; void get_values(int []); void print_values (int []); void sort_values (int [], int); void search_values (int []); int main() { int values[MAX]; get_values(values); sort_values(values, MAX); print_values(values); //This will allow user to see the sorted order of the values entered if enabled. search_values(values); return 0; } void get_values(int value[]) { for (int i=0; i < MAX; i++) { cout << "Please enter value #" << i+1 <<" : "; cin >> value[i]; } } void print_values (int value[]) { cout<<endl; for (int i=0; i<MAX; i++) { cout << value [i] << endl; } cout<<endl; } void sort_values (int value[], int maxval) { int i, j, temp, flag; 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 search_values (int value[]) { int check; cout << endl <<"Please enter the value you want to search for: "; cin >> check; for (int i=0; i<MAX; i++) { if (value[i] == check) { cout << "The value you searched for found in position #" << i+1 << endl; } } }
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.
•
•
Join Date: Apr 2006
Posts: 26
Reputation:
Solved Threads: 0
The declatrion of class is good.
So, why did declare "int value[15]" again in function main.
And what is this :
what does that do ?
C++ Syntax (Toggle Plain Text)
class project { public: project(); int get_value(); void sort_values(int value[]); void search_values(int value[]); private: int value[15]; };
So, why did declare "int value[15]" again in function main.
And what is this :
C++ Syntax (Toggle Plain Text)
project::project() { value[15]; }
![]() |
Similar Threads
- Reading in a *.csv file and loading the data into an Array (Java)
- Class File, making directories... relative to (Java)
- Question about debugging .class file (Java)
- Reading from a file to fill an array (Java)
- Read in a file and store in char array (C)
- How it is possible tu use a class that is in a new vb file?? (ASP.NET)
- How to load binary content of a .class file? (Java)
- Help Reading Info in Text File Into an Array (C++)
Other Threads in the C++ Forum
- Previous Thread: Change text colour in console
- Next Thread: How to put the random integer between 1-100 into the 2 dimensions array (5x5 metrix)
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





