(C++) Does not stop on std::cin and goes into infinity loop? - c++

I have a wird problem. Im using Visual Studio 2012, thats my code: (something is in polish but i hope you will understand how it works).
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <queue>
using namespace std;
#pragma warning (disable: 4996);
struct strona
{
int zawartosc;
int gdzie;
};
void Wyswietl(int tab[], int dlugosc)
{
cout<<"Tablica wyglada tak"<<endl;
for(int i=0;i<dlugosc;i++)
{
cout<<tab[i]<<"\t";
}
cout<<endl;
}
int main()
{
int ileStron=3;
int *tablicaStron, *tablicaBitowOdniesienia;
tablicaStron=new int[ileStron];
tablicaBitowOdniesienia=new int[ileStron];
queue <strona> kolejka;
char opcja='a';
while(opcja!='k')
{
cout<<"(D)odawac (K)oniec (W)yswietl";
cin>>opcja;//DONT STOP THE PROGRAM!
if(opcja=='D'|opcja=='d')
{
strona tymczas;
cout<<"Podaj co dodać do kolejki";
cin>>tymczas.zawartosc;
int licznik=0;
if(kolejka.size()<ileStron)
{
tymczas.gdzie=kolejka.size();
kolejka.push(tymczas);
tablicaStron[tymczas.gdzie]=tymczas.zawartosc;
}
else if(kolejka.size()==ileStron)
{
cout<<"bang bang";
int czyJest=0;
int licznikfora=0;
for(int i=0;i<ileStron;i++)//sprawdza czy wpisywana strona nie istnieje przypadkiem w tablicy stron
{
if(tablicaStron[i]==tymczas.zawartosc)
{
czyJest=1;
}
licznikfora++;
}
cout<<"czyJest ma wartosc "<<czyJest<<" a licznik fora "<<licznikfora<<endl;
if(czyJest==0)
{
tymczas.gdzie=kolejka.front().gdzie;
kolejka.pop();//TUTAJ SIE BEDZIE ZAPISYWAC DO PAMIECI WIRTUALNEJ
kolejka.push(tymczas);
tablicaStron[tymczas.gdzie]=tymczas.zawartosc;
}
else if(czyJest==1)
{
cout<<"to co chcesz dodac juz jest w pamieci";
}
}
else
{
cout<<"rozmiar kolejki sie nie zgadza";
}
}
else if(opcja=='W'|opcja=='w')
{
Wyswietl(tablicaStron,ileStron);
cout<<endl;
cout<<"pierwszy element w kol: "<<kolejka.front().zawartosc<<"|"<<kolejka.front().gdzie<<" "
<<"ostatni element w kol: "<<kolejka.back().zawartosc<<"|"<<kolejka.back().gdzie<<endl;
}
}
system("pause");
return 0;
}
The problem is that after choosing option (d) - just type d and press enter, then type any few letters and the program should show you
(D)odawac (K)oniec (W)yswietl
but it is starting to loop to infinity...
What is the problem?

I think problem is with this statement:
cin>>tymczas.zawartosc;
At this point if you give a char or string input, program blows up.
to make your program work properly:
start the program and give only one input d, let the control hit this point : cin>>tymczas.zawartosc;
now input a number here, as it is int type. now control hits first cin.
The problem is with using cin to read int but user inputs a char or string. Behaviour is undefined in this case.
This link should resolve confusion: C++ character to int

Related

How I test this code in DevCpp? (has other 2 .h)

