class help

#include<iostream>
using namespace std;

class Mp3Player{
public:
Mp3Player(){
ntracks=0;}


void playtrack(){
ntracks=30;}
void removeTrack(){
ntracks=0;}
void recordtracks(){
if(tracks>1) cout<<"no space remaining\n";
else ntrackts=ntrackts-1;

}

void batterylife(){
for(int j=0;j<= 90;j++)

{
batterylife >= 90 ? cout << "very high " : cout << "very low")

cout<<"batterylife >=50 ? "low battery" : "high battery")

cout<<"batterylife<=15 ? "very low battery" : "hight bettery" )

batterylife <= 5 ? cout << "dismiss the battery" : cout << "high battery")
}


int remaining(){
return ntracks;}


private:
int ntracks;
int bitrate;
int batterylife;
int batterylevel;



};
int main( ){
Camera Mp3Player;
Mp3Player.playtrack();

cout<<"I have "<<Mp3Player.ntracks<< " tracks remaining in my mp3player";
cout<<endl;
Mp3Player.removeTrack();
cout<<"I have "<<Mp3Player.remaining()<< " total tracks after delete";
cout<<endl;

Mp3Player.displaybatterylife();


cout<<"the battery life is"<<Mp3Player.displaybatterylife<<"in your mp3player";
cout<<endl;

return 0;

}

/* plz help me it can't work+ i won't it to show me batterylife i.e >= 90percent == full
Whoa. You've got a whole lot of errors in here, so let's start with the root problem.

Format - One of the first things you need to learn about writing code is this: If you can't read it, it's worthless. (Even if it functions like it's supposed to.) With that in mind you should try writing your code in a format that's as legible as possible. Proper indentation is crucial to legibility. For example, the poor indentation practice you used above makes it very difficult to spot the fact that you're missing a bracket after the batterylife function. Take a look at this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//Your code:
void batterylife(){ 
for(int j=0;j<= 90;j++)

{ 
batterylife >= 90 ? cout << "very high " : cout << "very low")

cout<<"batterylife >=50 ? "low battery" : "high battery")

cout<<"batterylife<=15 ? "very low battery" : "hight bettery" )

batterylife <= 5 ? cout << "dismiss the battery" : cout << "high battery") 
}

//And now again with better formatting:
void batterylife()
{ 
      for(int j=0;j<= 90;j++)
     { 
          batterylife >= 90 ? cout << "very high " : cout << "very low")

          cout<<"batterylife >=50 ? "low battery" : "high battery")

          cout<<"batterylife<=15 ? "very low battery" : "hight bettery" )

          batterylife <= 5 ? cout << "dismiss the battery" : cout << "high battery") 
     }


See how the missing } jumps out at you? That's what you want. There are differing opinions as to the 'proper' way to format code. I prefer the style above, where the brackets are always displayed on there own line, and indented to show there scope, but there are other methods you can use.


Now let's tackle the rest of the problems. First of all, when writing code you have to be a very careful typist. Typos mean errors. For example here:

1
2
3
4
5
6
7
8
9
10
11
12
void recordtracks()
{
     if(tracks>1) //Here the compiler will complain that it doesn't know what tracks is.  It should be ntracks.
     {
        cout<<"no space remaining\n";
     }
     else 
     {
        ntrackts=ntrackts-1; //Here again, you have ntrackts, instead of ntracks.  This will cause a compiler error.
      }
}
//One final note about all the above code: You have logic overlaps, and gaps.  For example if batterylife is equal to  91, both of the first two statements will execute. 


The other problem with the code directly above is that you haven't initialized battery life to anything yet. (At least, I don't see one.) You should assign batterylife a value in the constructor.

Also here:
1
2
3
4
5
6
7
8
9
10
11
void batterylife()
{ 
      for(int j=0;j<= 90;j++)
     { 
          batterylife >= 90 ? cout << "very high " : cout << "very low")  //Here there shouldn't be a ')' after "very low.  There should be a ';' though.

          cout<<"batterylife >=50 ? "low battery" : "high battery")  //Same comment as above, plus you need to remove the " before batterylife, and the cout<< needs to go with each individual case as you have it above.

          cout<<"batterylife<=15 ? "very low battery" : "hight bettery" ) //Same comments as the one above.  Also note the "hight bettery" instead of the "high battery"

          batterylife <= 5 ? cout << "dismiss the battery" : cout << "high battery") //Get rid of the ')' and add ';' 


Now in the main function:

The notation Camera Mp3Player doesn't mean anything in this context. If you're trying to declare an Mp3Player called 'Camera' then it should be declared:

1
2
Mp3Player Camera;  //This declares an Mp3Player called camera just as the code directly below would declare an int x.
int x;


Now you have an Mp3Player called Camera, so everywhere you reference the Mp3Player in main, you need to reference Camera. Remember: Operations are done with an instance of the class, not with the class itself. To keep this straight just remember that you wouldn't simply state int =8, instead you would state int x = 8. (x being the instance of the class int.)

The next problem in main is with the statement: Mp3Player.ntracks This statement will generate a compiler error because the variable ntracks is private in the Mp3Player class. In order to access this variable from outside the Mp3Player class you will either need to make nvariables public (bad practice) or you will need to define a function provide access to it from the 'outside world'. (That may seem like the more tedious solution but trust me, it's a habit that will help you in the long run.) Here's how you would write a function to provide access to ntracks.

1
2
3
4
void Accessntracks ()
{
   return ntracks;
}


Add that to the Mp3Player class and you'll be able to access the ntracks variable from outside of the class.

Another problem I see is that you make reference to a member function displaybatterylife. You never defined a function by that name. You defined batterylife(), and I can only assume that's what you intended.

A couple final suggestions when posting on this forum: Post an explanation of your problem before just posting the code. That's where most people will look for a summary of what is needed. I see your comment at the end, but many people will not even make it that far if you don't give an explanation of what is going on. Also, when you post code on this site, use the 'Insert Code' button, and paste your code in between the code symbols that pop up. That makes it easier to read.

Well, that's it. If you have any other questions, please feel free to post back and ask. Good luck!

Topic archived. No new replies allowed.