Help with Xcode error c++, comparison between pointer/integer

Hey guys, I know this code might be a mess and it's definitely not done, but i'm not asking for help with the entire thing. I just need to figure out why I keep getting this stupid error...

Comparison between pointer and integer ('char*' and int') on lines 26 and 43.


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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include<iostream>
#include<cctype>
#include<cmath>

using namespace std;

char transcribe(char);

char ans;

int main() {
    char nt1[10];
    char nt2[10];
    bool keep_going = true;
    
    cout << "This program is designed to calculate a sequence identity between two different DNA or RNA sequences." << endl;
    do {
        cout << "Please start by entering the first DNA or RNA sequence. Note: sequence must be exactly 10 characters." << endl;
        cin >> nt1;
        
        keep_going = false;
        
        for (int i = 0; i < 10; ++i) {
            nt1[i] = transcribe(nt1[i]);
            
            if (nt1 != 'a' && nt1 !- 'A' && nt1 != 'g' && nt1 != 'G' && nt1 != 'c' && nt1 != 'C' && nt1 != 'u' && nt1 != 'U'){
                cout << "invalid character was encountered, try again" << endl;
                keep_going = true;
                
                break;
            }
        }
        
        if (keep_going)
            continue;
        
        for (int i = 0; i < 10; ++i) {
            nt2[i] = transcribe(nt2[i]);
            
            cout << "Please continue by entering the second DNA or RNA sequence. Note: sequence must be exactly 10 characters." << endl;
            cin >> nt2;
            
            if (nt2 != 'a' && nt2 !- 'A' && nt2 != 'g' && nt2 != 'G' && nt2 != 'c' && nt2 != 'C' && nt2 != 'u' && nt2 != 'U') {
                cout << "invalid character was encountered, start over" << endl;
                keep_going = true;
                
                break;
            }
        }
        
        if (keep_going)
            continue;
        
        int counter = 0;
        
        for (int i = 0; i < 10; ++i) {
            
            cout << nt1[i];
            
            if (nt1[i] == nt2[i]) {
                ++counter;
            }
        }
        
        cout << counter;
        cout << " over 10" << endl;
        
        // nt1 and nt2
        cout << "Did you want to run the program again?\n Please type 'y' to test another sequence or press any other key to quit.";
        cin >> ans;
        
    }
    while (ans != 'Y' || ans != 'y');
    
    return 0;
}

char transcribe(char nt){
    if (nt == 't' || nt == 'T')
        return 'U';
    else
        return toupper (nt);
}


Thank you so much for all the help guys!
Last edited on
Instead of single quotes use double quotes while comparing (or convert the nt2 in a single char)

Also use strings instead of char a[];
www.cplusplus.com/articles/jEywvCM9

PS: please use code tags
www.cplusplus.com/articles/jEywvCM9
Line 26,43: nt1 is an array of chars. You're trying to compare a character literal to an array. That's not supported. You want to compare a specific character from the array to the character literal.

Line 26,43: Since transcribe() does an upshift, there is no reason to check lower case charaters here.
 
  if (nt1[i] != 'A' && nt1[i] != 'G' && nt1[i] != 'C' && nt1[i] != 'U')


Line 26,43: You have a typo. nt1 !- 'A' should be nt1 != 'A'

Line 37-38: You're trying to call transcribe() on nt2, but nt2 has not been input yet.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Thank you, sorry about the code tag. It should be fixed now.
I still am a little confused, though. Where are you talking about I should use double quotes? I thought you had to use single quotes inside the if statements?
AbstractionAnon wrote:
Line 26,43: nt1 is an array of chars. You're trying to compare a character literal to an array. That's not supported. You want to compare a specific character from the array to the character literal.
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <cctype>
#include <cmath>
using namespace std;

void transcribe(char *);    //  Changed to operate on array

//  Return true if all characters in sequence are value
bool is_valid (const char * nt)
{   for (int i=0; i<10; i++)
    {   //  Changed to use index and eliminate lower case compare
        if (nt[i] != 'A' && nt[i] != 'G' && nt[i] != 'C' && nt[i] == 'U')
        {   cout << "invalid character was encountered, try again" << endl;
            return false;   
        }
    }
    return true;
}

int main() 
{   char nt1[10];
    char nt2[10];
    bool keep_going = true;
    char ans;               //  moved from globals
    
    cout << "This program is designed to calculate a sequence identity between two different DNA or RNA sequences." << endl;
    do 
    {   cout << "Please start by entering the first DNA or RNA sequence. Note: sequence must be exactly 10 characters." << endl;
        cin >> nt1;
        transcribe (nt1);
        keep_going = false;
    
        if (! is_valid (nt1)) 
        {   keep_going = true;
            break;
        }

        if (keep_going)
            continue;

        cout << "Please continue by entering the second DNA or RNA sequence. Note: sequence must be exactly 10 characters." << endl;
        cin >> nt2;
        transcribe(nt2);
        if (! is_valid (nt2))  
        {   keep_going = true;
            break;
        }
        if (keep_going)
            continue;

        int counter = 0;

        for (int i = 0; i < 10; ++i) 
        {   if (nt1[i] == nt2[i]) 
                ++counter;        
        }
        cout << endl;
        
        if (counter == 10)
            cout << "Sequences are the same" << endl;
        else
            cout << "Sequences are not the same" << endl;


        // nt1 and nt2
        cout << "Did you want to run the program again?\n Please type 'y' to test another sequence or press any other key to quit.";
        cin >> ans;
    }
    while (ans != 'Y' || ans != 'y');
    return 0;
}

//  Change to operate on sequence 
void transcribe (char * nt)
{   for (int i=0; i<10; i++)
        if (nt[i] == 't' || nt[i] == 'T')
            nt[i] = 'U';
        else
            nt[i] = toupper (nt[i]);
}

Topic archived. No new replies allowed.