I am learning about vector pairs, mostly I can take other datatypes easily but while taking string as input it always shows no matching operator>> error at the cin>>s statement, I am attaching a small snippet of the code (its incomplete though for purpose)-
#include <iostream>
#include <string>
#include <vector>
#include <utility>
using namespace std;
int main()
{
int T;
cin>>T;
while(T--)
{
unsigned int N;
cin>>N;
vector <pair<int,string>> v(N);
for(unsigned int i = 0;i<N;i++)
{
string s[200];
cin>>s;
v[i].make_pair(i+1,s);
}
}
return 0;
}
Declaring an array for storing a single string does not make sense here. For your purposes you can simply declare a normal string variable as:
for(unsigned int i = 0;i<N;i++)
{
string s;
cin>>s;
v[i].make_pair(i+1,s);
}
You have basically made an array of string by using the statement
string s[200];
Taking an input in s is basically meaningless here.
You would have to take an input in a particular index of s.
Something like cin>>s[0]
Another error is when you make a pair, the type you have specified for the pair is <int,string>. The pair you make here will be <int,string*>
Related
I have no idea what the problem is.
#include <iostream>
#include <algorithm>
#include <fstream>
#include <set>
using namespace std;
int main() {
ifstream infile("meeting.in");
int FF, NumPaths;
infile >> FF >> NumPaths;
int Paths[NumPaths][4];
set<int> Bessie_Times[FF];
set<int> Elsie_Times[FF];
for(int i=0;i<NumPaths;i++)
{
infile >> Paths[i][0] >> Paths[i][1] >> Paths[i][2] >> Paths[i][3];
}
sort(Paths,Paths+NumPaths);
}
At these lines, I get these errors:
int Paths[NumPaths][4];
Array type 'int[4]' is not assignable
set<int> Bessie_Times[FF];
Error 1: Array initializer must be an initializer list
Error 2: variable length array of non-POD element type 'set'
Does anyone know what is causing this? I have researched around, but could not seem to find anything that resolved the problem. I assume I am trying to use a variable type where I shouldn't be, but I cannot find an instance of this.
g++ compile your code fine(except sort). I don't know is that violates standard or not. But anyway in c++ you should use containers from stdlib:
#include <iostream>
#include <algorithm>
#include <fstream>
#include <set>
#include <array>
using namespace std;
int main()
{
ifstream infile("meeting.in");
int FF, NumPaths;
infile >> FF >> NumPaths;
std::vector<array<int, 4>> Paths(FF);
std::vector<set<int>> Bessie_Times(FF);
std::vector<set<int>> Elsie_Times(FF);
for(int i = 0; i < NumPaths; i++)
{
infile >> Paths[i][0] >> Paths[i][1] >> Paths[i][2] >> Paths[i][3];
}
sort(Paths.begin(), Paths.end());
return 0;
}
Not sure about sorting; maybe you need to add
sort(Paths[i].begin(), Paths[i].end());
inside loop
int Paths[NumPaths][4];//Array type 'int[4]' is not assignable
You are attempting to statically declare an array with a length not known at compile-time, but only known at run-time. You cannot do this as C++ does not allow it. You can, however, dynamically declare an array of which length is known at run-time:
int Paths[NumPaths][4]; // wrong
int *Paths[4]; // right
for (int i = 0; i < 4; i++)
Paths[i] = new int[NumPaths];
set<int> Bessie_Times[FF];//Error 1: Array initializer must be an initializer list Error 2: variable length array of non-POD element type 'set<int>'`
Likewise, this becomes:
set<int> Bessie_Times[FF]; // wrong
set<int> *Bessie_Times = new set<int>[FF]; // right
Note that you must remember to deallocate all dynamic memory at the end of your program:
delete Paths[][];
delete BessieTimes[];
I've been trying to solve an easy problem, but I can't figure why my program doesn't work. I want to concatenate a string.
Can you help me? If so, can you also explain me why it doesn't work?
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
ifstream in("sirul.in");
ofstream out("sirul.out");
char a[4000]="a",b[4000]="b",aux[4000];
int main()
{ int n,i;
in>>n;
if(n==1)out<<"a";
if(n==2)out<<"b";
for(i=3;i<=n;i++)
{
aux=strcpy(aux,b);
b=strcat(b,a);
a=strcpy(a,aux);
}
return 0;
}
strcpy and strcat work directly on the pointer you pass in as the first argument, then also return is so that you can chain calls. As such, assigning their result back to the destination pointer is redundant. In this case, it's also invalid, as you can't reassign an array.
The fix is to just not assign the return value of those calls:
strcpy(aux,b);
strcat(b,a);
strcpy(a,aux);
However, since you are using C++, you should use std::string instead, which gives you nice value semantics for your string data.
you can not do (see 2)
char b[4000]="b";
char aux[4000];
aux /* 2 */ = strcpy(aux /* 1 */ , b);
because aux is not a pointer, but array. you can pass it as pointer argument (see 1), but you can not "collect" the result "inside" aux (see 2).
As other suggested, just remove "collection" and it will work as you expect.
char b[4000]="b";
char aux[4000];
strcpy(aux /* 1 */ , b);
// or even:
const char *s = strcpy(aux /* 1 */ , b);
Also you are mixing C and C++ in one file.
Also probably there is possibility for buffer overflow.
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
ifstream in("sirul.in");
ofstream out("sirul.out");
char a[4000]="a",b[4000]="b",aux[4000];
int main()
{
int n,i;
cin>>n;
if(n==1)cout<<"a";
if(n==2)cout<<"b";
for(i=3;i<=n;i++)
{
strcpy(aux,b);
strcat(b,a);
strcpy(a,aux);
}
return 0;
}
check out definition os strcpy, in should be cin and out should be cout
I am trying to create a C++ program which stores many names linked to a numeric value, and can alter the numeric value when the name is entered. My question is: if I create an array in the main function, can it be accessed from another function? And if so, what should be done for that?
Attaching code (part of it)
#include <iostream>
#include <fstream> //required as input\output is from\to file
#include <string>
using namespace std;
int name_checker (string input);
int main()
{
int cases;
cin >> cases;
string names[cases]; //this is the array.
int i=0;
while (i<cases)
{
cin >> names[i];
i++;
}
}
int name_checker (string input);
{
//i want the data stored in above array to be availible here. possible?
}
Yes, possible. Pass the array as an argument to the function.
Change the function as -
int name_checker (string input[]);
And pass the array to the function -
name_checker(names);
Note: Changing the values in the function will also affect the original values.
You should consider using a class.
So you can make the array a field (as πάντα ῥεῖ suggested you should consider using a vector ) and name_check a member function.
Please guide me on this code, I want to store list of 5 data using array and function, this is a piece of code of mine, but this is giving me an error ("33"):
Cannot convert `ABC (*)[5]' to `ABC*' for argument `1' to `void pass(ABC*)'
Code:
#include <iostream>
using namespace std;
struct ABC{
char name[20];
int phone;
char address[20];
};
void pass(ABC *abc){
for(int i=0; i<5;i++){
cout<<"Enter name"<<endl;
cin>>abc[i].name;
cout<<"Enter phone"<<endl;
cin>>abc[i].phone;
cout<<"Enter address"<<endl;
cin>>abc[i].address;
}
}
int main()
{
ABC abc[5];
pass(&abc);
system("PAUSE");
return EXIT_SUCCESS;
}
You can use pass(&abc[0]); or pass(abc); to get a pointer to the first element in the array. Otherwise if you use &abc alone you get a pointer to a whole array[5] not the elements inside the array.
Arrays are not pointers. But they can decay to pointers when you are doing function calls.
So you can pass your array like this:
pass(abc);
trying to get ‘sval’ to contain the string “$1” – “$500” for array indexes 0-499. in the following code, however itoa is giving me strange strings in the code below:
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef struct data_t {
int ival;
char *sval;
} data_t;
void f1(data_t **d);
int main()
{
data_t *d;
d=static_cast<data_t*>(malloc(500)); //is this even needed?
d = new data_t[500];
f1(&d);
}
/* code for function f1 to fill in array begins */
void f1(data_t **d)
{
int i;
char str[5];
for (int i=0; i<500; i++)
{
(*d)[i].ival=i+1;
itoa (i,str,10);
(*d)[i].sval= str;
}
}
it also seems itoa has been depreciated, but that was what i got when i googled int to string
You don't need ltoa, cout should be just fine. Why do you need to keep the number and its string representation in the array? when you do cout << 10 you get "10" on the output, you don't need any conversions of your own
You, on the other hand, do ltoa without allocating any memory for the strings, which is not healthy as you have probably noticed. You use a local variable (the same, for all the 500 array members), which you try to access after you exit the function - a big no-no, its undefined behavior.
And:
d=static_cast<data_t*>(malloc(500)); //is this even needed?
d = new data_t[500];
No. Not only not needed - shouldn't be there at all! When in C++ - use new and delete, never malloc, that's a C function.