I am trying to make an infix calculator for which I am currently trying to convert numbers entered in a character array to double.
here's my code:
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
char exp[500];
const int SIZE = 100;
char temp[SIZE];
char op;
int strLen = 0, k, l, num = 0, fnum = 0;
double number = 0;
cin.getline(exp, 500,'\n');
int i = 0, j = 0, fpoint=0;
cout << exp;
for (i = 0, j = 0; exp[j] != 0; i++)
{
if (i % 2 == 0)
{
for (int m = 0; exp[m] != ','; m++) //stopped working
temp[m] = exp[m];
cout << temp;
for (k = 0; k < SIZE && temp[k] != 0; k++)
{
strLen = k;
if (temp[k] == '.')
fpoint = k + 1;
}
cout << fpoint<<endl;
cout << "strLen" << strLen;
for (k = 0; k <= fpoint; k++)
{
num = num + ((temp[fpoint - k] - '0') * pow(10, k));
}
for (k = fpoint + 1, l = 0; k <= strLen; k++, l++)
{
fnum = fnum + ((temp[strLen - l] - '0') * pow(10, l));
}
number = num + (fnum / pow(10, strLen - fpoint + 1));
cout << number;
j = j + strLen + 1;
}
else
{
char op = temp[j];
cout << op;
}
}
system("pause");
return 0;
}
sample input
2.5*3
It stops working and gives segmentation fault as an error on the marked position.
This line for (int m = 0; exp[m] != ','; m++) //stopped working will always fail if there are no , characters since exp[m] != ',' will always be equal to true and so will reach beyond the end of the array of exp which triggers the "segmentation fault".
I'm doing some C++ array homework. The goals is to convert decimal to binary (include negative numbers). Here's my code, it gets the job done, but I would like to see if anything can be improved, or any better algorithm (using binary shift maybe?).
#include <iostream>
using namespace std;
// doi tu thap phan sang nhi phan
void decToBinary(int n, int nhiphan[])
{
for (int i=0; i < 16; i++)
{
// to binary
nhiphan[i] = n % 2;
n = n / 2;
}
// inverse array
for (int i = 0, j = 15; i < j; i++, j--)
{
int temp = nhiphan[i];
nhiphan[i] = nhiphan[j];
nhiphan[j] = temp;
}
}
void reverse(int& a)
{
if (a == 0)
a++;
else a--;
}
void outArr(const int a[], int size) {
for (int i = 0; i < size; ++i)
cout << a[i];
}
int main()
{
int nhiphan[16];
int n;
do {
cout << "Nhap so (-255 <= n <= 255) chuyen doi sang nhi phan (16 bit): ";
cin >> n;
} while (n > 255 || n < -255);
if (n < 0) {//check negative
n *= -1;
decToBinary(n, nhiphan);
for (int i = 0; i < 16; i++)// 1's complement
reverse(nhiphan[i]);
// +1
if (nhiphan[15] == 0)//2's complement
nhiphan[15] = 1;
else
{
nhiphan[15] = 0;
int i = 15;
do {
reverse(nhiphan[i-1]);
i--;
} while (nhiphan[i-1] == 0);
}
}
else decToBinary(n, nhiphan);
outArr(nhiphan, 16);
return 0;
}
I have an assignment for school where I need to create a lottery program. It is supposed to allow the user to input six numbers and then generate six random numbers for comparison. I got the inputs working, but I have encountered a problem where the random number generator (located in the while loop) is stuck in an infinite loop, and I have absolutely no idea what is causing it since I have never had an infinite loop in any previous programs. If someone could please look through the code and possibly establish what is wrong, I would greatly appreciate it.
#include<iostream>
#include<time.h>
using namespace std;
void randomizeSeed();
int randomRange(int min, int max);
int getInteger();
int main()
{
randomizeSeed();
const int minNumber = 1;
const int maxNumber = 49;
const int Size = 6;
int luckyNumbers[6] = {};
int randomNumber = randomRange(minNumber, maxNumber);
int winningNumbers[6] = {};
cout << "Enter six numbers between 1 and 49...\n";
{
for (int i = 0; i < Size; i++)
{
luckyNumbers[i] = getInteger();
}
for (int i = 0; i < Size; i++)
{
for (int i = 0; i < Size - 1; i++)
{
if (luckyNumbers[i] > luckyNumbers[i + 1])
{
int temp = luckyNumbers[i];
luckyNumbers[i] = luckyNumbers[i + 1];
luckyNumbers[i + 1] = temp;
}
}
}
cout << "Lucky Numbers: ";
for (int i = 0; i < Size; i++)
{
cout << luckyNumbers[i] << " ";
}
cout << "\n";
cout << "Press any button to see the Winning Numbers.\n";
system("pause");
bool exist = true;
while (exist == true)
{
int count = 0;
cout << "Winning Numbers: ";
for (int j = 0; j < 6; j++)
{
winningNumbers[j] = randomRange(1, 49);
cout << winningNumbers[j] << " ";
system("pause");
}
}
}
}
void randomizeSeed()
{
srand(time(NULL));
}
int randomRange(int min, int max)
{
int randomValue = rand() % (max + 1 - min) + min;
return randomValue;
}
int getInteger()
{
int value = 0;
while (!(cin >> value) || (value >= 50) || (value <= 0))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
return value;
}
for (int i = 0; i < Size; i++)
for (int i = 0; i < Size - 1; i++)
if (luckyNumbers[i] > luckyNumbers[i + 1])
{
int temp = luckyNumbers[i];
luckyNumbers[i] = luckyNumbers[i + 1];
luckyNumbers[i + 1] = temp;
}
You have two loops and they both use i. You probably mean to use the second loop with another variable name, for example:
for (int i = 0; i < Size; i++)
{
for (int k = 0; k < Size - 1; k++)
{
if (luckyNumbers[i] > luckyNumbers[k + 1])
{
int temp = luckyNumbers[i];
luckyNumbers[i] = luckyNumbers[k + 1];
luckyNumbers[k + 1] = temp;
}
}
}
If you set your compiler warning level to 4 then compiler should warn you about these errors. Try to resolve all compiler warnings.
I want to create a big number with 128 bit.
When I convert the string decimal to binary and set bit to the new data QInt :__int64 a[2], it only true for a small number (about 10 digits).
This is my code: http://codepad.org/HmYqMQme
#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
//new data
class QInt
{
private:
__int64 a[2];
public:
void Get()
{
cout << a[0] << endl;
cout << a[1] << endl;
}
QInt()
{
a[0] = 0;
a[1] = 0;
}
//the funtion get the string binary of a[1]
char* GetQInt(char *A);
//the devide two of string decimal
char* Div2(char *Str);
//the funtion set bit to a[0] and a[1]
void Setbit(int i, int bit);
//con vert decimal to binary
QInt ConvertDecimalToBinary(char *De, char *Bi);
};
//the funtion get the string binary of a[1]
char *QInt::GetQInt(char *A)
{
for (int i = 0; i < 64; i++)
{
if (((a[1] >> i) & 1) != 0)
{
A[63 - i] = 49;
}
else
{
A[63 - i] = 48;
}
}
return A;
}
// the funtion set bit to a[0] and a[1]
void QInt:: Setbit(int i, int bit)
{
//if i<64 we set bit to a[1]
if (i < 64)
{
if (bit==1)
{
a[1]=(1 << i) | a[1];
}
}//similar to a[1]
else
{
if (bit == 1)
a[0]=(1 << i) | a[0];
}
}
//the devide two of string decimal
char*QInt:: Div2(char *Str)
{
char Arr[100];
int n = strlen(Str);
int a = 0;//lay phan du
int i = 0;
int j = 0;
while (Str[i] == 0)
{
i++;
}
for (i; i < n; i++)
{
int c = Str[i] - 48 + a * 10;
a = c % 2;
Arr[j] = c / 2 + 48;
j++;
}
Arr[i] = '\0';
for (i = 0; i < strlen(Arr); i++)
{
Str[i] = Arr[i];
}
Str[i] = '\0';
return Str;
}
//con vert decimal to binary
QInt QInt::ConvertDecimalToBinary(char *De,char *Bi)
{
int bit;
int i = 127;
int lenth = strlen(De);
while (1)
{
//variable h use to count the number 0 of the string decimal,if h=lenth,exit
int k = 0;
int h = 0;
while (De[k])
{
if (De[k] == '0')
h++;
k++;
}
if (h == lenth)
break;
else
{
bit = (De[lenth - 1] - 48) % 2;
Bi[i] = bit + 48;
Setbit(127 - i, bit);
De = Div2(De);
i--;
}
}
Bi[128] = NULL;
return *this;
}
int main()
{
char s[100];
char b[200];
char c[200];
for (int i = 0; i < 128; i++)
{
b[i] = 48;
}
cout << "Please enter a string : ";
gets_s(s, 99);
QInt a;
a.ConvertDecimalToBinary(s, b);
a.Get();
a.GetQInt(c);
for (int i = 0; i < 64; i++)
cout << b[i];
cout << endl;
for (int i = 64; i < 128; i++)
cout << b[i];
cout << endl;
for (int i = 0; i < 64; i++)
{
cout << c[i];
}
cout << endl;
system("pause");
return 0;
}
If i==100, what do you think (1 << i) does? What should it do? And why are you ignoring a[0] in GetQInt ?
In general, you have quite a few problems. You're not comfortable with std::string, and making a mess of the char* everywhere. You're not clearly articulating (not even to yourself) what methods are supposed to be doing. You're putting mathods in a class that are unrelated to the class. Even worse, some of those methods redefine the name a ! That is very confusing.
I am coding the game connect four for my computer science class, everything was going great then I dont know what happened but I kept getting Sementation faults whenever I tried to run it. Here is my code.
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cctype>
using namespace std;
struct game{
char **board;
char p1;
char p2;
};
void print_board(game p, int r, int c);
bool play(game p, bool gamewon, int r, int c, int pieces);
bool check(int a, int b, game p, int r, int c, int pieces);
int drop(int b, char player, game p, int c);
int main(int argc, char *argv[]){
game p;
int r, c, pieces;
bool gamewon = false;
/*if(argc == 7) {
for(int i = 1; i < argc; i += 2) {
if(argv[i][0]=='-' && argv[i][1]=='r')
r = atoi(argv[i+1]);
else if(!strcmp(argv[i],"-c"))
c = atoi(argv[i+1]);
else if(!strcmp(argv[i],"-p")){
pieces = atoi(argv[i+1]);
while (pieces < 1){
cout << "You cannot have 0 pieces to connect." << endl;
cout << "Please enter a positive, non-zero integer"
<< " for the number of pieces to connect: ";
cin >> pieces;
}
}
else
cout << "Error." << endl;
}
}*/
r = 6;
c = 7;
pieces = 4;
p.board = new char*[r];
for(int i=0; i < r;i++)
p.board[i] = new char[c];
for(int a =0; a < r; a++){
for(int b = 0; b < c; b++)
p.board[a][b] = ' ';
}
print_board(p, r, c);
gamewon = play(p, gamewon, r, c, pieces);
return 0;
}
bool play(game p, bool gamewon, int r, int c, int pieces){
int col, hold = 0, charsPlaced = 0;
char player = 'y';
while(!gamewon){
if(hold != -1){
if(player =='y'){
cout<<"Player 1, what column do you want to put your piece? ";
player = 'r';
}
else{
cout<<"Player 2, what column do you want to put your piece? ";
player = 'y';
}
}
while(true){
if(charsPlaced == r*c) break;
cin>>col;
col--;
if(col <=r && col>= 0) break;
else cout<< "\nPlease enter a value between 1 and " << c << ": ";
if (cin.fail()){
cin.clear();
char d;
cin>>d;
}
}
if(charsPlaced == r*c) break;
hold = drop(col,player, p, c);
if(hold == -1) cout<<"Column is full!\nPlease enter another number between 1 and " << c << ": ";
else{
gamewon = check(hold, col, p, r, c, pieces);
charsPlaced ++;
print_board(p, r, c);
}
}
if(charsPlaced == r*c){
cout<<"No winner! Game is a draw\n";
return true;
}
if(player == 'y')
cout<<"Player 2 is the winner!\n";
else cout<<"Player 1 is the winner!\n";
return true;
}
void print_board(game p, int r, int c){
cout << endl;
for(int a = 0; a < r; a++){
for(int b = 0; b < c; b++)
cout << "|" << p.board[a][b];
cout << "|";
cout << endl;
for(int i = 0; i < c; i++)
cout << "--";
cout << endl;
}
}
bool check(int a, int b, game p, int r, int c, int pieces){
int vertical = 1, horizontal = 1, diagonalone = 1, diagonaltwo = 1, i , j;
cout << i << " " << b << " " << a << endl;
char player = p.board[a][b];
cout << player << endl;
for(i = a + 1; p.board[i][b] == player && i < r; i++, vertical++);
for(i = a - 1; p.board[i][b] == player && i >= 0; i--, vertical++);
if(vertical >= pieces)
return true;
for(j = b - 1; p.board[a][j] == player && j >= 0; j--, horizontal++);
for(j = b + 1; p.board[a][j] == player && j < c; j++, horizontal++);
if(horizontal >= pieces)
return true;
for(i = a - 1, j = b - 1; p.board[i][j] == player && i >= 0 && j >= 0; diagonalone++, i--, j--);
for(i = a + 1, j = b + 1; p.board[i][j] == player && i <= r && j <= c;diagonalone++, i++, j++);
if(diagonalone >= pieces)
return true;
for(i = a - 1, j = b + 1; p.board[i][j] == player && i >= 0 && j <= c; diagonaltwo++, i--, j++);
for(i = a + 1, j = b - 1; p.board[i][j] == player && i <= r && j >= 0; diagonaltwo++, i++, j--);
if(diagonaltwo >= pieces)
return true;
return false;
}
int drop(int b, char player, game p, int c){
if(b >= 0 && b <= c){
if(p.board[0][b] == ' '){
int i;
for(i = 0; p.board[i][b] == ' '; i++)
if(i == 5){
p.board[i][b] = player;
return i;
}
i--;
p.board[i][b] = player;
return i;
}
else{
return -1;
}
}
else{
return -1;
}
}
You are using
char **board;
in your game struct.
But this 2D array is never allocated and yet, you are working with it.
You are missing something like this:
in C:
board = malloc(10 * sizeof(char *));
for (int i = 0; i < 10; i++)
{
board[i] = malloc(10 * sizeof(char));
}
in C++:
board = new char*[10]
for (int i = 0; i < 10; i++)
{
board[i] = new char[10];
}
For starters, if you are in a Linux environment, valgrind is very helpful in finding where your segmentation fault is. To use it, if your program is called hello, just run it as valgrind hello.
Another method to debug these is to put cout or printf statements throughout your code, and observe what the last output was. You are probably indexing beyond the end of an array. Keep in mind that if you declare int x[5], the valid index values are 0 to 4.
You will probably need to change your for loops to < vs. <= so you aren't reading past the last part of the board (keep in mind that arrays are 0 indexed in c++).