Online C++ Grader Syntax Issue - c++

Im trying to use an online marker for a c++ programming site. The code runs fine on my computer. However, the site is repeatedly having a compilation error, spitting out these syntax errors:
/data/grader/2/81322/compile/source.cc: In function �int main()�:
/data/grader/2/81322/compile/source.cc:16: error: expected initializer before �:� token
/data/grader/2/81322/compile/source.cc:45: error: expected primary-expression at end of input
/data/grader/2/81322/compile/source.cc:45: error: expected �;� at end of input
/data/grader/2/81322/compile/source.cc:45: error: expected primary-expression at end of input
/data/grader/2/81322/compile/source.cc:45: error: expected �)� at end of input
/data/grader/2/81322/compile/source.cc:45: error: expected statement at end of input
/data/grader/2/81322/compile/source.cc:45: error: expected �}� at end of input
/data/grader/2/81322/compile/source.cc:45: error: expected �}� at end of input
#include <iostream>
#include <string>
using namespace std;
int main()
{
char vowels[] = { 'a', 'e', 'i', 'o', 'u' };
string word;
string newWord;
getline(cin, word);
for (int i = 0; i < (int)word.length(); i++) {
bool vowel = false;
char closest = 'z';
int close = 200;
for (char &n : vowels) {
if (n == word[i]) {
vowel = true;
break;
}
else if (abs(n-word[i]) < close) {
closest = n;
close = abs(n - word[i]);
}
}
newWord += word[i];
if (!vowel) {
newWord += closest;
if (word[i] == 'z') {
newWord += 'z';
break;
}
char next = word[i]+1;
for (char n : vowels) {
if (n == next) {
vowel = true;
break;
}
}
newWord += ((vowel) ? word[i] + 2:next);
}
}
cout << newWord << endl;
//system("pause");//remove
}
Being new to C++, I cant seem to make sense of this.
Thanks in advance.

Related

need help debugging error: invalid conversion from ‘token*’ to ‘char’ [-fpermissive]

Here is my code. It keeps saying I am doing the conversion wrong. What am I doing wrong, and can anyone help me fix this?
#include <iostream>
#include <fstream>
#include "StackAr.h"
using namespace std;
struct token{
int curLine = 1, lastLine = 1;
char next;
};
int main(int argc, char *argv[])
{
ifstream inf(argv[1]);
token t1;
typedef token *t_ptr; //assign a pointer to a struct
t_ptr ptr = &t1;
t_ptr end;
StackAr<t_ptr> stack;
while (inf >> t1.next)
{
if (t1.next == '{' || t1.next == '(' || t1.next == '[')
{
t1.lastLine = t1.curLine;
ptr -> next;
stack.push(ptr);
new token;
}
else if (t1.next == '}' || t1.next == ')' || t1.next == ']')
{
t1.lastLine = t1.curLine;
char previous = stack.topAndPop(); // get previous bracket
// deal with null
switch(t1.next)
{
case '}' : if (previous != '{') {printError(&t1.next, t1.curLine); return 1;} break;
case ')' : if (previous != '(') {printError(&t1.next, t1.curLine); return 1;} break;
case ']' : if (previous != '[') {printError(&t1.next, t1.curLine); return 1;} break;
}
}
if (inf.peek() == '\n')
t1.curLine++;
}
inf.close();
if (!stack.isEmpty())
{
char previous = stack.topAndPop()-> next;
printError(&previous, t1.lastLine);
return 1;
}
cout << "OK" << endl;
return 0;
}
In function int main(int, char**):
mockbalance.cpp:64:38: error: invalid conversion from ‘`token*`’ to ‘`char`’ [-fpermissive]
char previous = stack.topAndPop(); // get previous bracket
mockbalance.cpp:82:36: error: invalid conversion from ‘`token*`’ to ‘`char`’ [-fpermissive]
char previous = stack.topAndPop()-> next;
edit: the above code has been given a temporary fix as suggested by one of the users in the comment section but i fully recommend using a better structure also look at comments. Given notice any one who plans to use this, ago ahead but keep in mind this code in particular has been already submitted to moss database and should be used to learn.

Logic Error when trying to output a decrypted string from an encrypted string in Visual Studios C++

