Problem with reading a single character.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
int size=0;
int get(char b[]);
int set(char b[], char c[]);
 int main()
{
cout<<"Enter size\n";

cin>>size;

 char bp[size];
    char cbp[size];

    get(bp);

   set(bp,cbp);

    return 0;
}
int get(char b[ ]){

for(int i=1;i<=size;i++){
    cout<<"Enter "<<i<<endl;
cin>>b[i];



b[i]=toupper(b[i]);
if(b[i] != 'A'||b[i] !='C'||b[i] !='G' ||b[i]!='T'){
cout<<"Error: No such Nitrogen Base Found. You need to enter this value again \n";
i=i-1;
}

}

return 0;
}
int set(char b[], char c[])
{
    for(int j=1;j<=size;j++){

     if(b[j]=='A')
      c[j]='T';
       if(b[j]=='T')
      c[j]='A';
     if(b[j]=='C')
      c[j]='G';
     if(b[j]=='G')
      c[j]='C';
      cout<<setw(10)<<"-"<<b[j]<<"-"<<c[j]<<"-"<<endl;
      if((j%3)==0&& j!=0)
      cout<<endl;
    }
    return 0;
}

I'm currently using Code::Blocks IDE with MingW compiler. I want to read only specific character i.e 'A, C, G, T and U' (U is reserved for later).
But my program reads every character so it is a problem. So I used IF statement but now it will not accept anything.
What could be the solution?
Last edited on
Problem is that you use logical or in your statement. It wil give you an error if letter is not equal to, say, A, or if it is ot equals C. Afo if you enter A, b[i]!='C' will fire and if you enter C, b[i]!=A will fire. Replace || with && to solve problem.
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using namespace std;

int size=0;
int get(char b[]);
int set(char b[], char c[]);

int main()
{
	cout << "Enter size\n";
	cin >> size;
	char * bp = new char[size];  // Ignore this change (my compiler doesn't let me create non-const size array - have to allocate
	char * cbp = new char[size];  // same thing here (yes i know there is no delete the memory will be given back at the end of the program)
	get(bp);
	set(bp,cbp);
	cin >> size;  // Ignore this also (so my command prompt doesnt close until i enter something)
	return 0;
}

int get(char b[])
{
	for(int i = 0; i < size; i++) // heres your first problem - array indexes start at zero not one.. and go until
            // one less than the size of array therefor you need "< size".. not "<= size"
	{
		cout << "Enter " << i + 1 << endl;
		cin >> b[i];
		b[i] = toupper(b[i]);
		if( b[i] != 'A' && b[i] !='C' && b[i] !='G' && b[i]!='T' ) // here is your second problem - the || means that as long as
            // only one of the conditions are met the code in the block will execute..
            // this means that if you enter A, the first condition evaluates to false but then the
            // the rest of the conditions are true.. therefor you have: if ( false OR true OR true OR true ) which evaluates to true..
            // therefor replace || with && as shown here and like the previous post said and you have if ( false AND true AND true AND true)
            // which evaluates to false - it will only evaluate to true when all conditions are true
		{
			cout << "Error: No such Nitrogen Base Found. You need to enter this value again \n";
			--i; // this is just prettier to look at - no real huge difference
		}
	}
	return 0;
}

int set(char b[], char c[])
{
    for(int j = 0; j < size; j++) // finally your last mistake which is the same mistake as in the other for loop
	{

		if(b[j]=='A')
			c[j]='T';
		if(b[j]=='T')
			c[j]='A';
		if(b[j]=='C')
			c[j]='G';
		if(b[j]=='G')
			c[j]='C';

		cout << setw(10) << "-" << b[j] << "-" << c[j] << "-" << endl;

		if( (j%3) == 0 && j != 0 )
			cout<<endl;
    }
    return 0;
}



I think this is what you are going for.. hopefully this helps
Thank you dreamincolor and MiiNiPa for you help.
Now I'm having another problem regarding Codon and their respective amino acid.
For example UCU,UCA codes for Serine( Codon=group of 3 nucleotide like UUA)
UAU codes for Tyrosine.
Now the problem is what should I use to make this output?
The person gives coding of base pairs like a u a c g etc through that previous array.
What should be used here? Should I use
const char codon []="nucleotide";
but it'll take a lot of time and make my code lengthy.
May I use a file?Fstream thing? But I have no idea how it would work with it.
Please help.
Topic archived. No new replies allowed.