Sign in to follow this  

*problems/questions.

This topic is 4838 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

/*convets user inputted DNA to in the end, convert to final tRNA.*/
#include <iostream>
using namespace std;
#include <conio.h>

char *entire_dna[400]; //limit entry to 400
char *entire_mrna[400];
char *entire_trna[400];
int dna_array_bounds=0;
int max_dab;

//recieve char
//return a char
char dna_input()
{
	char singledna;
	cin >> singledna;
	return(singledna);
}

char copy(char *array1, char *array2) //array1 is the copy source, 2 is copy to be made
{
	for(int x=0;x<max_dab;++x)
	{
		array2[x]=array1[x];
	}
	return(*array2);
}
//function to check that code is working
void checkdna()
{
	for(int i=0;i<max_dab;++i)
		cout << entire_dna[i] << " ";
}

int main()
{
	cout << "Enter values of a,g,t,c or d anything else\n\n";
	//recieve DNA values from user
	while(1)
	{
		//firststep: writes the DNA;
		char userinput=dna_input();
		if(userinput!='d' && dna_array_bounds<400)
		{
			//invalid entry check
			if(userinput=='a' || userinput=='g' || userinput=='t' || userinput=='c')
			{
				entire_dna[dna_array_bounds]=&userinput;
				//increment to read everything.
				++dna_array_bounds;
			}
		}
		else
			break;

	}
	max_dab=dna_array_bounds; //sets how many dna "stuff" are set
	copy(&entire_dna, &entire_mrna);
	//We recieved the DNA input and now we need to
	//chnage it to mRNA
	//Conversioin: A-U,T-A,C-G
	for(int j=0;j<max_dab;++j)
	{
		//need to dereference the pointer.
		char mrna=*entire_dna[j];
		switch(mrna)
		{
		case 'a': *entire_dna[j]='u';
			break;
		case 't': *entire_dna[j]='a';
			break;
		case 'c': *entire_dna[j]='g';
			break;
		case 'g': *entire_dna[j]='c';
			break;
		}
	}

	checkdna();
	getch();
	return(0);
}




