my .exe well compiled program stops working after run it in CodeBlocks,
It stops after typing cin>>f.name the to the console after the choosed choice
I have a Windows 8 and i use GNU GCC compiler here is the code,maybe there is a code error , i don t know
tanks for your attention
:
using namespace std;
struct employee
{
string name;
int age;
};
employee employeeList[10];
class Stack
{
int pos;
public:
Stack(){};
void push(employee e)
{
employeeList[pos] = e;
pos++;
}
employee pop(int n)
{
if(n = pos - 1)return employeeList[pos];
if(n < pos - 1)
{
return employeeList[pos];
for(int j =n; j < pos; j++ )
{
employeeList[pos] = employeeList[pos + 1];
}
}
pos--;
}
string print(int n)
{
n = pos;
cout<<employeeList[pos].name<<endl;
}
char menu()
{
char choice;
cout << "Press 1. to push an employee"<<endl;
cout << "Press 2. to pop an employee"<<endl;
cout << "Press 3. to show an employee"<<endl;
cin>> choice;
return choice;
}
};
int main()
{
Stack s;
char input = s.menu();
int j;
do
{
switch(input)
{
case '1' :{employee f; cin>>f.name; s.push(f);}break;
case '2' :{int n; cin>>n; s.pop(n);} break;
case '3' :{int n; cin>>n; s.print(n);}break;
}
j++;
}
while(j < 10);
return 0;
}
You didn't proper initialization of instance variable "pos" of class "Stack". This means that initially it can have any value. If that value is beyond the range of your "employeeList", you are accessing an area of memory that does not belong to you. That's probably a segmentation error.
Related
I dont understand why is this code giving runtime error on input of any test case (size of string is n, and then string is input). Not even the word "CHECK" (in the solve() function) gets printed.. Please help!
The problem link is : problem
#include <bits/stdc++.h>
using namespace std;
int a[1000][26];
int mov(int l, int u, int x)
{
if(l==u)
{
if(a[l][x]-a[l-1][x]==1)
return 1;
else
return 0;
}
int m=(l+u)/2;
return max(mov(l,m,x+1)+a[u][x]-a[m][x],mov(m,u,x+1)+a[m][x]-a[l-1][x]);
}
void solve()
{
int i,k,j,n;
string str;
cin >> n >> str;
cout << "CHECK";// This is not getting printed
for(i=0;i<26;i++)
a[0][i]=0;
for(i=1;i<n+1;i++)
{
for(j=0;j<26;j++)
{
a[i][j]=a[i-1][j];
if(str[i-1]==j+'a')
a[i][j]++;
}
}
k=mov(1,n,0);
cout << n-k << "\n";
}
int main()
{
int t=1;
cin >> t;
while(t--)
solve();
}
Your very first input shows how many input "pairs" will be given. In your case, you have skipped this part and gone into gathering the input "pairs". This is why "CHECK" wont get printed. To resolve this, have a cin to capture the number of input pairs, then iterate through the inputs to get the pair.
void solve()
{
int i, k, j, n;
string str;
memset(a[0], 0, sizeof(int) * 26); // Use this instead.
cin >> n;
cout << "CHECK";
for (i = 1; i < n + 1; i++)
{
cin >> j >> str;
for (j = 0; j < 26; j++)
{
a[i][j] = a[i - 1][j];
if (str[i - 1] == j + 'a')
a[i][j]++;
}
}
k = mov(1, n, 0);
cout << k << "\n";
}
Also, try not to use using namespace std;.
i was writing a c++ code for performing binary search in an array,but am unable to wield the desired output. when execute the code, i ma getting a never ending output, what may be the reason behind this?
#include <iostream>
using namespace std;
void Binary(int arr[], int n, int key)
{
int s = 0;
int e = n - 1;
while (s <= e) {
int mid = (s + e) / 2;
if (arr[mid] == key) {
cout << "Element Found At Index No. " << mid;
} else if (arr[mid] > key) {
e = mid - 1;
} else {
s = mid + 1;
}
}
}
int main()
{
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int key;
cout << "Enter element to be searched!";
cin >> key;
Binary(arr, n, key);
return 0;
}
After you find your element, you generate output but never exit the loop or change either of the values that the loop condition checks.
Simply break out of the loop (or return from the function) after showing your output:
if (arr[mid] == key) {
cout << "Element Found At Index No. " << mid;
break;
}
Answer provided by #1201ProgramAlarm is the right fix. Adding few minor changes:
Instead of using namespace std; it would be better to explicitly provide the scope resolution for std::cin and std::cout
Providing some more print statements while entering the array elements.
#include <iostream>
void Binary(int arr[], int n, int key)
{
int s = 0;
int e = n - 1;
while (s <= e) {
int mid = (s + e) / 2;
if (arr[mid] == key) {
std::cout << "Element Found At Index No. " << mid;
break;
} else if (arr[mid] > key) {
e = mid - 1;
} else {
s = mid + 1;
}
}
}
int main()
{
int n;
std::cout << "Enter Number of elements in the array: ";
std::cin >> n;
int arr[n];
std::cout << "Enter " << n << " sorted array elements: ";
for (int i = 0; i < n; i++) {
std::cin >> arr[i];
}
int key;
std::cout << "Enter element to be searched!";
std::cin >> key;
Binary(arr, n, key);
return 0;
}
I've got a menu that I've been working on, and everything now seems to work, except for the binary search function. I am entering the size of the array, filling the array, printing it, then sorting it with my chronological menu, and then when I do sequential search, it seems to work. However, binary search does not even return -1 to indicate that the number was not found, the program just stops. Suggestions? Thanks.
#include<iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int size=0; //global variable size
int a[100]; //global array
int getSize()
{
cout << "Array Size: ";
cin >> size;
return size;
cout << endl;
}
int sequentialSearch(int n)
{
for(int i=0;i<size;i++)
{
if(n==a[i])
{
return (i+1);
}
}
return -1;
}
int binarySearch(int n)
{
int low=0,high=size,mid;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==n)
{
return mid+1;
}
else if(a[mid]>n)
high=mid-1;
else if(a[mid]<n)
low=mid-1;
}
return -1;
}
void sort1()
{
int i, j;
for (i = 0; i < size-1; i++)
{
for (j = 0; j < size-i-1; j++)
if (a[j] > a[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
void sort2()
{
int i, j, m;
for (i = 0; i < size-1; i++)
{
m = i;
for (j = i+1; j < size; j++)
{
if (a[j] > a[m])
m = j;
}
int temp=a[m];
a[m]=a[i];
a[i]=temp;
}
}
void printMenu()
{
cout<<"0. Exit\n";
cout<<"1.Get the size needed for todays use of the array \n";
cout<<"2.Fill an array with random numbers from 1-100 \n";
cout<<"3.Print the array with position numbers \n";
cout<<"4.Sort the array in ascending sequence \n";
cout<<"5.Sort the array in descending sequence \n";
cout<<"6.Sequential search of the array for a target\n";
cout<<"7.Binary search of the array for a target\n";
}
void printTheArray()
{
for(int i=0;i<size;i++)
{
cout<<a[i]<<" is at position :"<<i+1<<endl;
}
}
void fillWithRandom()
{
for(int i=0;i<size;i++)
{
int x=rand()%101;
a[i]=x;
}
}
void dispatch(int ch)
{
switch(ch)
{
case 1:cout<<"Size of array :"<<getSize() << endl;
break;
case 2:fillWithRandom();
break;
case 3: printTheArray();
break;
case 4:sort1();
break;
case 5:sort2();
break;
case 6:
{
cout<<"Enter the number you want to search\n";
int t;
cin>>t;
int res1=sequentialSearch(t);
if(res1!=-1)
cout<<"Found in position :"<<res1<<endl;
else if(res1==-1)
cout<<"Not Found \n";
break;
}
case 7:
{
cout<<"Enter the number you want to search\n";
int t;
cin>>t;
int res=binarySearch(t);
if(res!=-1)
cout<<"Found in position :"<<res<<endl;
else if(res==-1)
cout<<"Not Found \n";
break;
}
default:cout<<"wrong choice\n";
}
}
int main ()
{
printMenu();
int choice;
cout<<"Type in a choice"<<endl;
cin>>choice;
while(choice!=0)
{
dispatch(choice); // one big switch statement
printMenu();
cout<<"Type in a choice"<<endl;
cin>>choice;
}
cout << endl;
// wrap-up
cout << "This program is coded by Troy Wilms" << endl; // fill in your name
// stops the program to view the output until you type any character
return 0;
}
for example, if you try finding 4 in list {1, 3, 5}, low will be always 0 and high will be always 2. because of the code 'low = mid - 1' I think it should be changed to 'low = mid + 1'
Sorry, I'm a brand new newbie to C++ and programming and I'm getting a heap corruption error. I think Im writing in unallocated memory but I can't seem to find where the error is... the program is soppuse to take user input values and rearrange them so that they would ascend. I'm also learning templates too.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
template <typename T>
void sort(T arrayz[], int size, char ch)
{
T temporary;
int k, j;
if (ch = 'a')
{
for (k = 0; k < size; k++)
{
for (j = 0; j < size; j++)
{
temporary = arrayz[j];
arrayz[j] = arrayz[j + 1];
arrayz[j + 1] = temporary;
}
}
}
}
int main()
{
int choices, range, i;
int x;
char ch;
cout << ("Enter the amount of numbers you want =>");
cin >> x;
int *numbers = new int[x];
if (!numbers)
{
cout << "Memory Allocation error!";
cin.get();
exit(1);
}
for (int i = 0; i<x; i++)
{
cout << "Option number" << i + 1 << " =>";
cin >> numbers[i];
}
cout << "Do you want ascending or descending values (a/d) =>" ;
cin >> ch;
if (ch = 'a')
{
sort(numbers, x, ch);
}
else if (ch = 'd')
{
sort(numbers, x, ch);
}
delete[] numbers;
fflush(stdin);
cin.get();
return 0;
}
In your sort function, you are accessing elements at index j + 1. However, this is out of bounds. The valid indexes for your arrayz array are 0 through size-1. When j is size-1, j+1 is size, which accesses past the end of the array.
I have the following code:
#include <iostream>
#include <string>
#include <cstring>
#include <stdlib.h>
using namespace std;
class sekuence
{
public:
sekuence() {
string emra[5][20];
int m=0;
}
void shto (){
int i;
int j;
char temp[20];
cout << "=============================="<<endl
<< "Shkruani emrat qe doni te shtoni ne vektor"<<endl;
for(i=0; i < 5; i++){
cin >> emra[i];
m++;
}
for(i=0; i<5; i++)
{
for( j=1; j<5; j++){
if(strcmp(emra[j-1],emra[j]) > 0) {
strcpy(temp,emra[j-1]);
strcpy(emra[j-1],emra[j]);
strcpy(emra[j],temp);
}
}
}
}
void print(){
for(int i=0; i<5 ; i++){
cout<<endl<<emra[i];
}
cout <<m;
}
bool kerko (const char *gjej)
{
for(int i=0; i<5; i++)
{
if(strcmp(emra[i],gjej) == 0 )
return true;
return false;
}
}
int n (){return m;}
private:
int m;
char emra[5][20];
};
int main()
{ char* gjej;
int x;
sekuence d;
do{
cout<<endl << " =========================" << endl;
cout<<"1 - Funksioni i shtimit" <<endl;
cout<<"2 - Funksioni i afishimit" <<endl;
cout<<"3 - Funksioni i kerkimit" <<endl;
cout<<"4 - Funksioni i fshirjes" <<endl;
cout<<"5 - Dalje nga programi" <<endl<<endl;
cout<<"====================================" <<endl;
cin >> x;
switch (x)
{
case 1:
d.shto();
break;
case 2:
d.print();
break;
case 3:
cout<<"==================================" <<endl
<< "Shkruani emrin qe doni te kerkoni"<<endl;
cin >> gjej;
d.kerko(gjej);
break;
case 4:
break;
case 5:
cout << "Dalje nga Programi" <<endl;
exit (0);
default:
cout <<"Zgjedhje e gabuar";
return 0;
}}
while (x != 5);
return 0;
}
Everytime I try to run the bool function I get an error..i don't what to do.
when i try to enter the nam i want to fine the function bool kerko() and quit the prog. and want find the char i want to find it opens the visual studio debugger.
You are trying to access gjej (char*) but no memory allocated to it. Try allocating some memory using new
char* gjej = NULL;
gjej = new char[NumberOfChars];
Edit: used new instead of malloc now..
std::string would be even nicer..
According to your indentation of the kerko function, one of the return statements should be moved out of the for loop:
bool kerko (const char *gjej)
{
for(int i=0; i<5; i++)
{
if(strcmp(emra[i],gjej) == 0 )
return true;
}
//*** Note the new location.
return false;
}
The layout you have makes the bool function always return before the 2nd iteration of the loop.