Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Program For inputs like \"b/\", \"#/\", \"bA/\", \"#A/\', \"b5/\", \"#5/\" a

ID: 3601710 • Letter: C

Question

C++ Program

For inputs like "b/", "#/", "bA/", "#A/', "b5/", "#5/" and etc. , Why does my program give an assertion failuire or outputs "is song" when it's supposed to give "not song"?

#include
#include
#include
using namespace std;

bool hasCorrectSyntax(string song);

int main()
{
   for (;;)
   {
       //Declare all required variables
       string song;
       bool isSongText;

       //Read input from user
       cout << "Enter song : ";
       getline(cin, song);

       //Call hasCorrectSyntax
       isSongText = hasCorrectSyntax(song);
       if (isSongText)
       {
           cout << song << " is a song!" << endl;
       }
       else
       {
           cout << song << " is Not a song!" << endl;
       }
   }
}


//Method to check correct syntax in song
bool hasCorrectSyntax(string song) {

   //Declare all required variables
   int len;
   bool isSong;
   char lastCharOfSong;

   //Get length of the song
   len = song.length();

   //If len==0 return true
   if (len == 0)
   {
       return true;
   }

   lastCharOfSong = song[len - 1];
   isSong = false;

   //First check if last char of song is '/'
   if (lastCharOfSong == '/')
   {

       for (int i = 0; i        {
           if (isalpha(song[i]) && song[i] != 'b')
           {
               //Check for aplhabet and letter is A,B,C,D,E,F,G
               if (song[i] == 'A' || song[i] == 'B' || song[i] == 'C'
                   || song[i] == 'D' || song[i] == 'E'
                   || song[i] == 'F' || song[i] == 'G')
               {

                   isSong = true;
               }
               else
               {
                   //If not make the flag false
                   isSong = false;
                   //break the loop
                   break;
               }

           }
           //Check for digit   
           else if (isdigit(song[i]) && (song[i] >= '0' && song[i] <= '9') && !isdigit(song[i + 1])&& !isdigit(song[0]) && song[i-1]!='/' && song[i+1]!='b' && song[i+1]!='#')
           {
               isSong = true;
           }
           //Check for accidental char # and b
           else if ((song[i] == '#' && song[i + 1] != '#' && song[i + 1] != 'b' && song[0]!='b' && song[i-1] != '/') || (song[i] == 'b' && song[i + 1] != 'b' && song[i + 1] != '#' && song[0] != '#' && song[i - 1] != '/'))
           {
               isSong = true;
           }
           //Check for slash
           else if (song[i] == '/')
           {
               isSong = true;
           }
           else
           {
               //If not make the flag false
               isSong = false;
               //break the loop
               break;
           }

       }
   }
   else
   {
       //If not make the flag false
       isSong = false;
   }

   return isSong;
}

Explanation / Answer

Solution:

Hi, As I have reviewed your coding part, everything seems perfect except the check for accidental characters b and # part. Please make sure what kind of checking you want to do with those characters. Because it is not clearly coded.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote