Updated: I have an assignment where I'm supposed to print out a letter distribution of letters depending on what is typed on the keyboard. The printing should be for an example A:5 B:8 C:0 ... and so on which is done by the functions berakna_histogram_abs and skriv_histogram_abs. The total amount of letters should also be printed out from the first mentioned function. As it seems now it works where the amount for each letter is correct, however not the total amount which is always 0.
My first question is: Where in the code can I find the problem where the total amount of letters always is 0?
#include <string>
#include <cctype>
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
// Global constants:
const int ANTAL_BOKSTAVER = 26; //A-Z
const int ANTAL_SPRAK = 4;
const double TOLK_HJALP[ANTAL_SPRAK][ANTAL_BOKSTAVER]=
{{8.27,1.48,2.94,4.03,11.78,2.22,1.72,6.77, //English
7.39,0.12,0.81,3.76,2.85,6.71,7.79,1.54,
0.05,5.95,6.69,9.07,2.66,1.13,2.14,0.19,
1.89,0.03},
{7.97,1.40,3.55,3.79,16.89,1.02,1.00,0.75, //French
7.08,0.38,0.04,5.51,2.82,8.11,5.19,2.78,
1.01,6.69,8.35,7.22,6.09,1.35,0.02,0.54,
0.30,0.15},
{9.50,1.11,1.53,5.30,8.94,1.74,3.57,3.94, //Swedish
3.98,0.89,3.26,4.93,3.41,8.46,5.01,1.77,
0.00,6.73,5.56,9.20,1.94,2.42,0.00,0.05,
0.45,0.00},
{5.12,1.95,3.57,5.07,16.87,1.35,3.00,5.79, //German
8.63,0.19,1.14,3.68,3.12,10.64,1.74,0.42,
0.01,6.30,6.99,5.19,3.92,0.77,1.79,0.01,
0.69,1.24}};
// Class declaration
class Text
{
private:
string text;
bool histOK;
int f[ANTAL_BOKSTAVER];
public:
Text ();
Text (string nytext, bool nyhistOK,int nyf[] );
string get_text();
bool get_histOK();
int get_f();
void setText(string nyText);
bool beraknaHistogramAbs(const string &str,int hist[]);
void skrivHistogramAbs(string Alfab, int hist[]);
};
//Funktiondeklarations
bool beraknaHistogramAbs(const string &str,int hist[]);
void skriv_histogram_abs(string Alfab, int hist[]);
// Main program:
int main()
{
string text, alfabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
bool histOK = true;
int f[ANTAL_BOKSTAVER];
Text minText; // skapar ett objekt
cout <<"Ge en rad med text:" << endl;
getline(cin,text);
cout << "\nResultat för bokstäverna A-Z" << endl;
cout << "Totala antalet bokstäver: " << true << endl;
// Calling constructor
minText.setText( text );
histOK = minText.beraknaHistogramAbs(text,f);
minText.skrivHistogramAbs(alfabet,f );
return 0;
}
//Class implementation
Text::Text()
{
text="";
histOK=true;
}
Text::Text (string nytext, bool nyhistOK,int nyf[])
{
text=nytext;
histOK=nyhistOK;
}
string Text::get_text()
{
return text;
}
bool Text::get_histOK()
{
return histOK;
}
bool Text::beraknaHistogramAbs(const string &str,int hist[])
{
int index, totA=0;
char b;
for (int i=0; i < ANTAL_BOKSTAVER; i++) {
hist[i] = 0;
}
for (int i = 0; i <(int)str.length();i++)
{
b = toupper(str.at(i));
index = b - 'A';
if (isalpha(b))
{ hist[index] +=1;
totA+=1;
}
}
return totA;
}
bool letters (int &totA) // A bool variable is returned that is true if there contains any letters, else false.
{
bool status=true;
if(totA==0){
status=false;
}
return true;
}
void Text::skrivHistogramAbs(string Alfab, int hist[])
{ int i = 0;
cout << "\nLetter Distribution: " << endl;
cout << "Letters" << "\t" << "Amount" << endl;
while (i < ANTAL_BOKSTAVER)
{
cout << Alfab[i] << ":\t" << hist[i] << endl; i++;
}
}
void Text::setText(string nyText)
{
text=nyText;
}
I have multiple classes and each of them has their own methods. All of these methods perform the same task, as you can see in my code. The only unique thing is the values of the title, code and credit members that are defined inside the classes.
Is there a way to write this code such that a single set of methods can do the required tasks (using the specific values within the class that made the request to the method) for each and every class?
I'm a university student, and due to this I don't want to use inheritance since we haven't learned it yet.
class seng305
{
string title = "Software design and architecture", code = "SENG305";
int credit = 4;
public:
seng305();
~seng305();
string get_info();
string get_title();
int get_credit();
};
class comp219
{
string title = "Electronics in computer engineering", code = "COMP219";
int credit = 4;
public:
comp219();
~comp219();
string get_info();
string get_title();
int get_credit();
};
seng305::seng305()
{
cout << '\t' << "Created" << endl;
}
seng305::~seng305()
{
cout << '\t' << "Destroyed" << endl;
}
string seng305::get_info()
{
return (code + "-" + title);
}
string seng305::get_title()
{
return title;
}
int seng305::get_credit()
{
return credit;
}
//--------------------------------------------------
comp219::comp219()
{
cout << '\t' << "Created" << endl;
}
comp219::~comp219()
{
cout << '\t' << "Destroyed" << endl;
}
string comp219::get_info()
{
return (code + "-" + title);
}
string comp219::get_title()
{
return title;
}
int comp219::get_credit()
{
return credit;
}
As you can see, the get_info(), get_title(), and get_credit() methods do the same thing.
I would like for a single get_info(), get_title(), get_credit() to be able to do the task for each class.
There is no reason to use separate classes at all in this example. A single class will suffice, eg:
class course
{
string title, code;
int credit;
public:
course(const string &title, const string &code, int credit);
~course();
string get_info() const;
string get_title() const;
int get_credit() const;
};
course::course(const string &title, const string &code, int credit)
: title(title), code(code), credit(credit)
{
cout << '\t' << "Created" << endl;
}
course::~course()
{
cout << '\t' << "Destroyed" << endl;
}
string course::get_info() const
{
return (code + "-" + title);
}
string course::get_title() const
{
return title;
}
int course::get_credit() const
{
return credit;
}
Then, you simply create instances of your class as needed, eg:
course seng305("Software design and architecture", "SENG305", 4);
course comp219("Electronics in computer engineering", "COMP219", 4);
...
I know you said that you don't want to use inheritance, but that could be the next logical step, using the above code as a base:
class courseSeng305 : public course
{
public:
courseSeng305() : course("Software design and architecture", "SENG305", 4) {}
};
class courseComp219 : public course
{
public:
courseComp219() : course("Electronics in computer engineering", "COMP219", 4) {}
};
courseSeng305 seng305;
courseComp219 comp219;
...
I'm trying to create a simple class called 'Game' and assign some values to all three variables. However every time I run it, the values printed at the screen are completely irrelevant, and I'm pretty sure it has to do something with the class constructors but I don't know what exactly.The code is this:
#include <iostream>
#include <string>
using namespace std;
class Game
{
int id;
string name;
string winner;
public:
Game();
Game(int IDvalue, string NAMEvalue );
~Game();
void setId(int IDvalue);
void setName(string NAMEvalue);
void setWinner(string WINNERvalue);
int getId();
string getName();
string getWinner();
void status1();
};
Game::Game()
{
id = 0;
name = " ";
winner = " ";
}
Game::Game(int IDvalue, string NAMEvalue)
{
IDvalue = id;
NAMEvalue = name;
winner = " ";
}
Game::~Game()
{
}
void Game::setId(int IDvalue)
{
IDvalue = id;
}
void Game::setName(string NAMEvalue)
{
NAMEvalue = name;
}
void Game::setWinner(string WINNERvalue)
{
WINNERvalue = winner;
}
int Game::getId()
{
return id;
}
string Game::getName()
{
return name;
}
string Game::getWinner()
{
return winner;
}
void Game::status1()
{
cout << "Game's id : " << id << endl;
cout << "Game's type : " << name << endl;
cout << "Game's winner : " << winner << endl;
}
int main()
{
Game a(1, "Something");
a.setWinner("Someone");
a.status1();
return 0;
}
As you might have noticed (I'm sure you have) I'm pretty new to C++ ,so go easy on me...
Game::Game(int IDvalue, string NAMEvalue)
{
IDvalue = id;
NAMEvalue = name;
winner = " ";
}
Most of these assignments are backwards. To be consistent, the last line should have been
" " = winner;
This constructor isn't initializing id and name; instead it's assigning the (garbage) values of id and name to the parameters IDvalue and NAMEvalue, which are local variables in the function, so they're destroyed when the constructor returns and no one can see their modified values.
Fix:
Game::Game(int IDvalue, string NAMEvalue)
{
id = IDvalue;
name = NAMEvalue;
winner = " ";
}
Or better:
Game::Game(int IDvalue, string NAMEvalue)
: id(IDvalue)
, name(NAMEvalue)
, winner(" ")
{
}
By the way, your setter functions have the same problem.
You are assigning the member variables to the parameters instead of vice versa on the second constructor and set functions.
Game::Game(int IDvalue, string NAMEvalue)
{
IDvalue = id; --> id = IDvalue; OR setID(id);
NAMEvalue = name; --> name = NAMEvalue; OR setName(name);
winner = " ";
}
void Game::setId(int IDvalue)
{
IDvalue = id; --> id = IDvalue;
}
// ... the rest of the set functions
I have the following code written in C++:
#include<iostream>
#include<vector>
using namespace std;
class cViews {
string viewName;
double minD;
vector<double> dss;
public:
string minInput1, minInput2;
cViews(string);
cViews();
void setName(string s) { viewName = s; }
string getName() { return viewName; }
void setMinI(string m) { minInput1 = m; }
string getMinI() { return minInput1; }
void setMinD(double d) { minD = d; }
double getMinD() { return minD; }
void addD(vector<double> k){ dss = k; }
vector<double> getD(){ return dss; }
};
cViews::cViews(string str) {
viewName = str;
vector<double> dss = vector<double>();
}
cViews::cViews() {
vector<double> dss = vector<double>();
}
class Obj{
string name;
cViews dist;
public:
Obj(string);
void setName(string s) { name = s; }
string getName() { return name; }
void addDist(cViews k){ dist = k; }
cViews getDist(){ return dist; }
};
Obj::Obj(string str) {
name = str;
cViews dist();
}
void changeViewN(cViews *v, string s){
v->setMinI(s);
}
int main(){
Obj o1("Object1");
cViews v3;
cViews v1("View 1");
v1.setMinI("View 2");
v1.setMinD(1);
o1.addDist(v1);
cout << o1.getName() << " " << o1.getDist().getMinI() << endl;
v3 = o1.getDist();
changeViewN(&v3, "Changed");
cout << o1.getName() << " " << o1.getDist().getMinI() << endl;
return 0;
}
Output is:
Object1 View 2
Object1 View 2
The problem here is I am trying to change the value of an object that was created within another object.
Output should be:
Object1 View 2
Object1 Changed
Any help is greatly appreciated. Thank you.
To change the object and not a copy, ou have to use pointers or references. Else you just copy the object returned from getDist() and thus cant change the original object.
cViews* getDist(){ return &dist; }
...
changeViewN(o1.getDist(), "Changed");
It seems you got a couple of problems, the first few:
cViews::cViews(string str) {
vector<double> dss = vector<double>();
}
viewName isn't initialized, dss is Declared in the function (which is meaningless as it will be scrapped as soon as the function returns).
ps. you would like to change the second line like this:
cout << o1.getName() << " " << o1.getDist().getMinI() << endl;
to
cout << o2.getName() << " " << o2.getDist().getMinI() << endl;
You should really proofread your code ...
#include <iostream>
#include <string>
using namespace std;
class BookData
{
string Title;
int Qty;
public:
void setTitle(string in_title) { Title = in_title;}
string setQty(int in_qty) { Qty = in_qty; }
string getTitle() { return Title; }
int getQty() { return Qty; }
};
int main()
{
BookData book;
book.setTitle("Starting Out with C++");
book.setQty(10);
cout << "Title is " << book.getTitle() << ".\n\n";
cout << "Quantity is " << book.getQty() << ".\n\n";
return 0;
}
When I compile all I get is an empty console. Any suggestions?
Change the return type of BookData::setQty() from string to void.
Without this change, it should still work fine. On my Linux machine, it crashes when setQty() is called with a return type of string and no string being returned.
Does the console close immediately? If so, put this before return 0;:
System("PAUSE");
Or:
std::cin.ignore();
Or (for MSVC++2010):
int temp;
std::cin >> temp;