Problem in taking input while running a c++ program - c++

Hey I was solving a stack problem and i have written my logic for it but when i run my program .
And providing 3 as input.​
(a+(b*c))+(d/e)​
((())(()))
((((()
It is not taking input correctly .At the end of the 3 there is question mark symbol appearing .
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<stack>
using namespace std;
int main()
{
long int t;
cin>>t;
while(t--){
stack< pair<char,int> > stk1;
string s;
cin>>s;
// getline(cin, s);
vector<int> vect;
int i=0,k=1,l=1;
while(s[i] != '\0' ){
if(s[i]=='('){
stk1.push( {'(' , k} );
vect.push_back(k);
k++;
}
else if( s[i]== ')'){
if(stk1.empty()){
vect.push_back(l);
l++;
}else{
vect.push_back( stk1.top().second);
stk1.pop();
}
}
i++;
}
for(int z=0;z<vect.size();z++){
cout<<vect[z]<<" ";
}
cout<<"\n";
}
return 0;
}

I was using DEV C++ in that only this error was comming not in code blocks .
so there was saome problem in dev c++ only.

Related

Wrong Answer,Can't find out whats wrong in my code

I have already done this question with basic string methods,I am trying this question with stacks but getting wrong answer even my all test cases working correctly.
question link :https://www.codechef.com/problems/COMPILER
I really want to learn DSA hardly,please help me out and also give some suggestion in my coding style(i think i write code in very bad manner).
here is my code:
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--){
string str;
cin>>str;
class Node{
public:
char value;
int indexvalue;
};
std::stack<Node> f;
int start=0;
int end=0;
int k=-1;
for(int i=0;i<str.size();i++){
char s = str[i];
if(i == 0){
if(s == '>'){
break;
}
}
if(s=='<'){
Node n;
n.value=s;
n.indexvalue=i;
f.push(n);
}
else{
if(!f.empty()){
Node tempnode=f.top();
f.pop();
int tempstart=tempnode.indexvalue;
int tempend=i;
int tempk=tempend-tempstart;
if(tempk>k){
k=tempk;
start=tempstart;
end=tempend;
}
}
}
}
cout<<k+1<<endl;
}
}

scanf for both character and integer

I am trying to take input character and integer both. But when i use cin>>ch>>val; for taking input,it works.But Using scanf("%c%d",&ch,&val);,it shows me run time error.What can i do to get rid of this problem? I want to use scanf for faster input.
Here is my partial code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int q;
scanf("%d",&q);
while(q--)
{
char ch;
int val,in;
//cin>>ch>>val;
scanf("%c %d",&ch,&val);
in=val;
if(ch=='a'){
//scanf("%d",&val);
//update(1,0,m,++indx,val);
printf("First Case\n");
}else{
//si(in);
//if(in>tree[1]) printf("none\n");
//else query(1,0,m,in);
printf("Second Case\n");
}
}
}
I ran this code! It works fine for me. So probably you're using scanf in C++ and forgot to include or maybe it's something else with your code.
Can you share the full code. There is no issue with scanf() function as you have mentioned. You can take multiple inputs like scanf(%d%d%c%c,&a,&b,&c,&d);
#include <stdio.h>
int main()
{
printf("Hello World");
int q=1;
while(q--)
{
char ch;
int val,in;
scanf("%c%d",&ch,&val);
in=val;
if(ch=='a'){
printf("%d",val);
}else{
printf("%d",val);
}
}
return 0;
}

Runtime error "SIGCONT" while finding lapindromes using c++

I was trying to solve lapindrome problem on codechef.
On my local ide the program ran as expected.
But while in codechef submission it is giving runtime error "SIGCONT".
#include<iostream>
#pragma GCC optimize("Ofast")
using namespace std;
int main() {
int t;
cin>>t;
do
{
string l;
cin>>l;
int c = 0;
for (int i = 0; i < (l.length()/2); i++)
{
if (l[i] == l[i+(l.length()/2)])
{
c++;
}
}
if (c == l.length()/2) {
printf("YES\n");
}
else
{
printf("NO\n");
}
t--;
} while (t!=0);
return 0;
}
First thing I would like to point out that your code is wrong because for the test case rotor your code is giving NO as an answer, it should be YES.
Why getting the wrong answer?
In question it is given, if there are odd number of characters in the string, we ignore the middle character and check for lapindrome.
So, you need to modify/delete the below code.
if (c == l.length()/2) {
printf("YES\n");
}
else
{
printf("NO\n");
}
and instead do this:
if (c == l.length()/2) {
//check for lapindrome
}
else
{
//remove middle character
//check for lapindrome
}
Why "SIGCONT" error?
This error occurs when you exceed the IDE's printing limit. For Codechef IDE, it’s around 2^16 Characters.
You can try printing the output directly in a file locally. (Thanks to black_truce for this).
My Code:
You can check below solution for reference.
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
string str,str1,str2;
cin>>str;
int len=str.length();
if(len%2==0){
str1=str.substr(0, len/2);
str2=str.substr(len/2, len);
}
else{
str1=str.substr(0, len/2);
str2=str.substr(len/2+1, len);
}
sort(str1.begin(), str1.end());
sort(str2.begin(), str2.end());
if(str1==str2) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}

