Probleem with substr in c++ - c++

I made binary calculator. It works, but only when numbers doesnt start with 0. if yes, it shows this:
How I should repair the program, please? (vysledek means result)
Part of code with substr:
while(vysledek[0] == '0')
{
vysledek = vysledek.substr(1, vysledek.length());
w++;
}
int aa;
aa = p-w;
for(o; o < aa; o++)
{
cout << vysledek[o];
}
Whole program:`
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
using namespace std;
int main()
{
cout << "Zadejte dve binarni cisla:" << endl;
string cislo1, cislo2;
cin >> cislo1 >> cislo2;
int c = 0;
for (c; c < cislo1.length(); c++)
{
if ((cislo1[c] == '0') || (cislo1[c] == '1'))
{
continue;
}
else
{
cout << "Nespravny vstup.";
return 0;
}
}
int d = 0;
for (d; d < cislo2.length(); d++)
{
if ((cislo2[d] == '0') || (cislo2[d] == '1'))
{
continue;
}
else
{
cout << "Nespravny vstup.";
return 0;
}
}
string stringS2 = "";
for(unsigned i = 0; i < cislo1.length(); i++)
{
stringS2 += cislo1[cislo1.length()-1-i];
}
cislo1 = stringS2;
string stringS3 = "";
for(unsigned i = 0; i < cislo2.length(); i++)
{
stringS3 += cislo2[cislo2.length()-1-i];
}
cislo2 = stringS3;
/*
cout << cislo1 << endl;
cout << cislo2 << endl;
cout << cislo1.length() << endl;
cout << cislo2.length() << endl;
*/
int p;
if (cislo1.length() > cislo2.length())
{
p = cislo1.length();
int f = cislo2.length();
for(f; f < cislo1.length(); f++)
{
cislo2[f] = '0';
}
}
else
{
p = cislo2.length();
int f = cislo1.length();
for(f; f < cislo2.length(); f++)
{
cislo1[f] = '0';
}
}
int e = 0;
//cout << p << endl;
string vysledek;
int t;
t = (p - 1);
cislo1[p];
cislo2[p];
for(e; e < p; e++)
{
/*nasledujici podminky jsou pro pripad, kdy cisla maji odlisny pocet znaku
if(cislo1[e] == NULL)
{
cislo1[e] == '0';
}
if(cislo2[e] == NULL)
{
cislo2[e] == '0';
}*/
if (cislo1[e] == '0' && cislo2[e] == '0')
{
if ((cislo1[e-1] == '1' && cislo2[e-1] == '1')|| ((cislo1[e-1] == '0' && cislo2[e-1] == '1')&& vysledek[t+1] == '0') || ((cislo1[e-1] == '1' && cislo2[e-1] == '0')&& vysledek[t+1] == '0') || ((cislo1[e-1] == '1' && cislo2[e-1] == '1')&& vysledek[t+1] == '1') )
{
vysledek[t] = '1';
}
else
{
vysledek[t] = '0';
}
//cout << vysledek[t] << endl;
t--;
}
else if ((cislo1[e] == '1' && cislo2[e] == '0') || (cislo1[e] == '0' && cislo2[e] == '1') )
{
if ((cislo1[e-1] == '1' && cislo2[e-1] == '1') || ((cislo1[e-1] == '0' && cislo2[e-1] == '1')&& vysledek[t+1] == '0') || ((cislo1[e-1] == '1' && cislo2[e-1] == '0')&& vysledek[t+1] == '0') || ((cislo1[e-1] == '1' && cislo2[e-1] == '1')&& vysledek[t+1] == '1'))
{
vysledek[t] = '0';
}
else
{
vysledek[t] = '1';
}
// cout << vysledek[t] << endl;
t--;
}
else if (cislo1[e] == '1' && cislo2[e] == '1')
{
if ((cislo1[e-1] == '1' && cislo2[e-1] == '1') || ((cislo1[e-1] == '0' && cislo2[e-1] == '1')&& vysledek[t+1] == '0') || ((cislo1[e-1] == '1' && cislo2[e-1] == '0')&& vysledek[t+1] == '0') || ((cislo1[e-1] == '1' && cislo2[e-1] == '1')&& vysledek[t+1] == '1'))
{
vysledek[t] = '1';
}
else
{
vysledek[t] = '0';
}
//cout << vysledek[t] << endl;
t--;
}
}
cout << "Soucet: ";
int u;
u = (p - 1);
if(((cislo1[u] == '0' && cislo2[u] == '1')&& vysledek[0] == '0') || ((cislo1[u] == '1' && cislo2[u] == '0')&& vysledek[0] == '0') || (cislo1[u] == '1' && cislo2[u] == '1') )
cout << '1';
int o = 0;
int z;
int w = 0;
while(vysledek[0] == '0')
{
vysledek = vysledek.substr(1, vysledek.length());
w++;
}
int aa;
aa = p-w;
for(o; o < aa; o++)
{
cout << vysledek[o];
}
return 0;
}
`

