-3

so my program is that i want the user to enter information about a board so everything is working except the part where i need the user to enter the value of attitude then add another number to increase that attitude so I'm stuck at this. As you can see I declared this function as a friend but when I enter the number that I need to increase it to the value of attitude) it stays the same! I hope I made clear the problem I'm facing! and I'd appreciate some help

#ifndef BOARD_H
#define BOARD_H
class Board {


friend void incAttitude(Board &);


public:

friend void incAttitude(Board &);

static int boardcount;
Board(int=5,int=3,double=1.5,char* ='\0');
~Board();
Board &setBoard(int,int,double,char*);
char getLocation();
Board &print();
double surface();


private:

const int length;
int width;
double attitude;
char location[30];


};
#endif
#include<iostream>
#include<cstring>
#include<string>
#include<assert.h>

using namespace std;
#include "Board.h"

int Board::boardcount=0;

Board::Board(int l,int w,double a,char* lo)
    : length(l)
{
width=w;
attitude=a;
location[0]='\0';
boardcount++;
}


Board::~Board(){

boardcount--;
}

Board &Board::setBoard(int l,int w,double a,char* lo)

{   
    width=(w>=2 && w<=5)?w:3;
    attitude=(a>=1.5 && a<=2.8)?a:1.5;
    strncpy_s(location,lo,29);
    location[29]='\0';
    return *this;
}
char Board::getLocation(){


    return *location;

}

double Board::surface(){

return length*width;
}

void incAttitude(Board &h)
{
 double incAttitude;
cout<<"enter to increase attitude "<<endl;
cin>>incAttitude;
h.attitude+=incAttitude;
cout<<"the attitude of the board after the increase : "<<h.attitude<<endl;
}

Board &Board::print(){

    cout<<"the length of the boarad : "<<length<<endl;
    cout<<"the width of the board : "<<width<<endl;
    cout<<"the height of the board : "<<attitude<<endl;
    cout<<"the location of the board : "<<location<<endl;
    cout<<"the surface of the board : "<<surface()<<endl;
    return *this;

}

int main(){
    Board hh;
    Board *boardptr;
int count,len,wid;
double att;
char locat[30];


cout<<"how many boards do you need to create ? "<<endl;
cin>>count;
boardptr = new Board[count];
assert(boardptr!=0);



for (int i=0; i<count; i++){
 cout << "Enter the length: ";
 cin >>len;
 cout << "Enter the width: ";
 cin >> wid;
 cout << "Enter the attitude: ";
 cin >> att;
  incAttitude(hh);
 cout<<"Enter the location: ";
 cin >>locat;
 boardptr[i].setBoard(len,wid,att,locat);
 cout<<"------------------------------------------"<<endl;

  if (strcmp("NewYork",locat) == 0) 
  {
     boardptr[i].setBoard(len,wid,att,locat).print();
  }

}


delete [] boardptr;

system("pause");
return 0;
}
Caroline
  • 1
  • 1
  • 4

1 Answers1

2

You have an array of Boards, which is called boardptr. But then what is hh? A single board. What does it do? Nothing! But you are using it in incAttitude(hh);! It does increase the altitude, but not on the board boardptr[i] (that you are using), rather on hh.

Secondly, you are never setting att, which means the altitude is always 1.5 (that's the default value). So incAttitude adds the number to 1.5. You could pass att to incAttitude for example.


Then if your comparison condition, why do you set the board again? It resets every value the user chose. You don't need to, because you already call setBoard a few lines early. The data you set with setBoard is saved in that array, so you don't need to override the data with the exact same data. Just use boardptr[i].print().
char arrays, strcmp, raw pointers and fixed size arrays are from C, you should consider the alternatives:
  • Char arrays -> std::string
  • strcmp -> if (str == "foo")
  • Raw pointers -> Smart pointers (or in your case (because they are used as arrays) std::array or std::vector - std::vector for dynamic size arrays)
  • Fixed size arrays -> std::array
Rakete1111
  • 47,013
  • 16
  • 123
  • 162