What is exactly length_error in string and how to remove it

I made the following code and when I execute it I get this error:
terminate called after throwing an instance of 'std::length_error'
what(): basic_string::_S_create
0Aborted (core dumped)
#include<stdio.h>
#include<string>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
bool comp(char c1, char c2)
{
return c1 < c2;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
string str;
getline(cin,str);
sort(str.begin(), str.end(), comp);
int k;
long long int sum40=0,sum41=0;
scanf("%d",&k);
cout<<str.length();
for(unsigned int i=0;i<str.length();++i)
{
if(str[i]=='(')
sum40++;
else
sum41++;
}
if(sum40!=sum41 && k==1)
cout<<str;
else
if(sum40!=sum41 && k!=1)
printf("-1");
else
{
while(k>0 || (sum40==sum41))
{
str.erase(str.begin());
k--;
}
cout<<str;
}
}
return 0;
}
I want to know what is std::length_error error and how do i remove that.
I know it is common problem. I saw a bunch of article on this but those article did not solve my problem.
Any help will be appreciated
Regards
It means the string you are trying to create is larger than std::string::max_size(). See the Remarks section in the link below.
https://msdn.microsoft.com/en-us/library/as4axahk(VS.80).aspx
You can change string capacity using the string::reserve function: http://www.cplusplus.com/reference/string/string/reserve/

how to decleare a 1D double array

Data input from the keyboard.The length of array maybe very long.
I want to finish inputing when press ENTER twice.
the numbers separated by space, tab or ",". how to detect the length n?
I have tried like this:
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
void main()
{
bool flag=true;
unsigned n=0,i;
double x;
string line,str;
istringstream iss;
cout<<"input your numbers."<<endl;
count<<"Press the Enter key twice finish data inputting."<<endl;
while(flag)
{
getline(cin,line);
str+=line+' ';
if(line.empty())
flag=false;
}
// get the length n
iss.str(str);
while(iss>>x)
{
n++;
}
double *v=new double[n];
iss.seekg(0);
for(i=0;i<n;i++)
iss>>v[i];
for(i=0;i<n;i++)
{
cout<<v[i];
if(i<n-1)
cout<<' ';
}
cout<<endl;
delete []v;
}
I am a novice. Help me, Please!
Try this After taking the input in variable line do this:
#include<iostream>
#include<string>
#include<sstring>
using namespace std;
void main()
{
bool flag=true;
unsigned n,i=0;
double x;
string line,str;
istringstream iss;
cout<<"input your "
getline(cin,line);
int c=0;
char * pch;
pch = strtok (line," ,");// this will work for space and comma but you can add your own specifiers
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,");
c++;
}
return(c);// will give the length of arry.
}
Man to declare 1D array
the syntax is
void main()
{
int array[5];
//to initalize;
for(int i=0;i<5;i++)
{
array[i]=i;
}
for(int i=0;i<5;i++)
{
cout<<array[i]<<" ";
}
// and to declare dynamically
int * array=new int[5];
}
I have solved this problem.
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
void main()
{
bool flag=true;
unsigned n=0,i;
double x;
string line,str;
istringstream iss;
cout<<"input your numbers."<<endl;
cout<<"Press the Enter key twice finish data inputting."<<endl;
//Brecause data may come from clipboard and have multi line.
while(flag)
{
getline(cin,line);
str+=line+'\n';
// vc++ 6.0 have a bug in include file: STRING. You shoud fix it.
//Or you shoud press ENTER more than twice to terminate inputing.
//Replace I.rdbuf()->snextc(); to _I.rdbuf()->sbumpc();
if(line.empty())
flag=false;
}
// get the length n
iss.str(str);
while(iss>>x)
{
n++;
}
double *v=new double[n];
iss.clear();// very important.
// initialize v[n]
iss.str(str);
for(i=0;i<n;i++)
iss>>v[i];
// output v
for(i=0;i<n;i++)
{
cout<<v[i];
if(i<n-1)
cout<<' ';
}
cout<<endl;
delete []v;
}