string vysledek; creates an empty string. It is never given any other size.
All following uses of vysledek[t] and vysledek.substr are all out of range.

Related

Made an infix to postfix converter and calculator using an array based stack in c++. The calculations don't work. Only returns the top of the stack

My problem is that my evaluatePostfix() function is not calculating operands. It only returns the top of the stack in the converted postfix expression.
So for example, my output is:
Enter a infix expression: 1+4
your postfix expression is:
14+
your result is:
4
Why am I not getting 5?
Here's my main():
int main()
{
string infixExp = "";
cout << "Enter a infix expression: ";
cin >> infixExp;
cout << "your postfix expression is: " << endl;
infixToPostfix(infixExp);
cout << endl;
cout << "your result is: " << endl;
cout << evaluatePostfix(infixExp) << endl;
}
Here's evaluatePostfix():
int evaluatePostfix(string expression)
{
ArrayStack<int> S;
for (int i = 0; i < expression.length(); i++)
{
if (expression[i] == ' ' || expression[i] == ',') continue;
else if(IsOperator(expression[i]))
{
int operand2 = S.peek();
S.pop();
int operand1 = S.peek();
S.pop();
int result = PerformOperation(expression[i], operand1, operand2);
S.push(result);
}
else if(IsNumericDigit(expression[i]))
{
int operand = 0;
while (i < expression.length() && IsNumericDigit(expression[i]))
{
operand = operand * 10 + expression[i] - '0';
i++;
}
i--;
S.push(operand);
}
}
return S.peek();
}
Here's PerformOperation(), IsOperator(), IsNumericDigit(). These are all in evaluatePostfix.
bool IsNumericDigit(char C)
{
if (C >= '0' && C <= '9')
{
return true;
}
else
{
return false;
}
}
bool IsOperator(char C)
{
if (C == '+' || C == '-' || C == '*' || C == '/')
{
return true;
}
else
{
return false;
}
}
int PerformOperation(char operation, int operand1, int operand2)
{
if (operation == '+')
{
return operand1 + operand2;
}
else if (operation == '-')
{
return operand1 - operand2;
}
else if (operation == '*')
{
return operand1 * operand2;
}
else if (operation == '/')
{
return operand1 / operand2;
}
else
{
cout << "error" << endl;
}
return -1;
}
Lastly, here is infixToPostfix():
void infixToPostfix(string s)
{
ArrayStack<char> stackPtr;
string postfixExp;
for (int i = 0; i < s.length(); i++)
{
char ch = s[i];
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9'))
{
postfixExp += ch;
}
else if (ch == '(')
{
stackPtr.push('(');
}
else if (ch == ')')
{
while(stackPtr.peek() != '(')
{
postfixExp += stackPtr.peek();
stackPtr.pop();
}
stackPtr.pop();
}
else
{
while (!stackPtr.isEmpty() && prec(s[i]) <= prec(stackPtr.peek()))
{
postfixExp += stackPtr.peek();
stackPtr.pop();
}
stackPtr.push(ch);
}
}
while (!stackPtr.isEmpty())
{
postfixExp += stackPtr.peek();
stackPtr.pop();
}
cout << postfixExp << endl;
}

Program for NFA to accept strings that has atleast one character occurring in a multiple of 3

