Unexpected termination of of c++ program - c++

My error in the following program is: It terminates in the middle of execution and does not even allows user to enter the inputs.
#include<iostream>
using namespace std;
class Graph
{
public:
char city;
int distance;
Graph *next;
};
Graph **graph=new Graph*[20];
int size;
void create()
{
cout<<"Enter the number of cities:";
cin>>size;
cout<<"Enter "<<size<<" cities\n";
for(int i=0;i<size;i++)
{
cout<<"City number "<<i+1<<":";
cin>>graph[i]->city;
cout<<endl;
}
}
int main()
{
create();
return 0;
}
The output statements do not even appear and the program terminates. Please can someone run this code and help me out. This issue has never occurred before and so I do not know how to solve this.
Thank you in advance.

Related

error: use of undeclared identifier 'Solution' vector<int> ret = Solution().plusOne(param_1); return ret;

trying to add +1 to the nth term****and showing the error at the leetcode window which is mentioned above
#include<iostream>
using namespace std;
int main()
{
int n;
int a[n];//array of n number
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int i=0;i<n;i++)
{
if(a[i+1]==a[n]) //I am trying to add +1 to nth term of array
{
a[i]=a[i]+1;
}
cout<<a[i];
}
return 0;
}
Basically I think you might have erased all the code in leetcode window and pasted the above code in it. You might have erased
"***
Class Solution{
public:
};
***"
part of the console. Try to reset your code and paste your solution(which includes only the function) only under public section of the class. It should work fine.

Code Blocks gives me following error upon execution of code(even parity hamming code)

error message
This is what it shows in the build log:
Checking for existence: C:\CodeBlocks\hammingcodeven.exe
Executing: '"C:\CodeBlocks/cb_console_runner.exe" "C:\CodeBlocks\hammingcodeven.exe"' (in 'C:\CodeBlocks')
Set variable: PATH=C:\MinGW\bin;C:\MinGW;C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Users\mahek\AppData\Local\Microsoft\WindowsApps
Process terminated with status -1073741510 (0 minute(s), 54 second(s))
This is the code to find binary value of m bit code after using an even parity hamming code
#include<iostream>
#include<math.h>
using namespace std;
class data
{
int A[50],m,r,ps[6],B[6][56],final[56];
public:
void show();
void input();
void findr();
void findps();
void binary();
void findfinal();
};
void data::input()
{
cout<<"Enter length of frame:";
cin>>m;
cout<<"Enter frame:";
for(int i=1;i<=m;i++)
{
cin>>A[i];
}
findr();
}
void data::findr()
{
r=0;
for(int i=1;i<=m;i++)
{
int x=pow(2,i);
if(x>(m+i+1))
{
r=i;
break;
}
}
if(r==0)
cout<<"Error";
binary();
}
void data::binary()
{
for(int i=1;i<=m+r;i++)
{
int h=i;
for(int j=r;j>=1;j--)
{
B[i][j]=h%2;
h=h/2;
}
}
findps();
}
void data::findps()
{
for(int i=1;i<=r;i++)
ps[i]=0;
for(int i=1;i<=m+r;i++)
{
for(int j=1;j<=r;j++)
{
ps[j]=ps[j]^B[i][j];
}
}
findfinal();
}
void data::findfinal()
{
for(int i=1,j=1;i<=m+r,j<=r;i++,j*=2)
{
if(i==j)
{
final[i]=ps[j];
}
else
final[i]=A[i];
}
}
void data::show()
{
cout<<"Input:";
for(int i=1;i<=m;i++)
cout<<A[i]<<" ";
cout<<endl;
cout<<"Output:";
for(int i=1;i<=m+r;i++)
cout<<final[i]<<" ";
}
int main()
{
data obj;
obj.input();
obj.show();
}
In the first loop of the function binary(), the variable i can go as high as m + r. In your example, m is 8, so m + r is at least 9. However, i is used to index the first dimension of B, which has a sizr of six. Accessing B outside of its defined size results in reading/writing other memory on the stack, resulting in undefined behavior. To help with this, you could dynamically allocate your fields with the needed size. I also strongly recommend giving your fields meaningful names to help people understand what your code is trying to do.
Furthermore, you are indexing arrays incorrectly. In C++ and many other languages, arrays are zero indexed, meaning that the first value of an array A of size n is at A[0] and the last value is at A[n-1].