I need a fix for this code urgently for results FROM the program. I'm planning on reading the c++ book tomorrow and refreshing references and pointers but anyonewith a fix will be greatly appreciated!! This s the error I get: --------------------Configuration: dna - Win32 Debug-------------------- Compiling... main.cpp C:\Program Files\Microsoft Visual Studio\MyProjects\dna\main.cpp(59) : error C2664: 'copy' : cannot convert parameter 1 from 'char *(*)[400]' to 'char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Error executing cl.exe. dna.exe - 1 error(s), 0 warning(s) And moer than likely, I used * or & incorrectly. [Edited by - Charles Hwang on September 13, 2004 10:40:52 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Charles Hwang
I need a fix for this code urgently for results FROM the program. I'm planning on reading the c++ book tomorrow and refreshing references and pointers but anyonewith a fix will be greatly appreciated!!


if you need a fix you also need to tell us how it's broken...in detail.

but in the meantime these are all wrong:

char *entire_dna[400]; //limit entry to 400
char *entire_mrna[400];
char *entire_trna[400];

they should be:

char entire_dna[400]; //limit entry to 400
char entire_mrna[400];
char entire_trna[400];

-me

Share this post


Link to post
Share on other sites
Quote:
Original post by Charles Hwang
ok..then how do i make a pointer out of it? I'm going to be sending stuff from funciotn to main and stuff. i can't do that without a pointer.


it's already a pointer. an array variable is a pointer to a sequantial series of other variables in memory.

-me

Share this post


Link to post
Share on other sites

/*convets user inputted DNA to in the end, convert to final tRNA.*/
#include <iostream>
using namespace std;
#include <conio.h>

char aentire_dna[400]; //limit entry to 400
char aentire_mrna[400];
char aentire_trna[400];
char *entire_dna=&aentire_dna;
char *entire_mrna=&aentire_mrna;
char *entire_trna=&aentire_trna;
int dna_array_bounds=0;
int max_dab;

//recieve char
//return a char
char dna_input()
{
char singledna;
cin >> singledna;
return(singledna);
}

char copy(char *array1, char *array2) //array1 is the copy source, 2 is copy to be made
{
for(int x=0;x<max_dab;++x)
{
array2[x]=array1[x];
}
return(*array2);
}
//function to check that code is working
void checkdna()
{
for(int i=0;i<max_dab;++i)
cout << entire_dna[i] << " ";
}

int main()
{
cout << "Enter values of a,g,t,c or d anything else\n\n";
//recieve DNA values from user
while(1)
{
//firststep: writes the DNA;
char userinput=dna_input();
if(userinput!='d' && dna_array_bounds<400)
{
//invalid entry check
if(userinput=='a' || userinput=='g' || userinput=='t' || userinput=='c')
{
entire_dna[dna_array_bounds]=&userinput;
//increment to read everything.
++dna_array_bounds;
}
}
else
break;

}
max_dab=dna_array_bounds; //sets how many dna "stuff" are set
copy(&entire_dna, &entire_mrna);
//We recieved the DNA input and now we need to
//chnage it to mRNA
//Conversioin: A-U,T-A,C-G
for(int j=0;j<max_dab;++j)
{
//need to dereference the pointer.
char mrna=*entire_dna[j];
switch(mrna)
{
case 'a': *entire_dna[j]='u';
break;
case 't': *entire_dna[j]='a';
break;
case 'c': *entire_dna[j]='g';
break;
case 'g': *entire_dna[j]='c';
break;
}
}

checkdna();
getch();
return(0);
}



/me cries...more errors

Share this post


Link to post
Share on other sites
couple of changes to get it to compile / work:

- remove the * from the first 3 'char' defines as 'Palidine' mentions. you want them to be character array's, so the [400] does that.
- in the 'copy' function, if you're not using the return just change it to 'void'

- the line:
entire_dna[dna_array_bounds]=&userinput;
should be
entire_dna[dna_array_bounds]=userinput;

- the line:
copy(&entire_dna, &entire_mrna);
should be
copy(entire_dna, entire_mrna);

- the line:
char mrna=*entire_dna[j];
should be
char mrna=entire_dna[j];

- change the 4:
case 'a': *entire_dna[j]='u';
lines to
case 'a': entire_dna[j]='u';
(remove the *)

after these changes I get the following output:
aaagggd
u u u c c c

hope this helps.

Share this post


Link to post
Share on other sites
as a follow up, just read any good C++ book on pointers, specifically around string manipulation and passing character arrays around. you have the right intentions, just some assignment confusions.

Share this post


Link to post
Share on other sites
An array name is functionally equivalent to a pointer. There are differences - obtaining the length of an array name should give you the length of the array, not the size of the pointer, for instance, but generally speaking they are equivalent.

So to pass the entire array to a function simply use entire_dna. In your function you can then use it to access the contents of the array.

void somefunc(char arr[])
{
...
}

void main()
{
somefunc(entire_dna);
}

Share this post


Link to post
Share on other sites
Well Now i've got this, only one error left:


/*convets user inputted DNA to in the end, convert to final tRNA.*/
#include <iostream>
using namespace std;
#include <conio.h>

char entire_dna[400]; //limit entry to 400
char entire_mrna[400];
char entire_trna[400];
int dna_array_bounds=0;
int max_dab;

//recieve char
//return a char
char dna_input()
{
char singledna;
cin >> singledna;
return(singledna);
}

char copy(const char *&array1, char *&array2) //array1 is the copy source, 2 is copy to be made
{
for(int x=0;x<max_dab;++x)
{
array2[x]=array1[x];
}
return(*array2);
}
//function to check that code is working
void checkdna()
{
for(int i=0;i<max_dab;++i)
cout << entire_dna[i] << " ";
}

int main()
{
cout << "Enter values of a,g,t,c or d anything else\n\n";
//recieve DNA values from user
while(1)
{
//firststep: writes the DNA;
char userinput=dna_input();
if(userinput!='d' && dna_array_bounds<400)
{
//invalid entry check
if(userinput=='a' || userinput=='g' || userinput=='t' || userinput=='c')
{
entire_dna[dna_array_bounds]=userinput;
//increment to read everything.
++dna_array_bounds;
}
}
else
break;

}
max_dab=dna_array_bounds; //sets how many dna "stuff" are set
copy(entire_dna, entire_mrna);
//We recieved the DNA input and now we need to
//chnage it to mRNA
//Conversioin: A-U,T-A,C-G
for(int j=0;j<max_dab;++j)
{
//need to dereference the pointer.
char mrna=entire_dna[j];
switch(mrna)
{
case 'a': entire_dna[j]='u';
break;
case 't': entire_dna[j]='a';
break;
case 'c': entire_dna[j]='g';
break;
case 'g': entire_dna[j]='c';
break;
}
}

checkdna();
getch();
return(0);
}



And i get ONE error:

--------------------Configuration: dna - Win32 Debug--------------------
Compiling...
main.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\dna\main.cpp(59) : error C2664: 'copy' : cannot convert parameter 1 from 'char [400]' to 'const char *& '
A reference that is not to 'const' cannot be bound to a non-lvalue
Error executing cl.exe.

dna.exe - 1 error(s), 0 warning(s)

Any ideas?

Share this post


Link to post
Share on other sites
Sorry I didn't see this earlier charles you know my aim wnxdurfinator so if you need more help i'll help. I know a lot more c/c++ than i do web programming :)

Share this post


Link to post
Share on other sites

/*convets user inputted DNA to in the end, convert to final tRNA.*/
//include important headers.
#include <iostream>
using namespace std;
#include <conio.h>

