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;
}
At the beginning I apologize for my English.
I was trying to write a XML Parser that I encountered a weird problem.
to explain my problem I should say, I have a xml parser class that has an ifstream member. And this class has a function which reads until it reaches an open tag matching with the given input.
this is the parser class I was working on:
// XMLParser.cpp
#include <fstream>
#include "Stack.h"
using namespace std;
class XMLParser{
private:
int charReadRate = 3;
public:
ifstream *stream;
XMLParser(string add){
stream = new ifstream(add); // open input stream
}
void nextTag(string tag){
// find the first occurance of open-tag with name 'tag'
cout << "nextTag\n";
char * readData;
string tagName="";
stream->read(readData, charReadRate);
int len = string(readData).length();
int i = 0;
// cout << len << endl;
while(true){
if((*readData) == '<'){
readData++;
i++;
while(*readData != '>'){
tagName+=*readData;
readData++;
i++;
if(i>=len){
if(stream->eof()){
return ; // error didn't find
}
stream->read(readData, charReadRate);
// cout << readData << endl;
len = string(readData).length();
i = 0;
}else{
if(tagName == tag){
// cout << "find\n";
stream->seekg(i-len, ios::cur);
return;
}
}
}
}else{
readData++;
i++;
if(i>=len){
if(stream->eof()){
return ; // error didn't find
}
stream->read(readData, charReadRate);
len = string(readData).length();
i = 0;
}
}
}
}
};
in the nextTag function I read the file until I reach the open tag which name's matches with the given input.
and here is my main function
int main(){
XMLParser parser("test.xml");
cout << "ready\n";
parser.nextTag("Log");
char *c;
parser.stream->read(c,3);
cout << c << endl;
return 0;
}
I have figured out that the program crashes when the fifth line of the main function [parser.stream->read(c,3);] is executed.
I wonder why this happens?
The char pointer you pass to ifstream::read is not initialized and thus points to an invalid memory region, causing your program to crash. You need it to point to a buffer you allocated:
int main(){
XMLParser parser("test.xml");
cout << "ready\n";
parser.nextTag("Log");
char c[3];
parser.stream->read(c,3);
cout << c << endl;
return 0;
}
I've been trying to figure this out for hours now, and I'm at my wit's end. I would surely appreciate it if someone could tell me when I'm doing wrong.
I wrote a c++ code with class implementing a simple stack, trying to push and pop random stream of characters. It seems to work fine, but at the end of the file, it produces some sort of runtime error:
HEAP CORRUPTION DETECTED: after Normal block....
Since the error occurs at the end of the file, my guess is that there is a problem at deleting the pointer(class destructor). However, I have no idea what is wrong with the destructor I wrote.
Also, after some trial and error, I found out that if I address a bigger number to unsigned integer value iter1 (ex: 80), the runtime error does not occur. Could you explain what is the problem here, and how to bypass it?
stack.h:
class sstack
{
public:
sstack(int length = 256);
~sstack(void);
int sstackPop(char &c);
int sstackPush(char c);
bool isempty();
bool isFull();
protected:
private:
char *sstackBuffer;
int sstackSize;
int sstackIndex; // Initial = -1
};
stack.cpp:
#include "stack.h"
#include <iostream>
using namespace std;
sstack::sstack(int length)
{
sstackIndex = -1;
if (length > 0)
sstackSize = length;
else
sstackSize = 256;
sstackBuffer = new char[sstackSize];
}
sstack::~sstack(void)
{
delete[] sstackBuffer;
}
bool sstack::isempty()
{
if (sstackIndex < 0)
{
cout << "is empty!(isempty)" << endl;
return 1;
}
else
return 0;
}
bool sstack::isFull()
{
if (sstackIndex >= sstackSize)
return 1;
else
return 0;
}
int sstack::sstackPop(char &c)
{
if (!isempty())
{
c = sstackBuffer[sstackIndex--];
cout << sstackIndex << endl;
return 1;
}
else
{
cout << "is empty!(sstackPop)" << endl;
return 0;
}
}
int sstack::sstackPush(char c)
{
if (!isFull())
{
sstackBuffer[++sstackIndex] = c;
return 1;
}
else{
return 0;
}
}
main.cpp:
#include <iostream>
#include "stack.h"
#include <string>
using namespace std;
int main(){
unsigned int iter1 = 5;
unsigned int iter2 = 800;
sstack stackDefault;
sstack stack1(iter1);
sstack stack2(iter2);
char buffer[80];
memset(buffer, 0x00, 80);
char BUFFER[80] = "A random stream of characters";
strcpy_s(buffer, 80, BUFFER);
for (int i = 0; i< strlen(buffer); i++)
{
cout << " stack1: " << stack1.sstackPush(buffer[i]);
cout << " stack2: " << stack2.sstackPush(buffer[i]);
cout << " stackD: " << stackDefault.sstackPush(buffer[i]);
cout << " i : "<< i << endl;
}
cout << "out of Pushes" << endl;
int i = 0;
memset(buffer, 0x00, 80);
while (!stack1.isempty())
stack1.sstackPop(buffer[i++]);
cout << buffer << endl;
getchar();
}
sstackBuffer[++sstackIndex] = c;
Will write past the end of sstackBuffer when the stack only has one element left.
If you consider a stack of size 1. In the first call to push that line would evaluate to:
sstackBuffer[1] = c;
Which is beyond the memory you've allocated.
Be sure you're aware of the difference between pre-increment and post-increment operators. By your code example I would suggest you use post-increment in push and pre-increment in pop.
I am needing help with polymorphism. I have no clue how to work with this. I have to write a program that creates an Orc and a Human and demonstrates the behavior of the classes. I am just needing some help with setting this program up. I have set up the classes with the information, but how do I get the createCharacter function to work? Would I have characterTotal = characterStrength + characterDexterity + characterIntelligence in the createCharacter function? I know my program is not correct right now and I have some errors and things, but I am still just trying to get a better understanding of this.
UPDATE:
I am having trouble with the createCharacter function. It is a pure virtual function and I am needing some help on how to get it to work.
For Human class createCharacter will:
Get the values of STR, DEX and INT. Will calculate the total of the values.
(Let’s assume STR = 17, DEX = 12 and INT = 10. It will store 37 into characterTotal.Itwill print out a message: “The strong human Paladin, has a total scoreof 37.” (Strong adjective comes due to STR being 17. If something is above 17 you should say something related. STR = strong, DEX = dexterous, INT =
intelligent).
For Orc class createCharacter will:
Get the values of STR, DEX and INT. Will calculate the total of the values.
However Orcs receive -2 to INT and DEX. They receive +2 to STR. (Let’s
assume STR = 16, DEX = 10 and INT = 8. It will store 16+2,10-2,8-2 = 28 into
characterTotal.
It will print out a message “The berserker Orc has a total score of 28.” (Here the
Orcs get their adjectives from their clan names so you do not need to do
something specific to STR, DEX or INT.)
CODE:
//character.h
#ifndef CHARACTER_H
#define CHARACTER_H
using namespace std;
class Character
{
protected:
float characterTotal;
public:
virtual void createCharacter() = 0; //Pure virtual function
};
#endif
//human.h
#ifndef HUMAN_H
#define HUMAN_H
#include "Character.h"
using namespace std;
class Human
{
private:
int characterStrength;
int characterDexterity;
int characterIntelligence;
string characterType;
public:
Human();//Constructor
int getStength
{
cout << "Enter a number from 0 to 18\n";
cin >> characterStrength;
return characterStrength;
}
int getDexterity
{
cout << "Enter a number from 0 to 18\n";
cin >> characterDexterity;
return CharacterDexterity;
}
int getIntelligence
{
cout << "Enter a number from 0 to 18\n";
cin >> characterIntelligence;
return characterIntelligence;
}
string getType
{
cout << "Please choose one of the following\n";
cout << "A -- Paladin \n";
cout << "B -- Ranger \n";
cout << "C -- Wizard \n";\
cin >> characterType;
return characterType;
}
};
#endif
//orc.h
#ifndef ORC_H
#define ORC_H
#include "Character.h"
#include "Human.h"
using namespace std;
class orc
{
private:
int characterStrength;
int characterDexterity;
int characterIntelligence;
string characterClan;
public:
orc(); //Constructor
int getStrength
{
cout << "Enter a number between 0 to 18\n";
cin >> characterStrength;
return characterStrength;
}
int getDexterity
{
cout << "Enter a number between 0 to 18\n";
cin >> characterDexterity;
return characterDexterity;
}
int getIntelligence
{
cout << "Enter a number between 0 to 18\n";
cin >> characterIntelligence;
return characterIntelligence;
}
string getClan
{
cout << "Please choose one of the following\n";
cout << "A -- Barbarian \n";
cout << "B -- Berserker \n";
cout << "C -- Vanguard \n";\
cin >> characterClan;
return characterClan;
}
};
#endif
Example: A class called Account posesses a container with Character. Your classes Human and Orc inherit both from Character. Inside of your Human class you may want to set spell ABC to all characters which are created (in createCharacter). But if the player creates an Orc, you might want to set another spell XYZ to it, instead of ABC. Base classes come in handy here. In this example you see one of these abstract functions in action (Character::initCharacter).
You can put in more abstract functions inside of Character IF the classes which are supposed to inherit from Character MUST / NEED TO implement these functions.
This is only an example. If you want to do it properly, you need more than this and would have to modify all for database access and further abstraction.
enum CharacterType
{
CHAR_INVALID = 0x0,
CHAR_HUMAN = 0x1,
CHAR_ORC = 0x4
};
class Character
{
public:
Character(unsigned int charGUID) //You might want to set data in the constructor already
: m_charGUID(charGUID) //right after it has been load from Account::LoadAccountInformation()
{
std::cout << "Constructor of \"Character\"" << std::endl;
}
virtual ~Character(void) //Cleanup if needed
{
std::cout << "Destructor of \"Character\"" << std::endl;
}
virtual void createCharacter(void) = 0;
virtual void initCharacter(void) = 0;
CharacterType GetTypeID(void)
{ return m_typeID; }
protected:
void SetTypeID(CharacterType ct)
{ m_typeID = ct; }
private:
Character(const Character &);
unsigned int m_charGUID;
CharacterType m_typeID;
};
class Human : public Character
{
public:
Human(unsigned int charGUID)
: Character(charGUID)
{
SetTypeID(CHAR_HUMAN);
std::cout << "Constructor of \"Human\"" << std::endl;
}
virtual ~Human(void) //Cleanup if needed
{
std::cout << "Destructor of \"Human\"" << std::endl;
}
void createCharacter(void) override
{
//Set data...
}
void initCharacter(void) override
{
std::cout << "You initialized a character of type \"Human\"" << std::endl;
}
};
class Orc : public Character
{
public:
Orc(unsigned int charGUID)
: Character(charGUID)
{
SetTypeID(CHAR_ORC);
std::cout << "Constructor of \"Orc\"" << std::endl;
}
virtual ~Orc(void) //Cleanup if needed
{
std::cout << "Destructor of \"Orc\"" << std::endl;
}
void createCharacter(void) override
{
//Set data...
}
void initCharacter(void) override
{
std::cout << "You initialized a character of type \"Orc\"" << std::endl;
}
};
class Account
{
public:
Account(unsigned int accountGUID)
{ m_accGUID = accountGUID; }
//#Return: False if load from database failed
bool LoadAccountInformation(void) //You could give it data also
{
//You could also load data directly from a database here if you'd like to
//Here are just some sample values (partially hardcoded)
characters.clear();
const int charsOnAccount = 1; //Load from database
for (int i = 0; i < charsOnAccount; ++i)
{
CharacterType ct = CHAR_HUMAN; //Load from database
unsigned int characterGUID = i;
switch (ct)
{
case CHAR_HUMAN:
{
characters[characterGUID] = std::move(std::shared_ptr<Character>(new Human(characterGUID)));
} break;
case CHAR_ORC:
{
characters[characterGUID] = std::move(std::shared_ptr<Character>(new Orc(characterGUID)));
} break;
default:
{
std::cout << "Invalid character type: " << ct << std::endl; //Or log to file
} break;
}
}
return true;
}
void InitCharacters(void)
{
for (auto itr = std::begin(characters); itr != std::end(characters); ++itr)
itr->second->initCharacter();
}
private:
//A unique account-GUID
unsigned int m_accGUID;
//Let's say a unique char-GUID and the Character object
std::map<unsigned int, std::shared_ptr<Character> > characters;
//And more information...
};
int main(int argc, char *argv[])
{
Account ac = Account(1);
ac.LoadAccountInformation();
ac.InitCharacters();
return 0;
}
I am trying to test this simple function, but Opt.status, Opt.Year values are not returned back to main(). Why? Please help as I am new to C++.I am using visual c++ to execute these codes.This is in my .cpp file
#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include "stdio.h"
using namespace std;
int main()
{
Easy_Task obj_EasyTask;
TimeDateMonthOptions whatOptions=DATE;
TOptions Opt;
Opt.status=FALSE;
Opt.Year=1970;
printf("Enter code\n");
scanf("%d",&obj_EasyTask.code);
cout << "the code entered is: " << obj_EasyTask.code;
obj_EasyTask.display2(obj_EasyTask.code);
cout << "\nOutput: " << obj_EasyTask.show();
printf("\nEnter the options that you prefer\n");
scanf("%d",&whatOptions);
obj_EasyTask.display3(whatOptions, Opt);
cout << "\nOpt.Year: " << Opt.Year;
if(Opt.status)
{
obj_EasyTask.x=(Opt.Year)& 0x00FF;
obj_EasyTask.y=((Opt.Year)& 0xFF00)>>8;
cout << "\nX: " << obj_EasyTask.x;
cout << "\nY: " << obj_EasyTask.y;
obj_EasyTask.Result=(obj_EasyTask.x)*(obj_EasyTask.y);
}
char holdWindow;
std::cin >> holdWindow;
return 0;
}
uint16_t Easy_Task::display2(uint16_t code)
{
if(code==1)
{
c = 7;
}
else
{
c = 9;
}
return c;
}
uint16_t Easy_Task::display3(TimeDateMonthOptions whtOptions, TOptions Opt)
{
switch(whtOptions)
{
case 0:
case 1:
case 2:
case 3:
Opt.status=TRUE;
cout << "\nStatus1: " << Opt.status;
Opt.Year=1991;
cout << "\nYear1: " << Opt.Year;
break;
case 7:
Opt.status=FALSE;
cout << "\nStatus2: " << Opt.status;
Opt.Year=2013;
cout << "\nYear2: " << Opt.Year;
break;
default:
Opt.status=FALSE;
cout << "\nStatus3: " << Opt.status;
Opt.Year=2010;
cout << "\nYear3: " << Opt.Year;
break;
}
return Opt.status, Opt.Year;
}
In my .h file I have the class defined as follows:
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
typedef unsigned short uint16_t;
#define TRUE 1;
#define FALSE 0;
typedef struct TOptions
{
bool status;
uint16_t Year;
};
typedef enum
{
YEAR,
MONTH,
DATE,
HOURS,
MINUTES,
SECONDS,
HUNDRETHS,
UNDEFINED
}TimeDateMonthOptions;
class Easy_Task
{
public:
uint16_t code, c, x,y, Result;
uint16_t display2(uint16_t code);
uint16_t show()
{
return c;
};
uint16_t display3(TimeDateMonthOptions whatOptions, TOptions Opt);
};
The problem I have is line:
if(Opt.status)
Where it doesn't return the value of 1 but instead in takes the default value which was defined earlier. Why is this happening?
You need to pass the argument by reference:
uint16_t Easy_Task::display3(TimeDateMonthOptions whtOptions, TOptions& Opt)
//^
Otherwise, a copy of Opt is made and modified in the function and the caller will never see the changes.
Note that:
return Opt.status, Opt.Year;
does not somehow return two values. This is using the comma operator and will return the value Opt.Year. However, if you pass Opt by reference a return value is unrequired.
Better still define a function to return TOptions
TOptions Easy_Task::display3(...); and return a structure.
Remember that you can only ever return ONE SINGLE return value from the function.