My c++ Code is not working for a Graph

I am writing a code for a graph in c++ but there is some problem. It is not working properly. Please help me what is the problem with it? Its code for a graph which can take inputs for graph from user and each edge in graph has specific weight.
Here is the code:
#include <iostream>
#include <vector>
using namespace std;
struct edge {
char src;
char dest;
int weight;
};
class Graph {
public:
vector<edge> edges;
int size,j=0;
//Constructor
Graph(int c) {
size=c;
}
void graphDesign(char s,char d,int w) {
edges[j].src=s;
edges[j].dest=d;
edges[j].weight=w;
j++;
}
void printGraph() {
for(int i=0; i<size; i++) {
cout<<edges[i].src<<"->"<<edges[i].dest<<" :
<<edges[i].weight<<endl;
}
}
};
int main() {
int e,i,w;
char s,d;
cout<<"Enter number of edges of graphs: ";
cin>>e;
Graph graph(e);
for(i=0; i<e; i++) {
cout<<"Enter source: ";
cin>>s;
cout<<"Enter destination: ";
cin>>d;
cout<<"Enter weight of the edge: ";
cin>>w;
graph.graphDesign(s,d,w);
}
graph.printGraph();
return 0;
}
One issue is here:
void graphDesign(char s,char d,int w) {
edges[j].src=s;
edges[j].dest=d;
edges[j].weight=w;
j++;
}
Since edges is an empty vector, accessing edges[j] is an illegal access.
You need to size the edges vector appropriately before you use it.
class Graph {
public:
vector<edge> edges;
//Constructor
Graph(int c) : edges(c) {}
This creates a vector with c entries.
Also, do not use extraneous, unnecessary member variables such as size here. The vector class has a size() member function to tell you how many items are in the container.
Using extraneous variables like size runs the risk of bugs occurring due to having to update this variable anytime the vector changes size. Instead of trying to do this housekeeping yourself, use the size() function provided to you by std::vector.

C++ code compiles but doesn't run [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am currently writing a Texas Hold'em code in order to learn more about c++ and gain experience. But I recently ran into a problem in which I have no idea what to do.My code compiles just fine without errors but once I make it run and it arrive at a specific function it just stops working (as in I get an error from CodeBlock saying your program has stopped working). I have tried cutting out parts such as loops in the function to see which specific part is the problem but after a couple of days im still at a stop.
Here is the function and class that I believe is the problem:
class Player{
string name;
int bank=100;
static string cards[53];
static string final_card[2][6];
static string table_cards[5];
public:
void set_name(string a){name=a;}
string print_name(){return name;}
void card_generator();
string set_cards(int c){return cards[c];}
int print_bank(){return bank;}
void set_final_card(int i, int max_i);
void print_cards(){for(int i=0;i<6;i++){cout<<final_card[0][i]<<endl;}}
};
void Player::set_final_card(int i, int max_i){
srand(time(NULL));
int tempV = 17;//create temp cards
string tempCards[tempV];
int randNB[tempV];
int check1 = 0, tmp;
while (check1==0){
for(int g=0; g<tempV;g++){
tempCards[g]=cards[rand()%53];
check1=1;
tmp = g - 1;
for(int o=tmp; o!=0; o--){
if (tempCards[g]==tempCards[o]){
check1=0;
}
}
}
}
int p=0,k;
while(p<6){
k=0;
final_card[0][k]=tempCards[p];
k++;
p++;
}
while(p<12){
k=0;
final_card[1][k]=tempCards[p];
k++;
p++;
}
while(p<17){
k=0;
table_cards[k]=tempCards[p];
k++;
p++;
}
}
Here is the full code in case I am wrong of the source of the problem:
#include <iostream>
#include <string>
#include <stdlib.h>
#include <ctime>
using namespace std;
class Player{
string name;
int bank=100;
static string cards[53];
static string final_card[2][6];
static string table_cards[5];
public:
void set_name(string a){name=a;}
string print_name(){return name;}
void card_generator();
string set_cards(int c){return cards[c];}
int print_bank(){return bank;}
void set_final_card(int i, int max_i);
void print_cards(){for(int i=0;i<6;i++){cout<<final_card[0][i]<<endl;}}
};
string Player::cards[53];
string Player::final_card[2][6];
string Player::table_cards[5];
int main () {
int choice1=0, i, max_i, tempV;
string username;
cout<< "Welcome to Texas Hold'Em!\n1-Play\n2-Quit\n";//menu
while((choice1!=1)&&(choice1!=2)){//Makes sure that user enters correct input```
cin>>choice1;
if ((choice1!=1)&&(choice1!=2)){
cout<<"Invalid Input!\nTry again!\n";
}
}
system ("cls");
if (choice1==2){//End Program
return 0;
}
cout<<"How many players?[2-6]"<<endl;
while((i!=2)&&(i!=3)&&(i!=4)&&(i!=5)&&(i!=6)){//Makes sure that user enters correct input
cin>>i;
if ((i!=2)&&(i!=3)&&(i!=4)&&(i!=5)&&(i!=6)){
cout<<"Invalid Input!\nTry again!\n";
}
}
Player player[i];//creating array of players
player[0].card_generator();
max_i = i;//max_i is nb of players
i--;//since arrays start at 0
system("cls");
player[0].set_final_card(i,max_i);
player[0].print_cards();
if (choice1==1) {//SET NAMES OF ALL PLAYERS
for(i=0; i<max_i; i++){
cout<< "Whats your name?\n";
cin>>username;
player[i].set_name(username);
cout<<"Your name is "<< player[i].print_name()<< " and you have "<< player[i].print_bank()<<"$\n";
tempV=i+1;//used bc arrays start at 0
if(tempV!=max_i){
cout<< "Give PC to player "<< i+2 <<endl;
}
_sleep(3000);
system("cls");
}
}
return 0;
}
void Player::set_final_card(int i, int max_i){
srand(time(NULL));
int tempV = 17;//create temp cards
string tempCards[tempV];
int randNB[tempV];
int check1 = 0, tmp;
while (check1==0){
for(int g=0; g<tempV;g++){
tempCards[g]=cards[rand()%53];
check1=1;
tmp = g - 1;
for(int o=tmp; o!=0; o--){
if (tempCards[g]==tempCards[o]){
check1=0;
}
}
}
}
int p=0,k;
while(p<6){
k=0;
final_card[0][k]=tempCards[p];
k++;
p++;
}
while(p<12){
k=0;
final_card[1][k]=tempCards[p];
k++;
p++;
}
while(p<17){
k=0;
table_cards[k]=tempCards[p];
k++;
p++;
}
}
void Player::card_generator(){
string card_value[13];
card_value[0]="1";
card_value[1]="2";
card_value[2]="3";
card_value[3]="4";
card_value[4]="5";
card_value[5]="6";
card_value[6]="7";
card_value[7]="8";
card_value[8]="9";
card_value[9]="10";
card_value[10]="J";
card_value[11]="Q";
card_value[12]="K";
string card_type[4];
card_type[0]="of hearts";
card_type[1]="of diamonds";
card_type[2]="of clubs";
card_type[3]="of spades";
string card[53];
int x=0;
fill_n(card,53,0);
for (int j=0;j<4;j++){
for (int q=0;q<13;q++){
card[x]=card_value[q]+" "+card_type[j];
cards[x]=card[x];
x++;
}
}
}
If you have any criticism about the code itself even if not directly linked to problem feel free to tell me as I'm doing this to learn :D. Thank you in advance!!
#include <iostream>
#include <string>
#include <stdlib.h>
#include <ctime>
Be consistent in what you do. Including <stdlib.h> and <ctime> looks strange. Either include <cstdlib> and <ctime>, or include <stdlib.h> and <time.h>.
using namespace std;
Don't do this. This using imports all names from the std namespace, which is several hundreds. Only import those names that you actually need, or, alternatively, write std::time instead of the unqualified time. This makes it perfectly clear that you are referring to the time from the standard library instead of one that you might have defined yourself.
class Player{
string name;
int bank=100;
static string cards[53];
static string final_card[2][6];
static string table_cards[5];
The cards should not be represented as strings, but as a separate data type called Card, with properties like suit and rank and a to_string method.
public:
void set_name(string a){name=a;}
To make your program fast, pass a as const std::string & instead of a simple string. This will prevent some copying of data. You should give a better name to the parameter, e.g. void set_name(const std::string &name) { this.name = name; }.
string print_name(){return name;}
This method does not print anything, therefore it must not be called print_name.
void card_generator();
Methods usually are named with verbs, not with nouns. So generate_cards would be a better name. But what does generate mean here? (I'm not a native English speaker, but would draw_cards describe it accurately?)
string set_cards(int c){return cards[c];}
A method called set_* usually modifies something. This one doesn't. Why did you name it this way?
int print_bank(){return bank;}
void set_final_card(int i, int max_i);
Give better names to the parameters. From reading only this declaration, I have no idea what i and max_i might mean.
void print_cards(){for(int i=0;i<6;i++){cout<<final_card[0][i]<<endl;}}
};
string Player::cards[53];
string Player::final_card[2][6];
string Player::table_cards[5];
It looks strange that the cards are stored in the Player class, since no poker player should ever have insight to all 52 cards. And why 53? Is there a joker in your game? These three fields should be moved to a class Table. This allows you to have multiple independent tables, which is nice for a big tournament.
int main () {
int choice1=0, i, max_i, tempV;
string username;
cout<< "Welcome to Texas Hold'Em!\n1-Play\n2-Quit\n";//menu
while((choice1!=1)&&(choice1!=2)){//Makes sure that user enters correct input```
Before reading the choice1 variable, you must initialize it. Since you don't do it, you invoke undefined behavior and everything that the program does after that is unpredictable.
cin>>choice1;
if ((choice1!=1)&&(choice1!=2)){
cout<<"Invalid Input!\nTry again!\n";
}
}
system ("cls");
if (choice1==2){//End Program
return 0;
}
cout<<"How many players?[2-6]"<<endl;
while((i!=2)&&(i!=3)&&(i!=4)&&(i!=5)&&(i!=6)){//Makes sure that user enters correct input
Same here. The user hasn't yet entered anything, so how can you check it?
cin>>i;
Add error handling for every input by enclosing it in an if clause: if (std::cin >> i) {.
if ((i!=2)&&(i!=3)&&(i!=4)&&(i!=5)&&(i!=6)){
cout<<"Invalid Input!\nTry again!\n";
}
}
Player player[i];//creating array of players
Don't use arrays, use a std::vector instead. This allows you to easily extend the table to have 10 players. In the end, there should not be a single 6 in your program.
player[0].card_generator();
max_i = i;//max_i is nb of players
Why do you call this variable max_i, when the comment says that max_players would be a better name?
i--;//since arrays start at 0
system("cls");
player[0].set_final_card(i,max_i);
player[0].print_cards();
if (choice1==1) {//SET NAMES OF ALL PLAYERS
for(i=0; i<max_i; i++){
cout<< "Whats your name?\n";
cin>>username;
player[i].set_name(username);
cout<<"Your name is "<< player[i].print_name()<< " and you have "<< player[i].print_bank()<<"$\n";
tempV=i+1;//used bc arrays start at 0
if(tempV!=max_i){
What does the V in tempV mean?
cout<< "Give PC to player "<< i+2 <<endl;
}
_sleep(3000);
system("cls");
}
}
return 0;
}
void Player::set_final_card(int i, int max_i){
srand(time(NULL));
int tempV = 17;//create temp cards
This 17 is a magic number. It would be better to write it as 5 + 6 * 2, since that makes it much clearer.
string tempCards[tempV];
int randNB[tempV];
int check1 = 0, tmp;
while (check1==0){
for(int g=0; g<tempV;g++){
tempCards[g]=cards[rand()%53];
The 53 is wrong here. I can only be wrong. When you select from 52 cards with equal probability, it must be % 52.
check1=1;
tmp = g - 1;
for(int o=tmp; o!=0; o--){
if (tempCards[g]==tempCards[o]){
check1=0;
}
}
}
}
Captain Giraffe has answered this question in the comments. If any newbies like me face a similar problem look up what a debugger is, as errors like these are called run-time errors. Check this page for a simple explanation: http://www.cplusplus.com/forum/articles/28767/ .

Parameter Passing Converting Floats error

I'm having an issue with passing parameters for this simple program. I am getting a "cannot convert 'float*' to 'float' argument '2' to 'void getData(std::string, float, float, float, float)'" error when trying to compile this program. Can anyone figure out what I'm doing wrong? I've been trying forever. Note: Please ignore deprecated stuff like system("PAUSE") and a few other things. This is simple the way my teacher has thought me to code and this is what he wants me using for this program. I am aware of getchar() and I use it for practice and final work. Plus this shouldn't affect the program as I have been using it without issues before on small programs for my C++ class.
Here's the code:
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
void getData(string,float,float,float,float);
void getCalc(int,float,float,float,float,float,float,float,float,float,float);
void getPrint(float,float,float);
int main()
{
int const acres=1000;
string crop;
float cpa[4];
float yield[4];
float per[4];
float increase[4];
float cost[4];
float grossmin[4];
float grossmax[4];
float netmin[4];
float netmax[4];
float netave[4];
getData(crop,cpa,yield,per,increase);
getCalc(acres,cpa,yield,per,increase,cost,grossmin,grossmax,netmin,netmax,netave);
getPrint(netmin,netmax,netave);
system("PAUSE");
return 0;
}
void getData(string fcrop,float fcpa[],float fyield[],float fper[],float fincrease[])
{
for (int i=0;i<4;i++)
{
cout<<"Enter the crop: ";
getline(cin,fcrop);
cout<<"Enter the cost per acre:$ ";
cin>>fcpa[i];
cout<<"Enter the yield: ";
cin>>fyield[i];
cout<<"Enter $/bishell: ";
cin>>fper[i];
cout<<"Enter the percentage increase: ";
cin>>fincrease[i];
cin.ignore(80,'\n');
}
}
void getCalc(int acres,float fcpa[],float fyield[],float fper[],float fincrease[],float fcost[],float fgrossmin[],float fgrossmax[],float fnetmin[],float fnetmax[],float fnetave[])
{
for (int i=0;i<4;i++)
{
int acres=1000;
fcost[i]=acres*fcpa[i];
fgrossmin[i]=acres*fyield[i]*fper[i];
fgrossmax[i]=fgrossmin[i]+(fgrossmin[i]*fincrease[i]/100);
fnetmin[i]=fgrossmin[i]-fcost[i];
fnetmax[i]=fgrossmax[i]-fcost[i];
fnetave[i]=(fnetmin[i]+fnetmax[i])/2;
}
}
void getPrint(float fnetmin[],float fnetmax[],float fnetave[])
{
for (int i=0;i<4;i++)
{
cout<<"The minumum profit is:$ "<<fnetmin[i]<<endl;
cout<<"The maximum profit is:$ "<<fnetmax[i]<<endl;
cout<<"The average profit is:$ "<<fnetave[i]<<endl;
}
}
In the prototype you have written at the start of the program, this is written.
void getData(string,float,float,float,float) ;
It should be this instead identical to the one in its definition.
void getData(string,float[],float[],float[],float[]);
The function prototype should be same in its declaration and implementation.