I\'m getting errors and warning from this code: Severity Code Description Projec
ID: 3787772 • Letter: I
Question
I'm getting errors and warning from this code:
Severity Code Description Project File Line Suppression State
Warning C4018 '<': signed/unsigned mismatch 40
Severity Code Description Project File Line Suppression State
Warning C4101 'len': unreferenced local variable 33
Severity Code Description Project File Line Suppression State
Error C4996 'strcat': This function or variable may be unsafe. Consider using strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 49
Severity Code Description Project File Line Suppression State
Error C4996 'strcat': This function or variable may be unsafe. Consider using strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 58
Enter a group of words or ENTER to quit: Computer Programming is fun to learn!
Original words: Computer Programming is fun to learn!
New Words: omputercay ogrammingpray isway unfay otay earnlay!
Enter a group of words or ENTER to quit: Quit
Pig Latin Hint:
If a word begins with one or more consonants, move the consonant or consonant cluster to the end of the word. Add the letters "ay" to the end of the word. So, "pig" would be "igpay," and “latin” would be “atinlay.”
Here is my code
#include <iostream>
#include <string>
using namespace std;
void heading();
string piglatinstring(string);
string substr(string, int&);
int main()
{
string input;
heading();
cout << "Enter a group of words or ENTER to quit: ";
getline(cin, input);
while (input.length()>0)
{
cout << "Original words: " << input << endl;
cout << "New words: " << piglatinstring(input) << endl << endl;
cout << "Enter a group of words or ENTER to quit: ";
getline(cin, input);
}
return 0;
}
void heading()
{
cout << "*** You will be prompted to enter a string of *** ";
cout << "*** words. The string will be converted into *** ";
cout << "*** Pig Latin and the results displayed. *** ";
cout << "*** Enter as many strings as you would like. *** ";
}
string piglatinstring(string input)
{
int len, counter = 0, start = 0, stop = 0;
string word = "", newstring = "";
do
{
word = substr(input, start);
start++;
newstring = newstring + word;
} while (start < input.length());
return newstring;
}
string substr(string s, int& n)
{
char word[50] = "", suffix[50] = "";
int i = 0;
suffix[0] = s[n];
suffix[1] = '';
strcat(suffix, "ay");
n++;
while (s[n] != ' '&&s[n] != '')
{
word[i] = s[n];
n++;
i++;
}
strcat(word, suffix);
strcat(word, " ");
return word;
}
Explanation / Answer
Hi,
I have fixed the code. it is wirking fine now and highlighted the code changes below.
#include <iostream>
#include <cstring>
using namespace std;
void heading();
string piglatinstring(string);
string substr(string, int&);
int main()
{
string input;
heading();
cout << "Enter a group of words or ENTER to quit: ";
getline(cin, input);
while (input.length()>0)
{
cout << "Original words: " << input << endl;
cout << "New words: " << piglatinstring(input) << endl << endl;
cout << "Enter a group of words or ENTER to quit: ";
getline(cin, input);
}
return 0;
}
void heading()
{
cout << "*** You will be prompted to enter a string of *** ";
cout << "*** words. The string will be converted into *** ";
cout << "*** Pig Latin and the results displayed. *** ";
cout << "*** Enter as many strings as you would like. *** ";
}
string piglatinstring(string input)
{
int len, counter = 0, start = 0, stop = 0;
string word = "", newstring = "";
do
{
word = substr(input, start);
start++;
newstring = newstring + word;
} while (start < input.length());
return newstring;
}
string substr(string s, int& n)
{
char word[50] = "", suffix[50] = "";
int i = 0;
suffix[0] = s[n];
suffix[1] = '';
strcat(suffix, "ay");
n++;
while (s[n] != ' '&&s[n] != '')
{
word[i] = s[n];
n++;
i++;
}
strcat(word, suffix);
strcat(word, " ");
return word;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
*** You will be prompted to enter a string of ***
*** words. The string will be converted into ***
*** Pig Latin and the results displayed. ***
*** Enter as many strings as you would like. ***
Enter a group of words or ENTER to quit: pig
Original words: pig
New words: igpay
Enter a group of words or ENTER to quit: latin
Original words: latin
New words: atinlay
Enter a group of words or ENTER to quit:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.