Showing no output - c++

So I was trying to count the total number of negative numbers in the stack and I wrote this code but something went wrong with it and its showing no output. I am a beginner in c++ so I am sure it will be quite wrong.
#include <iostream>
#include <stack>
using namespace std;
size_t i(stack<int> s){
int count=0;
while(s.size() !=0){
if(s.top()<0){
count++;
s.pop();
} else if(s.top()>0){
s.pop();
} else{}
cout<<count<<endl;
}
return count;
}
int main(){
stack<int> s;
s.push(-1);
s.push(2);
s.push(-2);
size_t i(stack<int> s);
return 0;
}

In your main() function you do not call i() you simply redeclare it.
#include <iostream>
#include <stack>
using namespace std;
size_t i(stack<int> s){
int count=0;
while(s.size() !=0){
if(s.top()<0){
count++;
s.pop();
} else if(s.top()>0){
s.pop();
} else{}
cout<<count<<endl;
}
return count;
}
int main(){
stack<int> s;
s.push(-1);
s.push(2);
s.push(-2);
size_t i(stack<int> s); // this DOES NOT call the function
i(s); // <== THIS calls the function!!!
return 0;
}
Your statement size_t i(stack<int> s); does not call the function, it simply tells the compiler what parameters it accepts and what its return type is.

Related

what does undefined reference to a recursive function mean? [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 2 years ago.
I am getting the following error while compiled using gdb:
/tmp/ccYfysv0.o: In function `main':
main.c:(.text+0xf1): undefined reference to `reverse'
collect2: error: ld returned 1 exit status
other answers say it might be due to mispelling of the function name, but here is the code that I am trying to compile.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int reverse(int,int,int*);
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int temp,n,i;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
int y = reverse(n,(int)0,&a[0]);
int reverse(int o,int k,int* p){
temp = *(p+k);
*(p+k)=*(p+o);
*(p+o) = temp;
o=o-1;k=k+1;
if(o==k)
{
return 0;
}
else if((k+1)==o){
temp = *(p+k);
*(p+k)=*(p+o);
*(p+o) = temp;
return 0;
}
else{
reverse(o,k,p);
}
}
for(i=0;i<n;i++)
printf("%d",a[i]);
return 0;
}
But when I compile it with g++, I get the following error:
expected a ';'
could someone please help me out with this problem.
You can't define reverse inside of main like that. Move it out separate from main, something on this general order:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int reverse(int o,int k,int* p){
temp = *(p+k);
*(p+k)=*(p+o);
*(p+o) = temp;
o=o-1;k=k+1;
if(o==k)
{
return 0;
}
else if((k+1)==o){
temp = *(p+k);
*(p+k)=*(p+o);
*(p+o) = temp;
return 0;
}
else{
reverse(o,k,p);
}
}
int main() {
int temp,n,i;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
int y = reverse(n,(int)0,&a[0]);
for(i=0;i<n;i++)
printf("%d",a[i]);
return 0;
}
You declared a function reverse(), but you did not define it outside of main() (you can't define a function inside of another function, like you did), so the linker can't find the implementation of reverse() to satisfy main's call to it.
Also, your reverse() function is missing a return statement in the final else block.
Try this instead:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int reverse(int,int,int*);
int main()
{
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n,i;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
int y = reverse(n,0,&a[0]);
for(i=0;i<n;i++)
printf("%d",a[i]);
return 0;
}
int reverse(int o,int k,int* p)
{
int temp = *(p+k);
*(p+k)=*(p+o);
*(p+o) = temp;
o=o-1;k=k+1;
if(o==k)
{
return 0;
}
else if((k+1)==o){
temp = *(p+k);
*(p+k)=*(p+o);
*(p+o) = temp;
return 0;
}
else{
return reverse(o,k,p);
}
}

Recursively remove all adjacent duplicates(Using stack)

I did it using and also checked previously it's been answered (Recursively remove all adjacent duplicates) using the same logic but when I applied that it's showing the segmentation fault! Please help in correcting the code. The link to the question is https://www.geeksforgeeks.org/recursively-remove-adjacent-duplicates-given-string/
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
string s,ans;
cin>>s;
stack<char>st;
int n=s.size();
cout<<n;
int i=0;
while(n--)
{
char b;
if(s[i]==st.top())
{
b=st.top();
st.pop();
i++;
continue;
}
if(s[i]==b)
{
i++;
continue;
}
st.push(s[i]);
b='1';
i++;
}
// char a[st.size()];
int j=0;
while(!st.empty())
{
// a[j]=st.top();
ans.push_back(st.top());
// cout<<st.top();
st.pop();
j++;
}
// cout<<ans<<endl;
}
//code
return 0;
}

How to fix void value not ignored in a function that returns string?

