scanf for both character and integer - c++

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;
}

Related

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;
}

C++ (SPOJ - POL-polowa) - problem with compilation

I have problem with making correct code for this task in SPOJ https://pl.spoj.com/problems/POL/.
When I wrote all what I needed, program worked correctly. But when I tried to convert it in function, I have on ideone.com problem like this -> double free or corruption (out). Can anybody help me please ? What I am doing wrong ? I am beginner and I realize that can be very trivial.
#include <iostream>
using namespace std;
int polowa()
{
int t;
cin>>t;
string slowa[100]={};
string nowe_slowa[100]={};
for (int i=0;i<t;i++)
{
cin>>slowa[i];
}
for (int i=0;i<t;i++)
{
int k=slowa[i].length()/2;
nowe_slowa[i]=slowa[i].substr(0,k);
cout<<nowe_slowa[i]<<endl;
}
}
int main()
{
polowa();
return 0;
}
int polowa() promises to return an int, but the return keyword is conspicuously absent from the function.
Looking over the code, you never use more than one element at a time. Odds are good you could rewrite this function to use no arrays and only one loop. – user4581301
You don't have to separate reading input and print output. Arrays are not needed. Proposed implementation:
#include <iostream>
#include <string>
int main()
{
unsigned tests;
std::cin >> tests;
while (tests--)
{
std::string word;
std::cin >> word;
std::cout << word.substr(0, word.length() / 2) << "\n";
}
}
According to Dessus proposition I wanted to make this code as a function called in int main(). But SPOJ can't accept me this code (when i am compiling this in code blocks, everythings okay).
#include <iostream>
#include <string>
std::string nowe_slowo(int t)
{
while (t--)
{
std::string word;
std::cin >> word;
std::cout<<word.substr(0, word.length() / 2)<<"\n";
}
}
int main()
{
unsigned tests;
std::cin >> tests;
nowe_slowo(tests);
}

Character replacement not working

enter image description hereI have the following code that is supposed to replace instances of pi (3.14) with the word "pi". For example, "x3.14 3.14 3.14xx" should be turned into "xpi pi pixx". However, my code isn't doing that; why doesn't it work, and how would I fix it?
#include<iostream>
#include<cstring> //dsfsdf
#include<string>
#include<cstdio>//sdfdsf
using namespace std;
void replacepi(char *arr,int i)
{
//base
if(arr[i]=='\0')
{
cout<<arr<<endl;
return;
}
//recc
if(arr[i]=='3' && arr[i+1]=='.' && arr[i+2]=='1' && arr[i+3]=='4')
{
arr[i]='p';
arr[i+1]='i';
for(int j=i+4;arr[j]!='\0';j++)
arr[j-2]=arr[j];
arr[strlen(arr)-2]='\0';
}
replacepi(arr,i+1);
}
int main() {
long int n;
cin>>n;
for(int i=0;i<n;i++)
{
char arr[1000];
cin.getline(arr,sizeof(arr));
replacepi(arr,0);
}
return 0;
}
Looks like a job for regex_replace. Given you've read your input into arr you can just dump the replacement to the console like this:
regex_replace(ostream_iterator<char>(cout), cbegin(arr), cend(arr), regex("3\.14"), "pi")
Live Example
Naturally arr should be a string as in the example rather than a char[], as given any size char[] the input may be larger, a string will always work.

char Array in C++ is not functioning properly

I am coding a C++ program using char array.But i think it is giving problems.
This is my code:
#include <iostream>
#include<string.h>
using namespace std;
int main() {
long int t;
cin>>t;
char total[500],a[500],b[500];
cin>>total;
int len=strlen(total);
//cout<<strlen(total);
for(int i=0;i<len/2;i++){
a[i]=total[i];
}
for(int i=0;i<len/2;i++){
b[i]=total[i];
}
cout<<a<<endl;
cout<<b;
return 0;
}
It is not printing the arrays. But when i commented out
/*for(int i=0;i<len/2;i++){
b[i]=total[i];
}*/
it is printing array a as expected. What is the problem here?
You arrays are not NULL-terminated. If I add a[len/2]=0; and b[len/2] = 0; after the for loops, it works correctly.

Segmentation fault(SIGSEGV) in my code

Im getting Segmentation fault(SIGSEGV) for many problems that iv solved in spoj and other websites. Im giving the problm statement link and code. Can anybody tell me the mistakes that iv done below.
Problem :
http://goo.gl/CVROl
Thanks in advance.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int main()
{
char a;
string final,dummy;
int t,h,p,k,z;
scanf("%d",&t);
while(t)
{
cin>>a>>h>>p;
final=a;
for(int i=0;i<h;i++)
{
k=0;
for(int j=0;final[j]!='\0';j++)
{
if(final[j]=='a')
{
dummy[k]='a';
dummy[k+1]='b';
k+=2;
}
else
{
dummy[k]='b';
dummy[k+1]='a';
k+=2;
}
}
final=dummy;
}
printf("%c\n",final[p-1]);
t--;
}
return 0;
}
As pointed out by sstn, you did not allocate memory for dummy. Since it's a string and it looks like you just want to append characters to it, you can do:
for(int j=0;j < final.size();j++)
{
if(final[j]=='a')
{
dummy.push_back('a');
dummy.push_back('b');
}
else
{
dummy.push_back('b');
dummy.push_back('a');
}
}
final=dummy;
final is of type std::string, which aren't null terminated.
In your for loop: for(int j=0;final[j]!='\0';j++) you're checking for the end of the string as you would do in C (which a char* -- null terminated string), but in C++ you should iterate over string characters in some other ways: using an iterator, counting up the string size or something.
A fast quick to this problem (don't know whether there are others) is to get the null terminated char* representation of final: final.c_str() and iterater over it as you're doing.