PigLatin C++ function - c++

This code is giving me a lot of strange errors. For whatever reason, the "newstring" function is not running. I think it may have something to do with the fact that it is part of a cout statement, because if I remember correctly, it does not give the same error if I call the function independently of the cout statement. The program requires a string function, but the new function is not running for some reason. Could anyone take a look at the code?
#include <iostream>
#include <string>
using namespace std;
void newstring(string);
bool isVowel(char ch);
string rotate(string pStr);
string pigLatinString(string pStr);
int main()
{
string str;
cout << "Enter a sentence to be translated to Pig Latin: ";
getline(cin, str);
cout << endl;
cout << "The pig Latin form of " << str << " is: " << newstring(str);
system("PAUSE");
return 0;
}
bool isVowel(char ch)
{
switch(ch)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'Y':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'y':
return true;
default:
return false;
}
}
string rotate(string pStr)
{
string::size_type len = pStr.length();
string rStr;
rStr = pStr.substr(1, len - 1) + pStr[0];
return rStr;
}
string pigLatinString(string pStr)
{
string :: size_type len;
bool foundVowel;
if (isVowel(pStr[0]))
pStr = pStr + "-way";
else
{
pStr = pStr + '-';
pStr = rotate(pStr);
len = pStr.length();
foundVowel = false;
for ( int counter = 1; counter < len - 1; counter++)
{
if (isVowel(pStr[0]))
{
foundVowel = true;
break;
}
else
pStr = rotate(pStr);
if (!foundVowel)
pStr = pStr.substr(1, len) + "-way";
else
pStr = pStr + "ay";
}
return pStr;
}
}
string newstring(string sentence)
{
string newsentence, currentword;
for (int i = 0; i < sentence.length(); i++)
{
if (sentence[i]==' ')
{
pigLatinString(currentword)+" ";
currentword.clear();
}
else
{
currentword+=sentence[i];
}
}
return newsentence;
}

Your newstring prototype is wrong.
void newstring(string);
Should be
string newstring(string);

Function newstring is declared as having type void
void newstring(string);
You may not create objects of type void and send them in an output stream
cout << "The pig Latin form of " << str << " is: " << newstring(str);
Also the function has no definition because you defined another function with the same name but returning std::string
string newstring(string sentence)
^^^^^^^^^^^^^^^^^^

pigLatinString(currentword)+" ";
pigLatinString returns a string, but you don't do anything with that result.
newstring returns newsentence, but is is empty.
Maybe you should fill newsentence with what is returned from pigLatinString?
Oh, and now I notice you have two newstrings... a void and a string...

Related

How to ignore all characters except first character?

#include <iostream>
using namespace std;
int main ()
{
char letter;
while (cin>>letter)
{
switch (letter)
{
case 'a':
cout<<"ant"<<endl;
break;
default :
cout <<"enter only lower cases letters "<<endl;
}
}
return 0;
}
Is there any feature of c++ that ignores the characters next to first character? Because i.e., if I enter aaa it displays ant ant ant, so I want to get rid of this part. I hope you get my question.
Read a string and then switch on the first character. Like this.
int main () {
string word;
while (cin >> word) {
switch (word[0]) {
case 'a':
cout << "ant" << endl;
break;
default:
cout << "enter only lower cases letters " << endl;
break;
}
}
return 0;
}
You can treat the user input as a std::string and then just look at the first character from it for your switch statement. This will ignore anything the user inputs after the first character. I can't imagine the use case for this, but I believe this is what you're asking for.
#include <cstdlib>
#include <iostream>
int main( int argc, char *argv[] )
{
std::string word;
while (std::cin >> word)
{
char letter = word[0];
switch (letter)
{
case 'a':
std::cout << "ant" << std::endl;
break;
default:
std::cout << "please enter only lower case letters" << std::endl;
break;
}
}
return EXIT_SUCCESS;
}
Read chars repeatedly and keep track of what's been added
#include <set>
int main () {
char letter;
std::set<char> used;
while (cin >> letter) {
if (!used.insert(letter)[1]) // note returns a pair; 2nd item ([1]) is true if it didn't exist before
continue;
switch (letter) {
case 'a':
cout << "ant" << endl;
break;
default:
cout << "enter only lower cases letters " << endl;
break;
}
}
return 0;
}
Only first char will be saved
int main()
{
char c = 0;
c = getchar();
putchar(c);
return 0;
}

Trouble with whole sentence manipulation