I'm getting this error and I don't know why...
I surfed on google but not got asolution can anyone pls help
I have no clue why is this happening.
I thought that the string return type was messing up but char* return type also didn't help.
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int isoperand(char x){
if(x=='+' || x=='-' ||x=='*' ||x=='/')
return 0;
else
return 1;
}
int precedence(char x){
if(x=='+' || x=='-')
return 1;
else if(x=='*' ||x=='/')
return 2;
return 0;
}
string convert(string infix){
stack<char> s;
string postfix;
int i=0,j=0;
while(infix[i]!='\0'){
if(isoperand(infix[i]))
postfix[j++]=infix[i++];
else{
if(precedence(infix[i])>precedence(s.top()))
s.push(infix[i++]);
else
postfix[j++]=s.pop(); //Error here.
}
}
while(!s.empty()){
postfix[j++]=s.pop();
}
return postfix;
}
int main(){
string infix="a+b*c";
string postfix;
postfix==convert(infix);
cout<<postfix;
return 0;
}
The return type of std::stack::pop() is void. Hence, you may not use:
postfix[j++]=s.pop();
You need to use:
postfix[j++] = s.top();
s.pop();
Since that line is in an else block, you'll have to use:
else
{
postfix[j++] = s.top();
s.pop();
}
Make the same change in the two places in your code that has the same error.
Other errors in your code:
You are accessing postfix using out of bounds indices. Instead of using postfix[j++] = ..., you can use postfix.push_back(...).
You are calling s.top() without checking whether s is empty. When s is empty, the call s.top() throws an exception in my environment.
Here's a fixed up version that works for me.
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int isoperand(char x){
if(x=='+' || x=='-' ||x=='*' ||x=='/')
return 0;
else
return 1;
}
int precedence(char x){
if(x=='+' || x=='-')
return 1;
else if(x=='*' ||x=='/')
return 2;
return 0;
}
string convert(string infix){
stack<char> s;
string postfix;
int i=0;
while(infix[i]!='\0')
{
if(isoperand(infix[i]))
{
postfix.push_back(infix[i++]);
}
else
{
if ( s.empty() )
{
s.push(infix[i++]);
}
else
{
if ( precedence(infix[i])>precedence(s.top()) )
{
s.push(infix[i++]);
}
else
{
postfix.push_back(s.top());
s.pop();
}
}
}
}
while(!s.empty()){
postfix.push_back(s.top());
s.pop();
}
return postfix;
}
int main(){
string infix="a+b*c";
string postfix;
postfix=convert(infix);
cout<<postfix;
return 0;
}
The is the error message:
main.cpp:40:36: error: void value not ignored as it ought to be
postfix[j++]=s.pop(); //Error here.
^
Error message indicates the return-value of a function is 'void', but you are trying to assign it to a non-void variable.
In your case, std::stack<T,Container>::pop() return type is void.

counting words on in an string consisting alphabets,\n,\t and space characters

Given a string consisting of spaces,\t,\n and alphabets,task is to count the number of words where spaces,\t and \n work as separators.
For this problem, I wrote following code, which doesn't works properly, and suggestions why?
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
bool isalpha(char ch)
{
if((ch>='A'&& ch<='Z')||(ch>='a'&& ch<='z'))
return true;
return false;
}
int main()
{
int t;
cin>>t;
while(t--)
{
string str;
int count=0;
getline(cin,str);
for(int i=0;i<str.length();i++)
{
if(isalpha(str[i]))
{
count++;
while(str[i]!='\n'||str[i]!='\t'||str[i]!=' ')
{
i++;
}
}
else
continue;
}
cout<<count<<endl;
}
return 0;
}

This program will compile but not run. Other programs run

So, I'm using dev-C++. The compiler works fine, a simple hello world program works along with about a dozen other simple programs. This is a work-in-progress of something I'm working on for class.
For me this compiles but it never runs. What's wrong with it?
#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>
using namespace std;
void getNames(vector<string> &vectorName, int &last, string temp);
int main() {
vector<string> names;
string tmp;
int last = 0;
getNames(names, last, tmp);
for(int j = 0; j < last; j++) {
cout << names.at(j) << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
void getNames(vector<string> vectorName, int &last, string temp) {
while (true) {
cout << "Enter a name (quit to stop): ";
cin >> temp;
if (temp == "quit") break;
vectorName.push_back(temp);
last = vectorName.size();
}
}
The program should fail to link because it can't find the definition for:
void getNames(vector<string> &vectorName, int &last, string temp);
That's because you're missing the & in your definition:
void getNames(vector<string> vectorName, int &last, string temp){
^^^^^^^^^^^
Add in the & and it should compile and run just fine.
First your getNames declaration and implementation signatures are not exactly the same.
void getNames(vector<string> &vectorName, int &last, string temp){
void getNames(vector<string> vectorName, int &last, string temp){