Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8932385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:18:45+00:00 2026-06-15T09:18:45+00:00

This is the first C++ program I have ever written and I’m having trouble

  • 0

This is the first C++ program I have ever written and I’m having trouble understanding the order in which operands must be put in. This is for a class, but it looks like I’m not supposed to use the homework tag. Sorry if I’m doing this wrong.

This is my input

// Get DNA string

string st;
cout << "Enter the DNA sequence to be analysed: ";
cin >> st;

This seems to work ok, but I thought I would include it incase this is what I’m doing wrong.

This is what I have so far to check that the input is exclusively C,T,A, or G.
It runs through the program and simply prints “Please enter a valid sequnce1, please enter a valid sequence2, … ect. I’m sure I’m doing something very stupid, I just can’t figure it out.

 // Check that the sequence is all C, T, A, G

while (i <= st.size()){
if (st[i] != 'c' && st[i] != 'C' && st[i] != 'g' && st[i] != 'G' && st[i] != 't' && st[i] != 'T' && st[i] != 'a' && st[i] != 'A');
    cout << "Please enter a valid sequence" <<
    i++;
else if (st[i] == c,C,G,t,T,a,A)
    i++;

The second half of my program is to count the number of Cs and Gs in the sequence

for (i < st.size() ; i++ ;);
for (loop <= st.size() ; loop++;)
    if (st[loop] == 'c')
    {
    count_c++;
    }
    else if (st[loop] == C)
    {
    count_c++;
    }
    else if (st[loop] == g)
    {
    count_g++;
    }
    else if (st[loop] == G);
    {
    count_g++;
    }


cout << "Number of instances of C = " << count_c;
cout << "Number of instances of G = " << count_g;

It seems like it’s not looping, it will count 1 of one of the letters. How do I make it loop? I can’t seem to put in endl; anywhere without getting an error back, although I know I’ll need it somewhere.

Any help or tips to point me in the right direction would be greatly appreciated –
I’ve been working on this code for two days (this is embarrassing to admit).


Edit :

My sequence checker looks like this now:

while (i < st.size() ) {
if (st[i] != c && st[i] != C && st[i] != g && st[i] !=G && st[i] !=t && st[i] !=T && st[i] !=a && st[i] != A)
    cout << "Please enter a valid sequence" << "\n" << "\n";
    i++;
}

and my counter looks like this:

// Count the number of Cs and Gs

count_c = 0;
count_g = 0;

for (i = 0; i < st.size() ; i++) {
   if ((st[i] == 'c') || (st[i] == 'C'))
   count_c++;
   else if ((st[i] == 'g')|| (st[i] == 'G'));
   count_g++;
}


cout << "Number of instances of C = " << count_c;
cout << "Number of instances of G = " << count_g;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:18:46+00:00Added an answer on June 15, 2026 at 9:18 am

    You should remove “;” after that if operator:

     if (st[i] != 'c' && st[i] != 'C' && st[i] != 'g' && st[i] != 'G' && st[i] != 't' && st[i] != 'T' && st[i] != 'a' && st[i] != 'A');
    

    Now it’s not doing anything.

    You should use “<” instead of “<=” to avoid wrong indexing of string array (string of size “size” means that indexes are from 0 to size – 1)

    while (i <= st.size())
    

    If you already checked that symbol is not one of c,C,g,G,t,T,a,A, you don’t need to check it again, so if (st[i] == c,C,G,t,T,a,A) is useless.

    But even with theese corrections your code is logically wrong.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have received this message ever since I started building my first iPhone program
This is my first ever attempt at a Objective-c cocoa program, so I have
I have a C/ncurses program that I'm debugging/maintaining. This program does ripoffline twice: first,
This is literally the first thing I've ever written in C, so please feel
I have a C code (first C code I have ever written), and there
I have this code. I am trying to retrieve just the text first program.
This is my first program using Haskell. I'm writing it to put into practice
I am having a little trouble with my first ever ant build in eclipse,
I am complete newbie for programming and this is my first real program I
Code first: '''this is main structure of my program''' from twisted.web import http from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.