my program takes a encrypted string into a class function EncryptedString(string str). It seems like everything is being outputted correctly aside from when I try to call a get function to get the decrypted string I am met with "ZZZZZZZZ". The Program takes a phrase or sentence, encrypts the sentence and deletes any illegal characters, then decrypts it and outputs the decrypted result. I am putting in "Hello World!" and it deletes the ! just fine. The space is supposed to kept however it too is also turned into a Z.
Input: Hello World!
Expected result: Hello World
Output: ZZZZZZZZZZZ
I am also having a problem with the output of my encryption. I am also to output the encrypted version of the phrase. However when I output it, nothing is being put out.
Input: Hello World!
Expected result: Ifmmp Xpsme
Output is blank.
Here is the code for the entire EncryptedString.cpp file. Thank you to whoever helps me with this problem and if you need to see the main.cpp file or the header file for that declares the functions I will be happy to provide, it is just I do not think they are not necessary with this error. However I could be wrong.
#include "EncryptedString.h"
string decrypted;
EncryptedString::EncryptedString(string str)
{
string enCrypt = str;
set(enCrypt);
}
void EncryptedString::set(string str)
{
char chBase = 'A';
string enCry = str;
for (int i = 0; i < enCry.length(); i++)
{
char ch = enCry[i];
if ((enCry[i] < chBase || (enCry[i] > chBase + 25 && enCry[i] < tolower(chBase)) || enCry[i] > tolower(chBase + 25)) && enCry[i] != ' ')
{
enCry.erase(enCry.begin() + i);
}
else
{
if (enCry[i] = chBase + 25)
{
enCry[i] = 'A';
}
else if (enCry[i] = tolower(chBase) + 25)
{
enCry[i] = 'a';
}
else if (enCry[i] = ' ')
{
enCry[i] = ' ';
}
else
{
enCry[i] = ch + 1;
}
}
}
EncryptedString::encryption == enCry;
string decrypt = enCry;
for (int i = 0; i < decrypt.length(); i++)
{
char ch = decrypt[i];
if (decrypt[i] = 'A')
{
decrypt[i] = 'Z';
}
else if (decrypt[i] = 'a')
{
decrypt[i] = 'z';
}
else if (decrypt[i] = ' ')
{
decrypt[i] = ' ';
}
else
{
decrypt[i] = ch - 1;
}
}
decrypted = decrypt;
}
const string EncryptedString::get()
{
return decrypted;
}
const string EncryptedString::getEncrypted()
{
return EncryptedString::encryption;
}
The issue is that you're using the wrong operator for equality comparisons:
if (enCry[i] = chBase + 25)
In C++, you use == to compare for equality, not =. The above line should be:
if (enCry[i] == chBase + 25)
You make similar errors in several other lines in your program. Correct those errors and rerun your program.

How do I replace a single character like 'A' with something like "10"?

#include <iostream>
#include <string>
using namespace std;
int main ()
{
string s;
cin >> s;
for (int i = 0; i < s.size (); i++)
{
if (s[i] == 'A')
{
s[i] = "10";
}
cout << s[i];
}
return 0;
}
I am getting following error:
main.cpp: In function
'int main()': main.cpp:10:5: error: invalid conversion from 'const char*' to 'char' [-fpermissive] s[i]= "10";
Any help would be highly appreciated. Thank you.
You can find the position of A, starting from index 0 until end of the string and whenever you find, replace it with 10 using the information of both position where you found and the length of the string you would like to find in the given string.
Something like follows: https://www.ideone.com/dYvF8d
#include <iostream>
#include <string>
int main()
{
std::string str;
std::cin >> str;
std::string findA = "A";
std::string replaceWith = "10";
size_t pos = 0;
while ((pos = str.find(findA, pos)) != std::string::npos)
{
str.replace(pos, findA.length(), replaceWith);
pos += replaceWith.length();
}
std::cout << str << std::endl;
return 0;
}
why not use string::find() to locate the character you want to replace and then string::insert to insert a "10"? maybe not the best way, but can finish it correctly.
You code's question is that the operator[] of std::string returns a char&, and you can not assign a string literal to it.
And I think I should give you a function to implement it.
void Process(std::string& op)
{
auto pos=op.find('A');
op.erase(pos,1);
if(pos!=std::string::npos)
{
op.insert(pos,"10");
}
}