char entire_dna[400]; //limit entry to 400
char entire_mrna[400];
char entire_trna[400];
int dna_array_bounds=0;
int max_dab;

//recieve char
//return a char
char dna_input()
{
char singledna;
cin >> singledna;
return(singledna);
}

void copy(const char *src, char *dest) //array1 is the copy source, 2 is copy to be made
{
for(int x=0;x<max_dab;++x)
{
dest[x]=src[x];
}
}
//function to check that code is working
void checkdna()
{
for(int i=0;i<max_dab;++i)
cout << entire_dna[i] << " ";
}
void checkmrna()
{
for(int i=0;i<max_dab;++i)
cout << entire_mrna[i] << " ";
}
void checktrna()
{
for(int i=0;i<max_dab;++i)
cout << entire_trna[i] << " ";
}

int main()
{
cout << "Enter values of a,g,t,c or d anything else\n\n";
//recieve DNA values from user
while(1)
{
//firststep: writes the DNA;
char userinput=dna_input();
if(userinput!='d' && dna_array_bounds<400)
{
//invalid entry check
if(userinput=='a' || userinput=='g' || userinput=='t' || userinput=='c')
{
entire_dna[dna_array_bounds]=userinput;
//increment to read everything.
++dna_array_bounds;
}
}
else
break;

}
max_dab=dna_array_bounds; //sets how many dna "stuff" are set
copy(entire_dna, entire_mrna);
//We recieved the DNA input and now we need to
//chnage it to mRNA
//Conversion: A-U,T-A,C-G
for(int j=0;j<max_dab;++j)
{
//need to dereference the pointer.
char mrna=entire_mrna[j];
switch(mrna)
{
case 'a': entire_mrna[j]='u';
break;
case 't': entire_mrna[j]='a';
break;
case 'c': entire_mrna[j]='g';
break;
case 'g': entire_mrna[j]='c';
break;
}
}
copy(entire_mrna, entire_trna);//repeat everything
//chnage it to tRNA
//Conversion: A-U,T-A,C-G and this time we have possible Us (instead of possible t's
//because of the helix model) in the beginning translation, so U-A needs to be checked too
for(int y=0;y<max_dab;++y)
{
//need to dereference the pointer.
char trna=entire_trna[y];
switch(trna)
{
case 'a': entire_trna[y]='u';
break;
case 'u': entire_trna[y]='a';
break;
case 'c': entire_trna[y]='g';
break;
case 'g': entire_trna[y]='c';
break;
}
}

cout << "DNA\n";
checkdna();
cout << "\n\nmRNA\n";
checkmrna();
cout << "\n\ntRNA\n";
checktrna();

getch();
return(0);
}



What I don't understand is, within the copy() prototype, how is the actual values of the array being manipulated. The parameters with * derenferencing is the only way to make the program work (correctly) without errors but I see no passing by reference which is required when sending values through a function...n'est ce pas?

Share this post


Link to post
Share on other sites
The "type calculus" here really is simpler than you're trying to make it. Notice how the solution mostly involved *removing* *'s and &'s.

Passing by (non-const) reference is needed to modify *the parameter*. You're not modifying the parameter; your parameter is a pointer, and you modify the *pointed-at data*. Which is perfectly kosher, since your arrays are arrays of non-constant char's.

When you have dest[x] = src[x] inside your copy() function, it's equivalent to *(dest + x) = *(src + x). This is the (rough) equivalence between pointers and arrays. Yes they are different, but the differences are usually just pedantic (unless you need to play tricks with sizeof).

But now that that's out of the way, I feel I must scold you for your evil, evil mixing of C++ with old-style C. This stuff really is quite a bit easier with std::string. It would handle the copying for you, without the need for an arbitrary limit on data length.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
The "type calculus" here really is simpler than you're trying to make it. Notice how the solution mostly involved *removing* *'s and &'s.

Passing by (non-const) reference is needed to modify *the parameter*. You're not modifying the parameter; your parameter is a pointer, and you modify the *pointed-at data*. Which is perfectly kosher, since your arrays are arrays of non-constant char's.

When you have dest[x] = src[x] inside your copy() function, it's equivalent to *(dest + x) = *(src + x). This is the (rough) equivalence between pointers and arrays. Yes they are different, but the differences are usually just pedantic (unless you need to play tricks with sizeof).

But now that that's out of the way, I feel I must scold you for your evil, evil mixing of C++ with old-style C. This stuff really is quite a bit easier with std::string. It would handle the copying for you, without the need for an arbitrary limit on data length.


Could you eleborate on the evils?

Share this post


Link to post
Share on other sites
Mostly bad style. As well, using old school C can easily bypass some of the safety mechanisms that C++ builds in to protect you from mistakes. C didn't consider those mistakes. And, believe it or not, learning C++ fully shows you that the really complicated stuff in C can be made a *lot easier*.

(References come to mind right off the top of my head.)

Share this post


Link to post
Share on other sites

This topic is 4838 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this