So there this c++ code for the following question: NFA to accept strings that have at least one character occurring in a multiple of 3.
The problem is it's showing "accepting" as the output for all inputs.
for example: bbaacc output should be "not accepting" but this code is showing excepting
I am not able to figure out where I went wrong. Can somebody help me find where?
#include <bits/stdc++.h>
int nfa = 1;
int flag = 0;
using namespace std;
void state1(char c)
{
if (c == 'a')
nfa = 2;
else if (c == 'b' || c == 'c')
nfa = 1;
else
flag = 1;
}
void state2(char c)
{
if (c == 'a')
nfa = 3;
else if (c == 'b' || c == 'c')
nfa = 2;
else
flag = 1;
}
void state3(char c)
{
if (c == 'a')
nfa = 1;
else if (c == 'b' || c == 'c')
nfa = 3;
else
flag = 1;
}
void state4(char c)
{
if (c == 'b')
nfa = 5;
else if (c == 'a' || c == 'c')
nfa = 4;
else
flag = 1;
}
void state5(char c)
{
if (c == 'b')
nfa = 6;
else if (c == 'a' || c == 'c')
nfa = 5;
else
flag = 1;
}
void state6(char c)
{
if (c == 'b')
nfa = 4;
else if (c == 'a' || c == 'c')
nfa = 6;
else
flag = 1;
}
void state7(char c)
{
if (c == 'c')
nfa = 8;
else if (c == 'b' || c == 'a')
nfa = 7;
else
flag = 1;
}
void state8(char c)
{
if (c == 'c')
nfa = 9;
else if (c == 'b' || c == 'a')
nfa = 8;
else
flag = 1;
}
void state9(char c)
{
if (c == 'c')
nfa = 7;
else if (c == 'b' || c == 'a')
nfa = 9;
else
flag = 1;
}
bool checkA(string s, int x)
{
for (int i = 0; i < x; i++) {
if (nfa == 1)
state1(s[i]);
else if (nfa == 2)
state2(s[i]);
else if (nfa == 3)
state3(s[i]);
}
if (nfa == 1) {
return true;
}
else {
nfa = 4;
}
}
bool checkB(string s, int x)
{
for (int i = 0; i < x; i++) {
if (nfa == 4)
state4(s[i]);
else if (nfa == 5)
state5(s[i]);
else if (nfa == 6)
state6(s[i]);
}
if (nfa == 4) {
return true;
}
else {
nfa = 7;
}
}
bool checkC(string s, int x)
{
for (int i = 0; i < x; i++) {
if (nfa == 7)
state7(s[i]);
else if (nfa == 8)
state8(s[i]);
else if (nfa == 9)
state9(s[i]);
}
if (nfa == 7) {
return true;
}
}
int main()
{
string s = "bbbca";
int x = 5;
if (checkA(s, x) || checkB(s, x) || checkC(s, x)) {
cout << "ACCEPTED";
}
else {
if (flag == 0) {
cout << "NOT ACCEPTED";
return 0;
}
else {
cout << "INPUT OUT OF DICTIONARY.";
return 0;
}
}
}

How do I loop through 2d arrays in c++?