How I teste this code in DevCpp: because show no main error
undefined reference to `WinMain'
#include "Expression.h"
using namespace std;
expression encontra seu valor
void Expression::infixToPostfix()
{
string teste=getInfix();
int n = teste.length();
char atual[n+1];
char x;
strcpy(atual, teste.c_str());
Stack<char> operadores;
Stack<char> pos_fixa;
for(int i=0;i<n+1;i++){
if(isdigit(atual[i])){
pos_fixa.push(atual[i]);
}
else{
if(atual[i] != ')'){
operadores.push(atual[i]);
}
else{
while(!operadores.empty() || x != '('){
operadores.getTop(x);
if(x!='('){
pos_fixa.push(x);
}
operadores.pop(x);
}
}
}
}
while(!pos_fixa.empty()){
pos_fixa.getTop(x);
printf("%c\n",x);
postfix=postfix+x;
pos_fixa.pop(x);
}
}
and I don't know how I run this exemple program to test
because don't have a way to create a main? or anithing like that? has other parts but stackoverflow don't allowed to declare

How to not show the null character while inserting a character into a 2d string?

I was making a pretty simple Battleship game for my school project (I HAVE to use Turbo C++) and i ran into a problem. I'm basically using a 5x5 2D string as my board and hiding a "ship" in it. What I'm trying to do is that whenever the user makes a wrong guess, I want to replace the "O" in the board with an "X", but when i do that, the "O" in the next block gets replaced by a "/0" and shows as a blank space in the output. How do I fix that?
Here's the code:
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#include<stdio.h>
//A function to initialize the board
void start_board(char a[5][5])
{
for(int i=0;i<5;i++)
{ for(int j=0;j<5;j++)
{ strcpy(&a[i][j],"O");
}
}
}
//A function to display the board
void display_board(char a[5][5])
{ for(int i=0;i<5;i++)
{ for(int j=0;j<5;j++)
{ cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
class board
{ public:
char board[5][5];
void start()
{ start_board(board);
}
void display()
{ display_board(board);
}
};
class ship
{ public:
int ship_row, ship_col;
ship()//CONSTRUCTOR FOR PUTTING COORDINATES OF SHIP
{ randomize();
ship_row= random(5);
ship_col=random(5);
cout<<ship_row<<endl<<ship_col;
}
};
class guess: public board, public ship
{ public:
int guess_row,guess_col;
char vboard[5][5];
guess()
{ start_board(vboard);
}
void takeguess();
};
void guess:: takeguess()
{ int count=0;
while(count<3)
{
cout<<endl;
cout<<"Guess a row ";
cin>>guess_row;
cout<<"Guess a column ";
cin>>guess_col;
if(guess_row==ship_row && guess_col==ship_col)
{ cout<<"Congratulations! You sank the battleship!";
break;
}
else if(guess_row>4 || guess_col>4)
{ cout<<"invalid guess";
}
else
{ clrscr();
cout<<"Incorrect Guess!"<<endl;
strcpy(&vboard[guess_row][guess_col],"X");
display_board(vboard);
count+=1;
}
if(count==3)
{ cout<<"GAME OVER!";
}
}
}
void main()
{ clrscr();
board b;
b.start();
b.display();
guess g;
g.takeguess();
getch();
}
For example, If the user guesses 0,2, and that isn't the ship's location the output will show:
OOX O
OOOOO
OOOOO
OOOOO
OOOOO
Sorry for the messy code(it isn't complete) and any mistakes i made while writing this post, it's my first time using stackoverflow. Thank You for your help!
Don't use strcpy!! You're not copying a string, you're setting the value of a single character in the string, so use the right tool for the job.
vboard[guess_row][guess_col] = 'X';
This is because "X" is actual 2 characters 'X' and '\0' so your strcpy hits 2 cells in your array

C++ Object reference not set to an instance of an object

When I try to compile the following program it says "Build failed. Object reference not set to an instance of an object" . I'm kinda new to c++ so if anybody can help me it'll be great . I'm just trying out some example I saw in a book so I don't know whats wrong with this .
using namespace std;
class matrix
{
int m[3][3];
public:
void read(void);
void display(void);
friend matrix trans(matrix);
}
void matrix :: read(void)
{
cout<<"Enter the elements of the 3x3 array matrix : \n";
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"m["<<i<<"]["<<j<<"] =";
cin>>m[i][j];
}
}
}
void matrix :: display(void)
{
int i,j;
for(i=0;i<3;i++)
{
cout<<"\n";
for(j=0;j<3;j++)
{
cout<<m[i][j]<<"\t";
}
}
}
matrix trans(matrix m1)
{
matrix m2;
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
m2.m[i][j] = m1.m[j][i];
}
}
return(m2); //returning an object
}
int main()
{
matrix mat1,mat2;
mat1.read();
cout<<"\nYou entered the following matrix :";
mat1.display();
mat2 = trans(mat1);
cout<<"\nTransposed matrix :";
mat2.display();
getch();
return 0;
}
1 - Insert semi-colon after the class definition
2 - Insert the correct headers
#include <iostream>
#include <conio.h>
3 - Try getting a compiler that is a bit more descriptive with regards to errors. I did all that i mentioned and your program ran. Try it
It compiles fine after you fix your missing semi-colon (after your class declaration) and add either #include <conio.h> (Visual Studio) or #include <curses.h> (for a POSIX system) for the getch() function (which is not a standard function).

parallel_pipeline not terminating

i am using parallel_pipeline function in my code.Sometimes when my condition is satisfied it stops the pipeline and sometimes it does not.When the flow control calls stop even after that it does not terminate instead it calls its next part and prints on console and then console output becomes like it is been staying in infinite loop and doing nothing.
Code is :
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <tbb/pipeline.h>
#include <tbb/atomic.h>
#include <tbb/concurrent_queue.h>
#include <tbb/compat/thread>
#include <tbb/tbbmalloc_proxy.h>
#include <tbb/tick_count.h>
using namespace std;
using namespace tbb;
#define pi 3.141593
#define FILTER_LEN 265
double coeffs[ FILTER_LEN ] =
{
0.0033473431384214393,0.000032074683390218124,0.0033131082058404943,0.0024777666109278788,
-0.0008968429179843104,-0.0031973449396977684,-0.003430943381749411,-0.0029796565504781646,
-0.002770673157048994,-0.0022783059845596586,-0.0008531818129514857,0.001115432556294998,
0.0026079871108133294,0.003012423848769931,0.002461420635709332,0.0014154004589753215,
0.00025190669718400967,-0.0007608257014963959,-0.0013703600874774068,-0.0014133823230551277,
-0.0009759556503342884,-0.00039687498737139273,-0.00007527524701314324,-0.00024181463305012626,
-0.0008521761947454302,-0.00162618205097997,-0.002170446498273018,-0.002129903305507943,
-0.001333859049002249,0.00010700092934983156,0.0018039564602637683,0.0032107930896349583,
0.0038325849735515363,0.003416201274366522,0.002060848732332109,0.00017954815260431595,
-0.0016358832300944531,-0.0028402136847527387,-0.0031256650498727384,-0.0025374271571154713,
-0.001438370315670195,-0.00035115295209013755,0.0002606730012030533,0.0001969569787142967,
-0.00039635535951198597,-0.0010886127490608972,-0.0013530057243606405,-0.0008123200399262436,
0.0005730271959526784,0.0024419465938120906,0.004133717273258681,0.0049402122577746265,
0.0043879285604252714,0.002449549610687005,-0.00040283102645093463,-0.003337730734820209,
-0.0054508346511294775,-0.006093057767824609,-0.005117609782189977,-0.0029293645861970417,
-0.0003251033117661085,0.0018074390555649442,0.0028351284091668164,0.002623563404428517,
0.0015692864792199496,0.0004127664681096788,-0.00009249878881824428,0.0004690173244168184,
0.001964334172374759,0.0037256715492873485,0.004809640399145206,0.004395274594482053,
0.0021650921193604,-0.0014888595443799124,-0.005534807968511709,-0.008642334104607624,
-0.009668950651149259,-0.008104732391434574,-0.004299972815463919,0.0006184612821881392,
0.005136551428636121,0.007907786753766152,0.008241212326068366,0.00634786595941524,
0.003235610213062744,0.00028882736660937287,-0.001320994685952108,-0.0011237433853145615,
0.00044213409507615003,0.0022057106517524255,0.00277593527678719,0.0011909915058737617,
-0.0025807757230413447,-0.007497632882437637,-0.011739520895818884,-0.013377018279057393,
-0.011166543231844196,-0.005133056165990026,0.0032948631959114935,0.011673660427968408,
0.017376415708412904,0.018548938130314566,0.014811760899506572,0.007450782505155853,
-0.001019540069785369,-0.007805775815783898,-0.010898333714715424,-0.00985364043415772,
-0.005988406030111452,-0.001818560524968024,0.000028552677472614846,-0.0019938756495376363,
-0.007477684025727061,-0.013989430449615033,-0.017870518868849213,-0.015639422062597726,
-0.005624959109456065,0.010993528170353541,0.03001263681283932,0.04527492462846608,
0.050581340787164114,0.041949186532860346,0.019360612460662185,-0.012644336735920483,
-0.0458782599058412,-0.07073838953156347,-0.0791205623455818,-0.06709535677423759,
-0.03644544574795176,0.005505370370858695,0.04780486657828151,0.07898800597378192,
0.0904453420042807,0.07898800597378192,0.04780486657828151,0.005505370370858695,
-0.03644544574795176,-0.06709535677423759,-0.0791205623455818,-0.07073838953156347,
-0.0458782599058412,-0.012644336735920483,0.019360612460662185,0.041949186532860346,
0.050581340787164114,0.04527492462846608,0.03001263681283932,0.010993528170353541,
-0.005624959109456065,-0.015639422062597726,-0.017870518868849213,-0.013989430449615033,
-0.007477684025727061,-0.0019938756495376363,0.000028552677472614846,-0.001818560524968024,
-0.005988406030111452,-0.00985364043415772,-0.010898333714715424,-0.007805775815783898,
-0.001019540069785369,0.007450782505155853,0.014811760899506572,0.018548938130314566,
0.017376415708412904,0.011673660427968408,0.0032948631959114935,-0.005133056165990026,
-0.011166543231844196,-0.013377018279057393,-0.011739520895818884,-0.007497632882437637,
-0.0025807757230413447,0.0011909915058737617,0.00277593527678719,0.0022057106517524255,
0.00044213409507615003,-0.0011237433853145615,-0.001320994685952108,0.00028882736660937287,
0.003235610213062744,0.00634786595941524,0.008241212326068366,0.007907786753766152,
0.005136551428636121,0.0006184612821881392,-0.004299972815463919,-0.008104732391434574,
-0.009668950651149259,-0.008642334104607624,-0.005534807968511709,-0.0014888595443799124,
0.0021650921193604,0.004395274594482053,0.004809640399145206,0.0037256715492873485,
0.001964334172374759,0.0004690173244168184,-0.00009249878881824428,0.0004127664681096788,
0.0015692864792199496,0.002623563404428517,0.0028351284091668164,0.0018074390555649442,
-0.0003251033117661085,-0.0029293645861970417,-0.005117609782189977,-0.006093057767824609,
-0.0054508346511294775,-0.003337730734820209,-0.00040283102645093463,0.002449549610687005,
0.0043879285604252714,0.0049402122577746265,0.004133717273258681,0.0024419465938120906,
0.0005730271959526784,-0.0008123200399262436,-0.0013530057243606405,-0.0010886127490608972,
-0.00039635535951198597,0.0001969569787142967,0.0002606730012030533,-0.00035115295209013755,
-0.001438370315670195,-0.0025374271571154713,-0.0031256650498727384,-0.0028402136847527387,
-0.0016358832300944531,0.00017954815260431595,0.002060848732332109,0.003416201274366522,
0.0038325849735515363,0.0032107930896349583,0.0018039564602637683,0.00010700092934983156,
-0.001333859049002249,-0.002129903305507943,-0.002170446498273018,-0.00162618205097997,
-0.0008521761947454302,-0.00024181463305012626,-0.00007527524701314324,-0.00039687498737139273,
-0.0009759556503342884,-0.0014133823230551277,-0.0013703600874774068,-0.0007608257014963959,
0.00025190669718400967,0.0014154004589753215,0.002461420635709332,0.003012423848769931,
0.0026079871108133294,0.001115432556294998,-0.0008531818129514857,-0.0022783059845596586,
-0.002770673157048994,-0.0029796565504781646,-0.003430943381749411,-0.0031973449396977684,
-0.0008968429179843104,0.0024777666109278788,0.0033131082058404943,0.000032074683390218124,
0.0033473431384214393
};
class MyBuffer
{
public:
double *acc;
double *buffer;
int start,end;
static int j;
MyBuffer()
{
start=0;
end=0;
buffer=new double[150264];
acc=new double[150000];
fill_n(buffer,150264,0);
}
~MyBuffer()
{
delete[] buffer;
delete[] acc;
}
int startnumber()
{
return start;
}
int endnumber()
{
return end;
}
};
typedef concurrent_bounded_queue<MyBuffer> QueueMyBufferType;
QueueMyBufferType chunk_queue;
atomic<bool> stop_flag;
atomic<bool> stop_filter;
int MyBuffer::j=0;
int queueloopcount=30;
void input_function()
{
stop_flag = false;
cout<<"thread reached to call input function " <<endl;
ofstream o("testing sinewave.csv");
int counter=0;
while(counter<150000)
{
// cout<<"value of counter is \t" <<counter << endl;
MyBuffer *b=new MyBuffer;
b->start=(FILTER_LEN-1+(counter));
b->end=(5264+(counter));
// cout<<"value of b.start is and b.end is "<<b->start<<"\t" <<b->end<<endl;
for(int i =b->startnumber(); i <b->endnumber(); i++)
{
b->buffer[i] = sin(700 * (2 * pi) * (i / 5000.0));
o<<b->buffer[i]<<endl;
}
chunk_queue.push(*b);
counter+=5000;
// cout<<"value of queueloopcount is "<< queueloopcount << endl;
}
cout<<"all data is perfectly generated" <<endl;
}
int main()
{
int ntokens = 8;
thread inputfunc(input_function);
tick_count t1,t2;
ofstream o("filter700Hz.csv");
t1=tick_count::now();
bool stop_pipeline = false;
stop_filter=false;
inputfunc.join();
parallel_pipeline(ntokens,make_filter<void,MyBuffer*>
(
filter::parallel,[&](flow_control& fc)->MyBuffer*
{
if(queueloopcount==0)
{
fc.stop();
cout<<"pipeline stopped"<<endl;
}
else
{
MyBuffer *b=new MyBuffer;
chunk_queue.pop(*b);
{
cout<<"value of start and end popped is "<<b->startnumber()<<"\t"<<b->endnumber()<<endl;
queueloopcount--;
}
return b;
}
}
)&
make_filter<MyBuffer*, void>
(
filter::serial,[&](MyBuffer* b)
{
cout<<"value of second filter start is and end is \t "<< b->startnumber() << "\t" << b->endnumber() <<endl;
}
)
);
cout<<"now i am out" <<endl;
o.close();
t2=tick_count::now();
cout << "\n Time elapsed is \n\n" <<(t2-t1).seconds()<<endl;
return 0;
}
please help to find where code is wrong.
The Problem in this code is with filter i.e parallel in first stage that is causing problem to flow_control object which tries to pop and it is a blocking call due to which it blocks and solution to this is you should probably have a serial first filter that only create an empty MyBuffer* and stop the pipeline if no more work is due. Then have a parallel second filter that performs the real work and finally a serial (in-order) output stage.

Newbie - matrix addition implementation in c++

Hello i'm trying to program the addition of 2 matrices into a new one (and it does when i run the program step by step) but for some reason VS 2010 gives me an access error after it does the addition.
Here is the code.
#include <iostream>
#include <cstdio>
#include <conio>
using namespace std;
class operatii
{
typedef double mat[5][5];
mat ms,m1,m2;
int x1,x2,y1,y2;
public:
void preg();
int cit_val();
void cit_mat(int&,int&,double[5][5]);
void suma();
void afisare(int&,int&,double[5][5]);
};
void operatii::preg()
{
cit_mat(x1,y1,m1);
cit_mat(x2,y2,m2);
suma();
afisare(x1,y1,ms);
}
int operatii::cit_val()
{
int n;
cin>>n;
return n;
}
void operatii::cit_mat(int& x,int& y,double m[5][5])
{
char r;
cout<<"Matrice patratica? Y/N ";
cin>>r;
if ((r=='y')||(r=='Y'))
{
cout<<"Numar linii si coloane: ";
x=cit_val();
y=x;
}
else
{
cout<<"Numar linii: ";
x=cit_val();
cout<<"Numar coloane: ";
y=cit_val();
}
for (int i=1;i<=x;i++)
for (int j=1;j<=y;j++)
cin>>m[i][j];
}
void operatii::suma()
{
if ((x1==x2)&&(y1==y2))
for (int i=1;i<=x1;i++)
for (int j=1;i<=y1;j++)
ms[i][j]=m1[i][j]+m2[i][j];
else cout<<"Eroare";
}
void operatii::afisare(int& x,int& y,double m[5][5])
{
cout<<endl;
for (int i=1;i<=x;i++)
{
for (int j=1;j<=y;j++)
cout<<m[i][j];
cout<<endl;
}
}
void main()
{
operatii matrice;
matrice.preg();
system("PAUSE");
}
Any kind of help would be apreciated.
Arrays are 0-based in c++.
Change your various variants of for (somevar=1; somevar<=something) to for (somevar=0; somevar<something)
You're writing past the end of your arrays, which overwrites stack return address, leading to a return to nonexecutable code, again leading to an access violation.
Also,
for (int j=1;i<=y1;j++)
I think you want to use j not i here. Such errors are much easier to see if you use longer and more distinct variable names than "i" and "j", such as e.g. "Line" and "Column"