Im making a pig latin converter, I can get it to manipulate one word and need it to convert whole sentences, making each word outputted in pig latin, below is my code:
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;
bool isVowel (char);
string rotate(string);
string plString(string);
int main()
{
string str;
cout <<"Enter a sentence: ";
getline (cin, str);
cout << endl;
cout << str <<" in pig latin - " << plString(str) << endl;
ifstream infile;
infile.open("input.txt");
infile >> str;
while(infile)
{
cout << plString(str) << " ";
infile >> str;
}
cout << endl;
}
bool isVowel (char ch)
{
switch (ch)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'Y':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'y':
return true;
default:
return false;
}
}
string rotate(string str)
{
string::size_type len = str.length();
string str1;
str1 = str.substr(1, len - 1) + str[0];
return str1;
}
string plString(string str)
{
string::size_type len;
bool vowel;
string::size_type counter;
if (isVowel (str[0]))
str = str + "way";
else
{
str = str + "";
str = rotate(str);
len = str.length();
vowel = false;
for (counter = 1; counter < len - 1; counter++)
if (isVowel (str[0]))
{
vowel = true;
break;
}
else
str = rotate(str);
if(!vowel)
str = str.substr(1,len) + "way";
else
str = str + "ay";
}
return str;
}
any advice on how to get this to do full sentences would be greatly appreciated!
You would need to split your string into words (splitting by white space delimiter). Maybe this would help if you insert it after the getline call instead of the " in pig latin - " line:
char delimiter = ' ';
string::size_type i = 0;
string::size_type j = str.find(delimiter);
while (j != string::npos) {
string word = str.substr(i, j-i);
std::cout << plString(word) << std::endl;
i = ++j;
j = str.find(delimiter, j);
if (j == string::npos) {
word = str.substr(i, s.length());
std::cout << plString(word) << std::endl;
}
}

Pig Latin converter using toupper

I'm having trouble converting using toupper on the first character in my string.
I used tolower(first[0]) to turn the first letter into lower case.
Why doesn't toupper(first[0]) make the first character upper case?
Also, is there a way to move the first character in a string to the last spot?
Thanks a lot in advance.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
char ans;
do{
string first, last;
char first_letter, first_letter2;
cout << "This program will convert your name "
<< "into pig latin.\n";
cout << "Enter your first name: \n";
cin >> first;
cout << "Enter your last name: \n";
cin >> last;
cout << "Your full name in pig latin is ";
for(int x = 0; x < first.length(); x++){
first[x] = tolower(first[x]);
}
for(int x = 0; x < last.length(); x++){
last[x] = tolower(last[x]);
}
first_letter = first[0];
bool identify;
switch (first_letter)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
identify = true;
break;
default:
identify = false;
}
if(identify == true){
toupper(first[0]);
cout << first << "way" << " ";
}
first_letter2 = last[0];
bool identify2;
switch (first_letter2)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
identify2 = true;
break;
default:
identify2 = false;
}
if(identify2 == true){
toupper(first[0]);
cout << last << "way" << endl;
}
cout << "You you like to try again? (Y/N)\n";
cin >> ans;
} while(ans == 'y' || ans == 'Y');
return 0;
}
Just a simple blunder, compare
first[x] = tolower(first[x]);
with
toupper(first[0]);
usual case of the 'can't see the obvious thing missing' syndrome... I hate those mistakes.
As for moving the first character to the end I'd usually just use substr() for a simple case:
str = str.substr(1) + str[0];

creating complement of DNA sequence and reversing it C++