I am trying to figure out how to loop through a 2d array horizontally vertically, diagonally and sub diagonally to see if the array is equal to a char.if the array is equal to the char then a bool should return true, but my problem is the bool randomly returns true. the bool only returns true if the char is equal to array at a certain column and row. Here i am hard coding it in but i want to do it with for loops.
Let me know if you need any more info or if something needs to be explained
if (mCard[0][0] == '*' && mCard[0][1] == '*'&& mCard[0][2] == '*'&& mCard[0][3] == '*' && mCard[0][4] == '*') {
CheckBingo = true;
}
if (mCard[1][0] == '*' && mCard[1][1] == '*'&& mCard[1][2] == '*'&& mCard[1][3] == '*' && mCard[1][4] == '*') {
CheckBingo = true;
}
if (mCard[2][0] == '*' && mCard[2][1] == '*'&& mCard[2][2] == '*'&& mCard[2][3] == '*' && mCard[2][4] == '*') {
CheckBingo = true;
}
if (mCard[3][0] == '*' && mCard[3][1] == '*'&& mCard[3][2] == '*'&& mCard[3][3] == '*' && mCard[3][4] == '*') {
CheckBingo = true;
}
if (mCard[4][0] == '*' && mCard[4][1] == '*'&& mCard[4][2] == '*'&& mCard[4][3] == '*' && mCard[4][4] == '*') {
CheckBingo = true;
}
if (mCard[0][0] == '*' && mCard[1][0] == '*'&& mCard[2][0] == '*'&& mCard[3][0] == '*' && mCard[4][0] == '*') {
CheckBingo = true;
}
if (mCard[0][1] == '*' && mCard[1][1] == '*'&& mCard[2][1] == '*'&& mCard[3][1] == '*' && mCard[4][1] == '*') {
CheckBingo = true;
}
if (mCard[0][2] == '*' && mCard[1][2] == '*'&& mCard[2][2] == '*'&& mCard[3][2] == '*' && mCard[4][2] == '*') {
CheckBingo = true;
}
if (mCard[0][3] == '*' && mCard[1][3] == '*'&& mCard[2][3] == '*'&& mCard[3][3] == '*' && mCard[4][3] == '*') {
CheckBingo = true;
}
if (mCard[0][4] == '*' && mCard[1][4] == '*'&& mCard[2][4] == '*'&& mCard[3][4] == '*' && mCard[4][4] == '*') {
CheckBingo = true;
}
// checking for daigonal
if (mCard[0][0] == '*' && mCard[1][1] == '*'&& mCard[2][2] == '*'&& mCard[3][3] == '*' && mCard[4][4] == '*') {
CheckBingo = true;
}
// checking for sub diagonal
if (mCard[4][0] == '*' && mCard[3][1] == '*'&& mCard[2][2] == '*'&& mCard[1][3] == '*' && mCard[0][4] == '*') {
CheckBingo = true;
}
here I tried using for loops but it does int always return true if the array is equal to the char.
for(int row = 0; row < 5; row++) {
for(int col = 0; col < 5; col++) {
if(mCard[row][col] == '*') {
CheckBingo = true;
}
else {
CheckBingo = false;
break;
}
}
}
for(int row = 0; row < 5; row++) {
for(int col = 0; col < 5; col++) {
if(mCard[col][row] == '*') {
CheckBingo = true;
}
else {
CheckBingo = false;
break;
}
}
}
mCard[col][row]
...
row and col have to be flipped.
You then start the loop assuming that match is set. If one of the cells is not matched, then match is false. See example below.
Make sure the values are initialized.
int main()
{
char mCard[5][5] = {
'*','x','*','*','*',
'*','*','*','*','*',
'*','*','*','*','*',
'*','*','x','*','*',
'*','*','*','*','*',
};
bool CheckBingo = true;
for(int row = 0; row < 5; row++)
{
CheckBingo = true;
for(int col = 0; col < 5 && CheckBingo; col++)
{
if(mCard[row][col] != '*')
CheckBingo = false;
}
if(CheckBingo)
cout << "bingo on row " << row << "\n";
}
bool match = true;
for(int row = 0; row < 5 && match; row++)
for(int col = 0; col < 5 && match; col++)
if(mCard[row][col] != '*')
match = false;
if(match) cout << "bingo for all lines\n";
match = true;
for(int i = 0; i < 5 && match; i++)
if(mCard[i][i] != '*')
match = false;
if(match) cout << "top-left to bottom-right match\n";
match = true;
for(int i = 0; i < 5 && match; i++)
if(mCard[i][4-i] != '*')
match = false;
if(match) cout << "top-right to bottom-left match\n";
return 0;
}

game does not allow you to win

