I'm trying to write a C++ that inputs n and then inputs n words. Then I input an s and then print out the n words with s removed. When I try to implement my program, my program says that there is an abort error. Does anyone know why? Thanks a lot.
#include <iostream>
using namespace std;
int main() {
int n;
string words[100];
cin >> n;
for (int i=0; i<n; i++) {
cin >> words[i];
}
string s; cin >> s;
for (int i=0; i<n; i++) {
int n = words[i].find(s);
words[i] = words[i].erase(n,n+s.length());
}
for (int i=0; i<n; i++) {
cout << words[i] << endl;
}
}
Related
This question already has answers here:
Why does std::getline() skip input after a formatted extraction?
(5 answers)
Closed 1 year ago.
I want to input multi-word strings into vector "name". Using cin works fine for single word inputs.
What i want is :
Take number of string inputs from the user. for example : "Random Name"
Store all the String value into the vector names
Here is the code i wrote
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<string> names;
string user;
int n;
cout << "Enter number of Users : ";
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> user;
names.push_back(user);
}
for (int i = 0; i < names.size(); i++)
{
cout << names[i] << " ";
}
}
Problem:
When i use getline() instead of cin in the for loop, it omits 1 input.
For example , if the user inputs - Number of Users = 3, it only takes 2 string inputs
string user[n]; // value of n given by user using cin
for (int i = 0; i < n; i++)
{
getline(cin, user[i]);
names.push_back(user[i]);
}
Try this:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<string> names;
string user;
int n;
cout << "Enter number of Users : ";
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> std::ws;
getline(cin,user);
names.push_back(user);
}
for (int i = 0; i < names.size(); i++)
{
cout << names[i] << " ";
}
}
cin>>std::ws;
The issue here is that the compiler is considering the newline after you are entering the number of inputs, and pressing enter. So, the fix is to extract the additional whitespaces. Check http://www.cplusplus.com/reference/istream/ws/
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
map<string,string> mym;
int n;
string s,a,r;
cin >> n;
for(int i=0; i<n; i++){
getline(cin,s);
getline(cin,a);
mym.insert(pair<string,string>(s,a));
s.erase();
a.erase();
}
for(int i=0; i<n; i++){
getline(cin,r);
if(mym.count(r)!=0){
cout << r << '=' << mym.at(r) <<endl;
}
else cout << "Not found" << endl;
r.clear();
}
return 0;
}
I tried to use map in problem but it seems wrong i can t find problem. Could you help me?
https://www.hackerrank.com/challenges/30-dictionaries-and-maps/problem
input
sam 99912222
tom 11122222
harry 12299933
sam
edward
harry
my result
Not found
=sam 99912222
=sam 99912222
This line:
cin >> n;
Will consume the number of the input, but won't consume the subsequent end of line that follows before the next string.
So instead of cin >> n, do this:
string tmp;
getline(cin, tmp);
n = atoi(tmp.c_str());
There's probably other ways to do this.
Some other code cleanup as follows:
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
map<string,string> mym;
int n;
string tmp;
cin >> tmp;
n = atoi(tmp.c_str());
for(int i=0; i<n; i++) {
string s, a;
getline(cin,s);
getline(cin,a);
mym[s] = a;
}
for(int i=0; i<n; i++) {
string r;
getline(cin,r);
auto itor = mym.find(r);
if(itor != mym.end())
cout << r << '=' << itor->second <<endl;
}
else {
cout << "Not found" << endl;
}
}
return 0;
}
#include <iostream>
using namespace std;
int main(){
int n;
cout << "No. of values : ";
cin >> n;
int array[n];
for (int i=0; i<n; i++)
{
cin >> array[i];
}
return 0;
}
You can use std::cout like :
#include <iostream>
using namespace std;
int main(){
int n;
cout << "No. of values : ";
cin >> n;
int array[n];
for (int i=0; i<n; i++)
{
cin >> array[i];
if(i ==0)
std::cout<<"{" <<array[i];
else if(i == n-1)
std::cout<<","<<array[i]<<"}";
else
std::cout<<","<<array[i];
}
return 0;
}
#mystic's answer uses arrays, which works fine. You can also use vector. There are more advanced methods of iterating over a vector, but I have not included that here to keep it simple.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> intVector{};
int n;
int input;
cout << "No. of values : ";
cin >> n;
for (int i = 0; i < n; i++) {
cin >> input;
intVector.push_back(input);
}
// Print out the array
cout << "{";
for(int i = 0; i < intVector.size(); i++) {
cout << intVector[i];
// print out the comma, except for the last number
if(i < intVector.size() - 1) {
cout << ", ";
}
}
cout << "}" << endl;
return 0;
}
If you want to use an iterator for printing the array, you can replace the print loop with this code:
// Print out the array
cout << "{";
for(auto i=intVector.begin(); i!=intVector.end(); ++i) {
if (i != intVector.begin()) {
cout << ", ";
}
cout << *i;
}
cout << "}" << endl;
I want to print cardinality of a set using array or vector and membership of an element.
I am able to find cardinality but but can't find membership in same program.
Here is my code..
#include<iostream>
#include<vector>
using namespace std;
int main(){
vector<int> v;
int ch;
int j;
for(int i=1; cin>>j; i++){
v.push_back(j);
}
cout << v.size();
cout << " enter element you want to find whether it is a memebr or not: ";
cin >> ch;
for(int i=0; i < v.size(); i++){
if(v[i] == ch){
cout << "element found";
break;
}
}
return 0;
}
See what you did is taking an input during runtime inside a loop that too at condition validation place.Since to terminate this you gave a char. It throws an exception for that.So your program is not working as expected. Dont ever do that. Always ask for the size and then iterate the loop for that many number of times. Here is the code which is working :
#include<iostream>
#include<vector>
using namespace std;
int main(){
vector<int> v;
int ch;
int j,temp;
int size;
cin>>size;
for(int i=1; i<=size; i++){
cin>>temp;
v.push_back(temp);
}
cout << " enter element you want to find whether it is a memebr or not: ";
cin >> ch;
for(int i=0; i < v.size(); i++){
if(v[i] == ch){
cout << "element found";
break;
}
}
cout<<v.size();
return 0;
}
Hope this would help you :)
Try this:
#include <iostream>
using namespace std;
#include<vector>
#include<stdlib.h>
int main(){
vector<int> v;
int ch;
int j;
cout<<"Enter the size of set:"<<endl;
int n;
cin>>n;
for(int i=0; i<n ; i++){
cin>>j;
v.push_back(j);
}
cout << v.size()<<endl;
cout << " enter element you want to find whether it is a memebr or not: ";
cin >> ch;
for(int i=0; i < v.size(); i++){
if(v[i] == ch){
cout <<endl<< "element found";
exit(0);
}
}
cout<<endl<<"Element not found.";
return 0;
}
I tried solving a problem in the Google Code Jam Practice Page Minimum Scalar Product and i have the program written in c++ and i read in the FAQ page that we have to test our programs with the .in test file placed on the practice page for download but i don't know how and i use UBUNTU 12.04 LTS & please i am participating in the contest for the first time..So any help would be greatly appreciated..Thanks In Advance
I tried
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int numCase;
cin >> numCase;
int i, j, n;
long long c;
for (i = 0; i < numCase; i++)
{
cin >> n;
vector<long long> array1, array2;
for (j = 0; j < n; j++)
{
cin >> c;
array1.push_back(c);
}
for (j = 0; j < n; j++)
{
cin >> c;
array2.push_back(c);
}
sort(array1.begin(), array1.end());
sort(array2.begin(), array2.end(), greater<long long>());
long long ans = 0;
for (j = 0; j < n; j++)
ans += (array1[j] * array2[j]);
cout << "Case #" << (i+1) << ": " << ans << endl;
}
return 0;
}
You can use ifstream and ofstream
as follows:
#include <vector>
#include <algorithm>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin("input.in");
ofstream fout("output.out");
//-- check if the files were opened successfully
if (!fin.is_open()) cout << "input.in was not open successfully" << endl;
if (!fout.is_open()) cout << "output.out was not open successfully" << endl;
int numCase;
fin >> numCase;
int i, j, n;
long long c;
for (i = 0; i < numCase; i++)
{
fin >> n;
vector<long long> array1, array2;
for (j = 0; j < n; j++)
{
fin >> c;
array1.push_back(c);
}
for (j = 0; j < n; j++)
{
fin >> c;
array2.push_back(c);
}
sort(array1.begin(), array1.end());
sort(array2.begin(), array2.end(), greater<long long>());
long long ans = 0;
for (j = 0; j < n; j++)
ans += (array1[j] * array2[j]);
fout << "Case #" << (i + 1) << ": " << ans << endl;
}
fin.close();
fout.close();
return 0;
}
You can treat fin and fout as cin, so instead of reading the input from the console, you read the input from the file in.txt. Instead of writing to the console using cout you write to output.out using fout.
The general format for test cases are:
int t;
cin >> t;
while (t--) (
{
//code here
}
So we make a variable for our test cases and ask the user to input a value for it... then we make a while loop which checks if the value of t > 0 and decrements the value by 1 every time)
Hope I helped! :D