#include<iostream>
#include<string.h>
using namespace std;
int main()
{
int n;
cin>>n;
int x=0;
while(n--)
{
char s[3];
cin>>s;
if(strcmp(s,"X++")==0||strcmp(s,"++X")==0)
x+=1;
else
x-=1;
}
cout<<x;
}
The loop worked fine when I removed the strcmp line inside the if statement.
Like πάντα ῥεῖ said, the size of s must be 4 in order to store the '\0' character. Otherwise :
cin >> s; // For example write "X++"
will store your string in s and put '\0' out of the bounds of s.
When the program goes in the loop for the second time, you won't be able to write it because it will get "" (an empty string) every time.
To fix that, just replace 3 with 4 and that will be OK.
Note : If you really want to use C++, I suggest you to use std::string.
#include<iostream>
#include<string>
using namespace std;
int main()
{
int n;
cin >> n;
int x = 0;
while(n--)
{
string s;
cin >> s;
if(s == "X++" || s == "++X")
x+=1;
else
x-=1;
}
cout << x;
return 0;
}
Related
I have written a code in c++ to count zeros. The input have to take as string. The input will be a digit with 1 and 0.
Example:
Input: 1000000001
Output: 8
The answer should be 8 but my code is showing 0. Where is the problem in the code.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string players;
cin >> players;
int count = 0;
for (int j = 0; j < players.length(); j++)
{
if (players[j] == 0)
{
count++;
}
}
cout << count;
return 0;
}
Please help me to find and solve it.
Thank you.
you need to compare with char not int:
if (players[j] == '0')
if condition for players[j] == 0 is wrong, your input is string type while you are trying to compare with the integer type 0. The correct if check is players[j] == '0'. Using bits/stdc++.h and using namespace std; is a big no in C++.
#include <iostream>
#include <string>
int main()
{
std:: string players;
std:: cin >> players;
int count = 0;
for(const char &player : players)
{
if (player == '0')
{
count += 1;
}
}
std:: cout << count;
return 0;
}
the condition is wrong.
write code like
players[j] == "0"
your condition means that
player[j] == NULL
I'm dealing with a competitive programming challenge in which I have to take a line of space-separated integers from standard input, put them into an array, and treat them in a certain way. The problem is that I don't know how many integer I may get in each test case.
In case I know, my code would be like:
int n; // number of integers;
int arr[n];
for(int i = 0; i < n; i++)
cin >> arr[i];
In case I don't have 'n', how would I achieve the same thing?
std::vector<int> is basically a dynamically-sized array of ints. You can keep adding stuff to it and it will grow as necessary. If you are given a number of elements as the first input, you can do something like:
std::vector<int> items;
int count;
std::cin >> count;
// Preallocates room for the items. This is not necessary, it's just an optimization.
items.reserve(count);
while (count > 0) {
int item;
std::cin >> item;
items.push_back(item);
--count;
}
If you are not given the number of items, just read until reading fails:
std::vector<int> items;
int item;
while (std::cin >> item) {
items.push_back(item);
}
Use vectors beacuse vectors are dynamic in size. Keep pushing elements into vector until inputs are there.
std::vector<int> v;
int temp;
while (std::cin >> temp) {
v.push_back(temp);
}
When you will be given the value of n. You can follow any of the following two steps:
Step: 1
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int n;
cin >> n; // Input n
vector<int>vv(n); // It will declare a vector(similar to an array) of size n
for(int i = 0; i < n; i++)
{
cin >> vv[i];
}
return 0;
}
Step: 2
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int n, number;
cin >> n; // Input n
vector<int>vv; // It will declare an empty vector
for(int i = 0; i < n; i++)
{
cin >> number; // Take a number as input
vv.push_back(number); // Put the input to the last of the vector
}
return 0;
}
When you will not be given the value of n:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int number;
vector<int>vv; // It will declare an empty vector.
while(cin >> number)
{
vv.push_back(number); // Push(put) the input to the back(end/last) of the vector
}
/* In case of reading input from a file,
the loop will continue until the end of the file.
When you'll try it from console, you need to enter
end-of-file command from keyboard.*/
return 0;
}
I was creating a code for rotation of specif number in a string and initially i created it for one user input ! It worked as soon as i added on logic for multiple test case its is showing floating point exception.....
can anyone tell me in the code what caused that error !
#include<iostream>
#include<cstring>
using namespace std;
void stringrot(char str[],int n)
{
int l=strlen(str);
char temp[100];
int k=n%l;
for(int i=0;i<k;i++)
{
temp[i]=str[(l-k+i)];
}
for(int i=0;i<l-k;i++)
{
temp[k+i]=str[i];
}
temp[l]='\0';
cout<<temp;
}
int main()
{
int test;
cin>>test;
while(--test>=0)
{
char str[100];
cin.getline(str,50);
int n;
cin>>n;
stringrot(str,n);
}
}
Here is the code !
Let's take a closer look at the while loop in main
while(--test>=0)
{
char str[100];
cin.getline(str,50);
int n;
cin>>n;
stringrot(str,n);
}
Loop Iteration 1
cin.getline(str,50); << reads a line
cin>>n; << reads an integer. After enter is pressed. Enter
is not an integer. Enter is not read and stays
in the stream
stringrot(str,n);
Loop Iteration 2
cin.getline(str,50); << reads a line. Fortunately there is an enter
still in the stream making it really easy to
find the end of the line. It's the first
character that's going to be read.
cin>>n; << reads an integer. Unfortunately the user
probably thinks they are typing in the string
and doesn't type in a number. Oops.
stringrot(str,n);
Anyway stringrot gets called with stringrot("",0), so the first few lines of stringrot wind up looking like
int l=strlen(""); << l will be zero because the string is emtpy
char temp[100];
int k=n%l; << resolves to 0%0 and an integer divided by zero is BOOM!
Solution:
Change the guts of the while loop to something like
char str[100];
cin.getline(str,50);
int n;
cin>>n;
// discard everything else on the line including the end of line character
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
stringrot(str,n);
Why an integer division results in a floating point error, one that isn't a catchable C++ exception by the way, seems to be mostly historical. More here: Why is this a floating point exception?
Recommendation:
Use std::string not char arrays.
#include<iostream>
#include<cstring>
using namespace std;
//void stringrot(char str[],int n)
void stringrot(string str,int n)
{
//int l=strlen(str);
int l=str.size();
char temp[100];
int k=n%l;
for(int i=0;i<k;i++)
{
temp[i]=str[(l-k+i)];
}
for(int i=0;i<l-k;i++)
{
temp[k+i]=str[i];
}
temp[l]='\0';
cout<<temp;
}
int main()
{
int test;
cin>>test;
while(--test>=0)
{
//char str[100];
//cin.getline(str,50);
string str;
cin>>str;
int n;
cin>>n;
stringrot(str,n);
}
}
**Example:-Animals ( Reptiles Birds ( Eagles Pigeons Crows ) ) as input? **
I am not getting the answer for this input Animals ( Reptiles Birds ( Eagles Pigeons Crows ) )
#include<vector>
#include<string>
#define ll long long
#include <iostream>
using namespace std;
int main()
{
ll n,m,k,cb=0,ob=0;
cin>>n;
string c;
vector<string> s;
while(cb!=ob)
{
cin>>c;
if(c=="(")
ob++;
else if(c==")")
cb++;
s.push_back(c);
}
for(ll i=0;i<s.size();i++)
cout<<s[i];
return 0;
}
First problem is that the first thing you read is a long long value. Animal just isn't.
You are checking for while(cb!=ob). Some lines above cb and ob are both initialized with 0. so your loop will never run.
Even if you fix your loop the next problem will be that your input doesn't start with a (. So after reading Animal your loop will exit.
The third problem is your output. You don't flush cout when you are done.
Also: Why do you use long long for i? std::vector::size() returns size_t. So please stick with that.
One "easy" solution would be something like this:
#include<vector>
#include<string>
#include <iostream>
using namespace std;
int main()
{
int cb = 0, ob = 0;
string c;
vector<string> s;
while((ob==cb && ob==0) || ob!=cb)
{
cin >> c;
if (c == "(")
ob++;
else if (c == ")")
cb++;
s.push_back(c);
}
for (size_t i = 0; i < s.size(); i++)
{
cout << s[i];
}
cout << endl;
return 0;
}
One other thing: This code won't work if the parenthesis won't match (endless loop or premature break) and won't handle text behind the last closing parenthesis.
As your question isn't really specific about what you want my answer is just for showing you the main problems.
I would also suggest that you check your input stream for errors.
Consider this question: Ambiguous Permutation. My code is written in C++11 using G++ 4.7.2
#include<iostream>
#include<vector>
using std::cin;
using std::cout;
using std::vector;
int main()
{
int h;
while((cin >> h) && (h!=0))
{
int num;
bool c=true;
vector<int> arr;
while((cin >> num) && (h!=0))
{
arr.push_back(num);
--h;
}
const auto n = arr.size();
for(int i=0; i!=n; ++i)
{
if(arr[arr[i]-1] != (i+1))
{
c = false;
cout << "Not Ambiguous\n";
break;
}
}
if(c==true)
cout << "Ambiguous\n";
}
return 0;
}
This code works fine as long as I enter every number in a newline. If I start providing inputs separated by white space (for permutation) then the code behaves unexpectedly. It demands an invalid input for the condition while((cin >> num) and exits after one round of input. Any suggestions for removing this issue? I was also wondering if using STL Arrays would be good choice or not. Thanks!
You are taking input incorrectly, specifically here-
while((cin >> num) && (h!=0)) //WRONG!
{
arr.push_back(num);
--h;
}
A clearer (hence bug free) way to do it is-
for (int i = 0; i < h; i++)
{
cin >> num;
arr.push_back(num);
}
For your second question-
I was also wondering if using STL Arrays would be good choice or not.
I would suggest using std::vector here (and for most purposes).
You code has something wrong there.
1. You should use h!=1 when you get the numbers for each permutation, in your current implementation, it is weird, if h =4, you are going to ask for 5 numbers, which does not make sense
2. You can certainly read those numbers for each test case in one line separated by space as follows:
#include<iostream>
#include<vector>
using std::cin;
using std::cout;
using std::vector;
int main()
{
int h;
while((cin>> h) && (h!=0))
{
int num;
bool c=true;
vector<int> arr;
while((cin >> std::skipws >> num) && (h!=1))
{ //^^^^^^^^^^^^^(add this) ^^^^^^^here should be 1 not 0
arr.push_back(num);
--h;
}
const auto n = arr.size();
for(int i=0; i!=n; ++i)
{
if(arr[arr[i]-1] != (i+1))
{
c = false;
cout << "Not Ambiguous\n";
break;
}
}
if(c==true)
cout << "Ambiguous\n";
}
return 0;
}
However, in either cases, your results are not right, it outputs the following:
4
1 4 3 2
Not Ambiguous
5
2 3 4 5 1
Not Ambiguous
1
1
Ambiguous
While the first should be "Ambiguous", you may need to check the logic for checking ambiguity.