Home > Back-end >  how to allow string to have specific letters in C
how to allow string to have specific letters in C

Time:10-18

So basically I made a program that takes some input from the user and stores it in the variable. But the problem is that i want user to input Nucleotide DNA sequence and since the DNA sequence only consists of the Letters ATGC, i want to limit the input and only include these characters and if the user inputs some other character it shows an error

#include <iostream>
#include <string>

using namespace std;

int main ()
{
    string DNAsequence;
    cout<<"Enter the DNA sequence : ";
    cin>> DNAsequence;
    cout<< "Your 5 to 3 DNA sequence is "<<DNAsequence;
    
    
}

CodePudding user response:

As mentioned in a comment by Bathsheba, this sounds like something to be handled in a custom >> overload.

First lets define a type that holds a string and is parametrized on the allowed letters:

template <char ...allowed_chars>
struct DNASequence {
    std::string value;
};

The output operator is simple:

template <char ...allowed_chars>
std::ostream& operator<<(std::ostream& out,const DNASequence<allowed_chars...>& dna){
    out << dna.value;
    return out;
}

A function that checks if a given letter is allowed:

template <char ...allowed_chars>
bool check(char c){
    auto eq = [](char a,char b){ return a==b;};
    return (eq(c,allowed_chars) || ...);
}

Now the input operator can just read a string like usually and then check each character of that string:

template <char ...allowed_chars>
std::istream& operator>>(std::istream& in, DNASequence<allowed_chars...>& dna){
    std::string temp;
    in >> temp;
    for (const auto& c : temp) {
        if (! check<allowed_chars...>(c)){
            in.setstate(std::ios::failbit);
            return in;
        }
    }
    dna.value = temp;
    return in;
}

Main can look like this:

int main ()
{
    DNASequence<'A','C','G','T'> dna;
    std::cout << "Enter the DNA sequence : ";
    if (std::cin >> dna) {
        std::cout << "Your 5 to 3 DNA sequence is " << dna;
    } else {
        std::cout << "invalid input";
    }
}

I suppose you want to reset std::cins error state and use a loop to ask the user again in case they entered invalid input.

Complete example: https://godbolt.org/z/r4947G5sP

CodePudding user response:

you can use cin.get() function in cycle and check input characters

string GetStringFromConsole(){
        string Returner; 
        while (true){
            Returner  = cin.get();

            if (Returner[Returner.size() - 1] != 'A' && 
            Returner[Returner.size() - 1] != 'T' &&
            Returner[Returner.size() - 1] != 'G' &&
            Returner[Returner.size() - 1] != 'C'){
                 cout << "Error";
                 Returner.resize(Returner.size() - 1);
                 continue;
            }
            if (Returner[Returner.size() - 1] == '\n'){
                Returner.resize(Returner.size() - 1);
                break;
            }
        }
        return Returner;
}
  • Related