May 22, 2013 at 2:14pm UTC
Heloo,
I create a function for entry with ids. The code is below.Now I need create a function for enter with the markers by id.
Ex: id markers
12SA AABBCD
1345 BBABCC
How can I do that?
void ide(int nanim)
{
cout << "Enter the number of animals: ";
cin >> nanim;
string *id = new string[nanim];
for ( size_t i=0; i < nanim; i++ )
{
cout<< "\nEnter id anim "<< i+1 << ": ";
cin >> id[i];
}
for ( size_t i = 0; i < nanim; i++ )
{
cout << id[i] << endl;
}
}
Last edited on May 22, 2013 at 2:14pm UTC
May 22, 2013 at 2:51pm UTC
You can use an array of type std::pair<std::string, std::string>. For example
std::pair<std::string, std::string> *id = new std::pair<std::string, std::string>[nanim];
for ( size_t i=0; i < nanim; i++ )
{
cout<< "\nEnter id anim "<< i+1 << ": ";
cin >> id[i].first;
cout<< "\nEnter marker anim "<< i+1 << ": ";
cin >> id[i].second;
}
for ( size_t i = 0; i < nanim; i++ )
{
cout << id[i].first << '\t' << id[i].second << endl;
}
May 22, 2013 at 3:20pm UTC
I tried this code, it was compiled without errors but nothing happen when I run;
int main()
{ int nanim;
pair < string,string> *id= new pair<string,string>[nanim];
for ( size_t i=0; i < nanim; i++ )
{
cout<< "\nEnter id anim "<< i+1 << ": ";
cin >> id[i].first;
cout<< "\nEnter marker anim "<< i+1 << ": ";
cin >> id[i].second;
}
for ( size_t i = 0; i < nanim; i++ )
{
cout << id[i].first << '\t' << id[i].second << endl;
}
return 0;
}
Thank you!
May 22, 2013 at 3:23pm UTC
First of all you did not initialize nanim. So the size of the array is uknown.
{ int nanim;
pair < string,string> *id= new pair<string,string>[nanim];
And secondly size of an array shall be a constant expression.
May 22, 2013 at 4:57pm UTC
I do a code below...
But I need um array with ids and other array with genotype.
How can I return these ararys?
void * ide(int nanim, int nsnp)
{
cout << "Enter the number of animals: ";
cin >> nanim;
cout << "Enter the number of snps: ";
cin >> nsnp;
string *id = new string[nanim];
string *genotype = new string[nanim];
string snp= "";
for ( size_t i=0; i < nanim; i++ )
{
cout<< "\nEnter id anim "<< i+1 << ": ";
cin >> id[i];
for (size_t j=0; j<nsnp;j++)
{
cout<< "SNP "<< j+1<< " Anim "<< i+1 << ": ";
cin >> snp;
genotype[i]=genotype[i]+snp;
}
}
for ( size_t i = 0; i < nanim; i++ )
{
cout << "\n"<<id[i] << "\t "<< genotype[i]<< endl;
}
}
Last edited on May 22, 2013 at 6:54pm UTC
May 22, 2013 at 7:17pm UTC
How can I do using make_pair?
May 22, 2013 at 7:31pm UTC
If you are asking about std::map then I have not understood the question. I showed you an example of using std::map.
As for std::make_pair then it is simple
std::make_pair( "1", "AA" );
or
std::make_pair( std:;string( "1" ), std:;string( "AA" ) );
May 22, 2013 at 7:58pm UTC
I tried the code below for return a array with id; but there was a error in int main
[Error] variable-sized object 'anim' may not be initialized...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
[#include <iostream>
#include <string>
using namespace std;
[code]string* getArray(int nanim)
{
cout << "Enter the number of animals: " ;
cin >> nanim;
string *id = new string[nanim];
for ( size_t i=0; i < nanim; i++ )
{
cout<< "\nEnter id anim " << i+1 << ": " ;
cin >> id[i];
}
for ( size_t i = 0; i < nanim; i++ )
{
cout << id[i] << endl;
}
return (new string[nanim]);
}
int main()
{
int n;
string anim[n]=getArray(n)]
Last edited on May 22, 2013 at 9:01pm UTC
May 22, 2013 at 8:28pm UTC
As I said early the size of the array shall be a constant expression and you may not initialize an array with a pointer.
Last edited on May 22, 2013 at 8:28pm UTC
May 22, 2013 at 8:51pm UTC
How can I do that using this function?
thank you! Sorry for basic questions...
Last edited on May 22, 2013 at 8:51pm UTC
May 22, 2013 at 8:53pm UTC
He means something like this
1 2
const int SIZE = 1e9;
int array[ SIZE ];
Edit - Can you please use code tags? They are on the right side of the reply/edit box they look like
You can also do something like
which looks like this
//stuff
Last edited on May 22, 2013 at 8:55pm UTC
May 22, 2013 at 9:05pm UTC
I tried the code, but not worked;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include <iostream>
#include <string>
using namespace std;
string* getArray()
{ int nanim;
cout << "Enter the number of animals: " ;
cin >> nanim;
string *id = new string[nanim];
for ( size_t i=0; i < nanim; i++ )
{
cout<< "\nEnter id anim " << i+1 << ": " ;
cin >> id[i];
}
for ( size_t i = 0; i < nanim; i++ )
{
cout << id[i] << endl;
}
return id;
}
int main()
{
int n;
cin>>n;
string* anim[n]=getArray();
cout<<anim;
return 0;
}
Last edited on May 22, 2013 at 9:06pm UTC
May 23, 2013 at 2:09am UTC
25 26 27 28
int n;
cin >> n;
string* anim[n] = getArray();
cout << anim;
1) n is not a constant value, so you cannot declare *anim[n].
2) getArray returns a pointer to an array but *anim[n] is an array of pointers. Just use *anim.
3) You are displaying the memory address anim first points to, or &anim[0]. If you want to display the strings pointed to by anim, just use a for loop.
4) What is the point of variable n when the user has to reenter the number of animals in getArray()? I think you could do with eliminating everything in main except for a call to getArray and return 0. Plus, if you do this, change the return type of getArray to void and get rid of the return statement.
5) You never deallocate memory allocated by your getArray function.
Last edited on May 23, 2013 at 2:10am UTC