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

The following code was written for the prompt below: Please write a program that

ID: 3906390 • Letter: T

Question

The following code was written for the prompt below:

Please write a program that first assigns numbers 1-26 to the alphabet (i.e. A=1, B=2, C=3, etc.) then takes a numeric input from the user (i.e. 8-1-16-16-25 2-9-18-20-8-4-1-25 yields happy birthday) and translates it to the corresponding words. *Provide clear comments on each line of the code to explain what is happening.

The code builds successfully and then it yields the following message when it is run:

#include "stdafx.h"

#include <iostream>

#include <string>

using namespace std;

int main()

{

cout << "This program translates numbers to equivalent letters. Enter in a series of numbers in the space below. ";

string s;

getline(cin, s); //to take input from string when there is a space in between as string doesnt take input if space comes in between

for (int i = 0; i < s.size(); i++)

{

if (s[i] >= '0' && s[i] <= '9' && (s[i - 1] == '1' || s[i - 1] == '2')) //when its a double digit and we dont look at the left side which is dash, it is to take care of 14 and 15 but not 3 in example 3-14-15

{

cout << char(((s[i - 1] - 48) * 10) + s[i] + 48);

}

else if (s[i] >= '1' && s[i] <= '9' && (s[i + 1] == '-' || i + 1 == s.size())) // when we take single digit into account that is something like 3-14-15, this if is to take care of 3 and not 14 or 15

{

cout << char(s[i] + 48);

}

else if (s[i] == ' ') //takes care of the space in between

{

cout << ' ';

}

}

return 0;

}

  

This program translates numbers to equivalent letters. Enter in a series of numbers in the s Microsoft Visual CRuntime Library Debug Assertion Failed! Program: C:Windows SYSTEM32MSVCP140D.dll File: ciprogram files (86) microsoft visual studio 2017community vctools msvc 14.13.26128includest ing Line: 3148 Expression: string subscript out of range For information on how your program can cause an assertion failure, see the Visual Cdocumentation on asserts. (Press Retry to debug the application) Retry Ignore

Explanation / Answer

/*The following code can be used*/

#include<iostream>
#include<string>
#include<string.h>
#include<stdlib.h>

using namespace std;

int main(){

   string str = "abcdefghijklmnopqrstuvwxyz";
   cout << "Enter series of numbers with - as separator: ";
   string line;
   getline(cin,line);
   char *ch = strtok((char *)line.c_str(),"-");
   string ans = "";
   while (ch != NULL){
       int n = atoi(ch);
       ans = ans + str[n-1];
       ch = strtok(NULL,"-");
   }
   cout << ans << endl;
   return 0;
}

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