I'm writing the code for a game with C++, and when a player is supposed to win, for example all x's on the top row, the game keeps on prompting the next player for their move even when there is supposed to already be a winner. I don't know what's wrong with it, so any help in catching the problem is appreciated! Below is my full code.
#include <iostream>
#include <string>
using namespace std;
enum status{X_WON, O_WON, DRAW, UNFINISHED};
class Board{
public:
char boardArray[3][3];
Board();
bool makeMove(int, int, char);
status gameState();
void print();
};
Board::Board(){
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
boardArray[i][j] = '*';
};
};
};
bool Board::makeMove(int xIn, int yIn, char player){
if (boardArray[xIn][yIn] == '*'){
boardArray[xIn][yIn] = player;
return true;
}else{
return false;
};
};
status Board::gameState(){
status result;
if(boardArray[0][0] == 'x' && boardArray[0][1] == 'x' && boardArray[0] [2] == 'x'){
result = X_WON;
};
if(boardArray[0][0] == 'o' && boardArray[0][1] == 'o' && boardArray[0][2] == 'o'){
result = O_WON;
};
if(boardArray[1][0] == 'x' && boardArray[1][1] == 'x' && boardArray[1][2] == 'x'){
result = X_WON;
};
if(boardArray[1][0] == 'o' && boardArray[1][1] == 'o' && boardArray[1][2] == 'o'){
result = O_WON;
};
if(boardArray[2][0] == 'x' && boardArray[2][1] == 'x' && boardArray[2][2] == 'x'){
result = X_WON;
};
if(boardArray[2][0] == 'o' && boardArray[2][1] == 'o' && boardArray[2][2] == 'o'){
result = O_WON;
};
if(boardArray[0][0] == 'x' && boardArray[1][0] == 'x' && boardArray[2][0] == 'x'){
result = X_WON;
};
if(boardArray[0][0] == 'o' && boardArray[1][0] == 'o' && boardArray[2][0] == 'o'){
result = O_WON;
};
if(boardArray[0][1] == 'x' && boardArray[1][1] == 'x' && boardArray[2][1] == 'x'){
result = X_WON;
};
if(boardArray[0][1] == 'o' && boardArray[1][1] == 'o' && boardArray[2][1] == 'o'){
result = O_WON;
};
if(boardArray[0][2] == 'x' && boardArray[1][2] == 'x' && boardArray[2][2] == 'x'){
result = X_WON;
};
if(boardArray[0][2] == 'o' && boardArray[1][2] == 'o' && boardArray[2][2] == 'o'){
result = O_WON;
};
if(boardArray[0][0] == 'x' && boardArray[1][1] == 'x' && boardArray[2][2] == 'x'){
result = X_WON;
};
if(boardArray[0][0] == 'o' && boardArray[1][1] == 'o' && boardArray[2][2] == 'o'){
result = O_WON;
};
if(boardArray[0][2] == 'x' && boardArray[1][1] == 'x' && boardArray[2][0] == 'x'){
result = X_WON;
};
if(boardArray[0][2] == 'o' && boardArray[1][1] == 'o' && boardArray[2][0] == 'o'){
result = O_WON;
};
int taken = 0;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(boardArray[i][j] != '*'){
taken++;
};
};
};
if(taken == 9 && (result != X_WON || result != O_WON)){
result = DRAW;
};
if(taken != 9 && (result != X_WON || result != O_WON || result != DRAW)){
result = UNFINISHED;
};
return result;
};
void Board::print(){
cout << " " << "0 " << "1 " << "2" << endl;
for(int i = 0; i < 3; i++){
cout << i << " ";
for(int j = 0; j < 3; j++){
cout << boardArray[i][j] << " ";
};
cout << endl;
};
};
class Ttt {
private:
char player;
public:
Board newBoard;
Ttt();
void play();
};
Ttt::Ttt(){
do{
cout << "Who should have the first move? x or o?" << endl;
cin >> player;
if (player == 'x' || player == 'o'){
break;
}else{
cout << "Not a valid player. Try again." << endl;
};
}while(true);
};
void Ttt::play(){
do{
int xcoord, ycoord;
do{
cout << "Player " << player << " , please enter your move. Example: 0 [enter] 0 (numbers from 0 - 2)" << endl;
cin >> xcoord >> ycoord;
if(newBoard.makeMove(xcoord, ycoord, player) == false){
cout << "That move is not valid. Try again." << endl;
}else{
break;
};
}while(true);
newBoard.makeMove(xcoord, ycoord, player);
newBoard.print();
if(player == 'x'){
player = 'o';
}else{
player = 'x';
};
if(newBoard.gameState() == X_WON){
cout << "Player X is the winner!" << endl;
break;
};
if(newBoard.gameState() == O_WON){
cout << "Player O is the winner!" << endl;
break;
};
if(newBoard.gameState() == DRAW){
cout << "Draw!" << endl;
break;
};
}while(true);
};
int main(){
Ttt newgame;
newgame.play();
}
The test
(result != X_WON || result != O_WON || result != DRAW)
is always true, since result can't be equal to all 3 values.
result should be initialised to UNFINISHED and the test changed to:
(taken != 9 && result == UNFINISHED)