So I am trying to create the complement of the sequence
TGAGACTTCAGGCTCCTGGGCAACGTGCTGGTCTGTGTGC
however my output didn't work as expected.
The complements for each letter in the sequence are
A -> T
G -> C
C -> G
T -> A
I've been programming in Java for over a year now so I've gotten really rusty with pointers in C++, I'm guessing the problem lies in the reverse methods and the way to pointers are shifted around through each pass of the function call
#include<stdio.h>
#include<iostream>
using namespace std;
void reverse(char s[]);
int main() {
char s[40] = {'T','G','A','G','A','C','T','T','C','A','G','G','C','T','C','C','T','G','G','G','C','A','A','C','G','T','G','C','T','G','G','T','C','T','G','T','G','T','G'};
cout << "DNA sequence: "<< endl << s << endl;
reverse(s);
cout << "Reverse Compliment: "<< endl << s << endl;
system("pause");
}
void reverse(char s[])
{
char c;
char *p, *q;
p = s;
if (!p)
return;
q = p + 1;
if (*q == '\0')
return;
c = *p;
reverse(q);
switch(c) {
case 'A':
*p = 'T';
break;
case 'G':
*p = 'C';
break;
case 'C':
*p = 'G';
break;
case 'T':
*p = 'A';
break;
}
while (*q != '\0') {
*p = *q;
p++;
q++;
}
*p = c;
return;
}
Standard modern C++ makes this low-level, pointer-oriented programming, unnecessary (in fact, you're effectively writing C).
Once you have a function, say complement, which transforms a nucleotide to its complement, you just need to apply some standard library function like transform.
Here is a rewrite of your program in C++11:
#include <string>
#include <iostream>
#include <algorithm>
#include <cassert>
using namespace std;
char complement(char n)
{
switch(n)
{
case 'A':
return 'T';
case 'T':
return 'A';
case 'G':
return 'C';
case 'C':
return 'G';
}
assert(false);
return ' ';
}
int main()
{
string nucs = "ACAATTGGA";
transform(
begin(nucs),
end(nucs),
begin(nucs),
complement);
cout << nucs << endl;
}
std::string style, looks easy and clean: I presume it may be useful to OP or others.
creating complement of DNA sequence and reversing it C++
In other words, it is reverse complement of a DNA sequence, which can be easily achieved by reversing the DNA sequence and then getting its complement. Or getting the complement and then reversing. An example is shown below.
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
std::string DNAseq = "TGAGACTTCAGGCTCCTGGGCAACGTGCTGGTCTGTGTGC";
reverse(DNAseq.begin(), DNAseq.end());
for (std::size_t i = 0; i < DNAseq.length(); ++i){
switch (DNAseq[i]){
case 'A':
DNAseq[i] = 'T';
break;
case 'C':
DNAseq[i] = 'G';
break;
case 'G':
DNAseq[i] = 'C';
break;
case 'T':
DNAseq[i] = 'A';
break;
}
}
std::cout << "reverse complement : " << DNAseq << std::endl;
return 0;
}
Change the reverse method like this
void reverse(char s[])
{
while (*s) {
switch(*s) {
case 'A':
*s = 'T';
break;
case 'G':
*s = 'C';
break;
case 'C':
*s = 'G';
break;
case 'T':
*s = 'A';
break;
}
++s;
}
return;
}
...and you will get correct result.
If you don't like pointers, please, don't use them! In modern C++ pointers aren't often necessary. The following code is C++11 (do you have C++11 compiler?) written as I would do it.
#include <string>
#include <iostream>
#include <algorithm>
std::string reverse(std::string seq)
{
auto lambda = [](const char c) {
switch (c) {
case 'A':
return 'T';
case 'G':
return 'C';
case 'C':
return 'G';
case 'T':
return 'A';
default:
throw std::domain_error("Invalid nucleotide.");
}
};
std::transform(seq.cbegin(), seq.cend(), seq.begin(), lambda);
return seq;
}
int main()
{
std::string seq("TGAGACTTCAGGCTCCTGGGCAACGTGCTGGTCTGTGTG");
std::cout << "DNA sequence: " << std::endl << seq << std::endl;
seq = reverse(seq);
std::cout << "Reverse Compliment: " << std::endl << seq << std::endl;
system("pause");
return EXIT_SUCCESS;
}
Some notes:
Use default in switch statement.
In C++ it's better to return a value from a function than to manipulate a variable via pointer.
Prefer std::string to plain char.

Program is ignoring input

I'm trying to write a simple brainfuck interpreter in C++. It works great so far, but it ignores the character input command (',').
The Interpreter:
#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;
#define SIZE 30000
void parse(const char* code);
int main(int argc, char* argv[])
{
ifstream file;
string line;
string buffer;
string filename;
cout << "Simple BrainFuck interpreter" << '\n';
cout << "Enter the name of the file to open: ";
cin >> filename;
cin.ignore();
file.open(filename.c_str());
if(!file.is_open())
{
cout << "ERROR opening file " << filename << '\n';
system("pause");
return -1;
}
while (getline(file, line)) buffer += line;
parse(buffer.c_str());
system("pause");
return 0;
}
void parse(const char* code)
{
char array[SIZE];
char* ptr = array;
char c;
int loop = 0;
unsigned int i = 0;
while(i++ < strlen(code))
{
switch(code[i])
{
case '>': ++ptr; break;
case '<': --ptr; break;
case '+': ++*ptr; break;
case '-': --*ptr; break;
case '.':
cout << *ptr;
break;
case ',':
cin >> *ptr;
break;
case '[':
if (*ptr == 0)
{
loop = 1;
while (loop > 0)
{
c = code[++i];
if (c == '[') loop ++;
else if (c == ']') loop --;
}
}
break;
case ']':
loop = 1;
while (loop > 0)
{
c = code[--i];
if (c == '[') loop --;
else if (c == ']') loop ++;
}
i --;
break;
}
}
cout << '\n';
}
The UtraSimple brainfuck code that breaks everything:
,.
Does anyone know what causes it to skip the input character?
I'd be looking at this for a start:
unsigned int i = 0;
while(i++ < strlen(code)) // increments i NOW !
{
switch(code[i]) // uses the incremented i.
The first character that will get processed there will be code[1], not code[0].
So the program ",." will first process . then \0 (end of string) hence there will be no input command , processed.
You can see this if you change the code as follows:
unsigned int i = 0;
while(i++ < strlen(code))
{
cout << "DEBUG [" << i << ":" << (int)code[i] << ":" << code[i] << "]\n";
switch(code[i])
and you'll see:
DEBUG [1:46:.]
DEBUG [2:0: ]
You need to hold off on incrementing i until after you're finished with it.