Pig_latin program.
Aug 21, 2013 at 7:49am UTC
I stuck at main function. I don't know how to address user input to main(int argc, char *argv[]). user's input by ./pig_latin button .output: uttonbay. Any help would be appreciated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#include<iostream>
#include <iomanip>
#include<string>
using namespace std;
bool errorCheck(string);
void firstLetter(string);
void wordTranslate(string);
int main(int argc, char *argv[])
{
string wordInput="" ; //array
bool inputError = false ;
string wordInput = atoi(argv[1]);
inputError = errorCheck(wordInput);
if (inputError == false )
{
firstLetter(wordInput);
}
return 0;
}
bool errorCheck(string wordInput)
{
bool errorVerif = false ;
int Length = wordInput.length();
int Counter = 0;
char *specificLetter = &wordInput.at(0);
while (Counter < Length && errorVerif == false )
{
if (!isalpha(*specificLetter))
{
cout << *specificLetter << "is not an acceptable entry. Please re-enter your word." ;
errorVerif = true ;
}
if (isspace(*specificLetter))
{
cout << *specificLetter << "is not an acceptable entry. Please re-enter your word." ;
errorVerif = true ;
}
*specificLetter++;
Counter++;
}
return errorVerif;
}
void firstLetter(string wordInput)
{
char firstLetter;
firstLetter = wordInput.at(0);
if (firstLetter == 'a' || firstLetter == 'e' || firstLetter == 'i' || firstLetter == 'o' || firstLetter == 'u' )
{
cout << "In Pig Latin, that would be " << wordInput << "way" << endl;
}
else
{
wordTranslate(wordInput);
}
}
void wordTranslate(string wordInput)
{
char firstLetter;
char lastLetter = wordInput.length();
firstLetter = wordInput.at(0);
string otherLetters = wordInput.substr((1), (lastLetter - 1));
cout << "In Pig Latin, that would be " << otherLetters << firstLetter << "ay." << endl;
}
Aug 21, 2013 at 9:56am UTC
atoi is a function to convert strings to integers.
replace
string wordInput = atoi(argv[1]);
with
1 2
// uses the string(const char*) constructor
string wordInput = string(argv[1]);
Aug 21, 2013 at 4:26pm UTC
Thanks. I changed and got this error when executed.
[code]
pig_latin.cpp:(.text+0x136): undefined reference to `firstLetter(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status
[code]
Aug 21, 2013 at 4:48pm UTC
line 75 should be int
or unsigned int
?
Topic archived. No new replies allowed.