How to ignore certain letters and spaces in character arrays

Trying to make an else statement that get rid of all other letter and spaces then the ones i want. This function is to change user inputted letters into other letters
using namespace std;
void dna_to_rna(char rna[])
{
for (int i = 0; i < 100; i++)
{
if (rna[i] == 'a' || rna[i] == 'A')
rna[i] = 'U';
else if (rna[i] == 'c' || rna[i] == 'C')
rna[i] = 'G';
else if (rna[i] == 'g' || rna[i] == 'G')
rna[i] = 'C';
else if (rna[i] == 't' || rna[i] == 'T')
rna[i] = 'A';
}
What should the else statement look like in order to drop all other chars?
If the input parameter can be changed to std::string, then you can use one of the following implementation:
void dna_to_rna(std::string& rna)
{
auto it = rna.begin();
while (it != rna.end())
{
if (*it == 'a' || *it == 'A') *it = 'U';
else if (*it == 'c' || *it == 'C') *it = 'G';
else if (*it == 'g' || *it == 'G') *it = 'C';
else if (*it == 't' || *it == 'T') *it = 'A';
else
{
it = rna.erase(it);
continue; // it already "points" to the next element
}
++it;
}
}
std::string dna_to_rna(const std::string& dna)
{
std::string rna;
for (auto c : dna)
{
if (c == 'a' || c == 'A') rna += 'U';
else if (c == 'c' || c == 'C') rna += 'G';
else if (c == 'g' || c == 'G') rna += 'C';
else if (c == 't' || c == 'T') rna += 'A';
}
return rna;
}
Maybe like this:
using namespace std;
void dna_to_rna(char rna[])
{
string s = "";
for (int i = 0; i < 100; i++)
{
if (rna[i] == 'a' || rna[i] == 'A')
s += 'U';
else if (rna[i] == 'c' || rna[i] == 'C')
s += 'G';
else if (rna[i] == 'g' || rna[i] == 'G')
s += 'C';
else if (rna[i] == 't' || rna[i] == 'T')
s += 'A';
}
strcpy(rna, s.c_str());
}
The idea is simply to use a std::string as a temporary buffer. The string is empty to start with. Then you add the characters you want one-by-one. When done with the loop, copy the content of the std::string back to the rna-array.
To make you code much simpler, and easier to read:
using namespace std;
void dna_to_rna(char rna[]) {
int arrLength = sizeof(rna)/sizeof(rna[0]); // Get size of array
for (int i = 0; i < arrLength; i++){
if (toupper(rna[i]) == 'A'){
rna[i] = 'U';
}
else if (toupper(rna[i]) == 'C') {
rna[i] = 'G';
}
else if (toupper(rna[i]) == 'G'){
rna[i] = 'C';
}
else if (toupper(rna[i]) == 'T'){
rna[i] = 'A';
}
}
}
I created a second array and as long as the information that I was looking for met the criteria that was necessary I placed it into the second array making sure that the position that I was placing it in the array was always in the right spot by creating a second variable that would count the the proper position in the array then just cout array
using namespace std;
void dna_to_rna(char rna[])
{
int x = 0;
char newrna[100];
for (int i = 0; i < 100; i++)
{
if (rna[i] == 'a' || rna[i] == 'A')
{
newrna[x] = 'U';
x++;
}
else if (rna[i] == 'c' || rna[i] == 'C')
{
newrna[x] = 'G';
x++;
}
else if (rna[i] == 'g' || rna[i] == 'G')
{
newrna[x] = 'C';
x++;
}
else if (rna[i] == 't' || rna[i] == 'T')
{
newrna[x] = 'A';
x++;
}
}