Error passing strings and char to bool function

I am trying to create a function that looks at a '-' sign and checks whether is a minus sign or a negative sign based on if it has a ' ' (space) in front of it. I am doing so by comparing the current char I have (infix[x]) and comparing to infix[x+1]; I keep getting error but im unsure if its because im not passing correctly or something else?
for(unsigned x = 0; x < infix.length(); ++x)
{
// place numbers (standard, decimal, & negative)
// numbers onto the 'postfix' string
if((isdigit(infix[x])) || (infix[x] == '.'))
{
postfix += infix[x];
}
else if ((infix[x] == '-'))
{
if(checkfornegative(infix[x], infix)== 1)) // error: expected primary-expression before ‘)’ token
if(checkfornegative(infix[x], infix)== 1))
{
postfix+= " ";
}
else if(checkfornegative(infix[x], infix)== 0)) //error: expected primary-expression before ‘)’ token
if(checkfornegative(infix[x], infix)== 1))
{
postfix += infix[x];
}
}
// This is the function in using
bool checkfornegative(char C, string& QQ)
{
bool status;
if((C == '-') && (QQ[C+1] == ' '))
{
status = true;
}
else if((C == '-') && (QQ[C+1] != ' '))
{
status = false;
}
return status;
}
You are getting compilation errors because you have extra closing parenthesis in the if conditions.
if(checkfornegative(infix[x], infix)== 1)) // error: expected primary-expression before ‘)’ token
Remove the last closing parenthesis from the condition. Same goes for the second condition also.
However, there are several issues in your code but they are not compilation errors.

C++ error: expected ',' or ';' before '{' token when their is

I am trying to create a function that will return the alphabet position of a letter that is passed into the function
for example
cout<<getPosition('l')
would return the integer 12. I am fairly certain I have got the logic correct however I am having some trouble with the syntax. I found many similar questions however I still was not able to solve the problem.
Any help appreciated
#include <iostream>
using namespace std;
int getPosition(letter)
{
int pos = 0;
const char alphabet[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
for (int counter=0; counter!=26; counter++)
{
if (alphabet[counter] == letter)
{
pos = counter;
break;
}
}
return pos;
}
int main()
{
string letter = 'r';
cout << posInAlpha(letter);
return 0;
}
You are mixing std::string and char, while you don't need the first. Moreover, your main() uses a function not declared. Furthermore, your function's parameter lacks it's type. Your compiler should be pretty explanatory for these. I modified your code to get the desired behavior; please take a moment or two understanding what had to be modified here:
#include <iostream>
using namespace std;
// Returns the index of 'letter' in alphabet, or -1 if not found.
int getPosition(char letter)
{
int pos = -1;
const char alphabet[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
for (int counter=0;counter!=26;counter++)
{
if (alphabet[counter]==letter)
{
pos=counter;
break;
}
}
return pos;
}
int main()
{
char letter='r';
cout<<getPosition(letter);
return 0;
}
Output:
17
I initialized pos to -1, in case letter does not belong in the alphabet. Otherwise the function would return 0, meaning that a was the answer.
PS: If letter was an std::string, then your comparison with every letter of the alphabet would produce a compilation error, since the types do not match.
it all begins here
int getPosition(letter)
{
this function is not right declared/defined, letter must be a type and you just gave none...
assuming this
char letter = 'r';
cout << posInAlpha(letter);
that letter must be a char and the function posInAlpha should be renamed to getPosition
it all looks like you are mixing std::strings and chars,
your final fixed code should look like:
#include <iostream>
using namespace std;
int getPosition(char& letter)
{
int pos = 0;
const char alphabet[26] = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' };
for (int counter = 0; counter != 26; counter++)
{
if (alphabet[counter] == letter)
{
pos = counter;
break;
}
}
return pos;
}
int main()
{
char letter = 'r';
cout << getPosition(letter);
cin.get();
return 0;
}
which prints 17 for the given char 'r'
#include <iostream>
int getPosition( char c )
{
c |= 0x20; // to lower
return ( c < 'a' || c > 'z' )
? -1
: c - 'a';
}
int main()
{
std::cout << getPosition( 'R' );
return 0;
}
Demo