Is there a way to bypass error C2137 on Visual Studio Community 2015? I am removing characters with stringstream but I do not want to replace them (even with a blank space), I want to erase them so, if I want to remove all 'o' in 'cool' it becomes 'cl' and not 'c l'. I saw in Stroustrup's book he wrote a if (...) ch = ''; but my compiler returns me an error and my best proxy is white space that's still unacceptable.
Here's my function with C2137:
string rem_vow(string& s)
{
for (char& c : s)
{
switch (c)
{
case 'A': case 'a': case 'E': case 'e': case 'I':
case 'i': case 'O': case 'o': case 'U': case 'u':
c = '';
break;
default:
break;
}
}
return s;
}
EDIT:
That's the code I saw in the book:
Thank you in advance
No, in order to remove a character in a string you will have to move the rest of the string one step, you cannot simple replace it with "empty" character. You could use the erase method though, but then you should probably not do that while iterating the string.
What you probably should do is to build a new string as you traverse the original string, something like:
string rem_vow(string const& s)
{
string res;
for (char c : s)
{
switch (c)
{
case 'A': case 'a': case 'E': case 'e': case 'I':
case 'i': case 'O': case 'o': case 'U': case 'u':
//c = ' ';
break;
default:
res.push_back(c);
break;
}
}
return res;
}
Related
#include <string>
#include <iostream>
using namespace std;
int main() {
string input, numBin = "";
cout << "Enter a hexadecimal number: ";
getline(cin, input);
for (int i = 0; i < input.length(); i++) {
switch (input[i]) {
case 0: numBin.append("0000"); break;
case 1: numBin.append("0001"); break;
case 2: numBin.append("0010"); break;
case 3: numBin.append("0011"); break;
case 4: numBin.append("0100"); break;
case 5: numBin.append("0101"); break;
case 6: numBin.append("0110"); break;
case 7: numBin.append("0111"); break;
case 8: numBin.append("1000"); break;
case 9: numBin.append("1001"); break;
case 'a': numBin.append("1010"); break;
case 'A': numBin.append("1010"); break;
case 'b': numBin.append("1011"); break;
case 'B': numBin.append("1011"); break;
case 'c': numBin.append("1100"); break;
case 'C': numBin.append("1100"); break;
case 'd': numBin.append("1101"); break;
case 'D': numBin.append("1101"); break;
case 'e': numBin.append("1110"); break;
case 'E': numBin.append("1110"); break;
case 'f': numBin.append("1111"); break;
case 'F': numBin.append("1111"); break;
default: break;
}
}
cout << "Your number in binary is " << numBin << ".";
}
This program is supposed to change a hexadecimal input ('input') into a binary result ('numBin'). I don't have much experience using switch statements and do not fully understand the "default" case, so any clarification about that or if I am using it incorrectly would be helpful!
The error I'm getting is on the for loop, and it thorws: comparison between signed and unsigned integer expressions [-Wsign-compare]
In the line:
for (int i = 0; i < input.length(); i++) ...
input.length() returns a size_t, which is a unsigned type.
(see http://www.cplusplus.com/reference/string/string/length/)
Comparing signed and unsigned values is not safe, which is why the compiler warns you, read more about it in this post among many others:
A warning - comparison between signed and unsigned integer expressions
To fix it, simply change to
unsigned int i = 0
The default switch case will be executed when none of the other cases match. You should put some code there that handles incorrect input for example.
case '0':
case '1':
...
Use all characters..not number and characters.
And one ore thing..for(i=0;i<(int) input.length();i++)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I have made a simple encryption function,which encrypts everything except 0-9 numbers (ignoring the special characters).
Here is the code.
#include <iostream>
using namespace std;
void encrypt(char s[])
{
char *ptr;
ptr=s;
while(*ptr)
{
switch (*ptr)
{
case 'a': *ptr='b';
break;
case 'b': *ptr='a';
break;
case 'c': *ptr='z';
break;
case 'd': *ptr='y';
break;
case 'e': *ptr='c';
break;
case 'f': *ptr='d';
break;
case 'g': *ptr='x';
break;
case 'h': *ptr='g';
break;
case 'i': *ptr='i';
break;
case 'j': *ptr='h';
break;
case 'k': *ptr='f';
break;
case 'l': *ptr='j';
break;
case 'm': *ptr='q';
break;
case 'n': *ptr='o';
break;
case 'o': *ptr='p';
break;
case 'p': *ptr='m';
break;
case 'q': *ptr='n';
break;
case 'r': *ptr='l';
break;
case 's': *ptr='k';
break;
case 't': *ptr='x';
break;
case 'u': *ptr='w';
break;
case 'v': *ptr='u';
break;
case 'w': *ptr='v';
break;
case 'x': *ptr='t';
break;
case 'y': *ptr='s';
break;
case 'z': *ptr='r';
break;
case 1: *ptr=5;
break;
case 2: *ptr=6;
break;
case 3: *ptr=0;
break;
case 4: *ptr=1;
break;
case 5: *ptr=2;
break;
case 6: *ptr=7;
break;
case 7: *ptr=4;
break;
case 8: *ptr=3;
break;
case 9: *ptr=8;
break;
case 0: *ptr=9;
break;
default: *ptr=*ptr;
break;
}
*ptr++;
}
*ptr='\0';
}
int main()
{
char password[10];
cout<<"Enter the password\n";
cin>>password;
encrypt(password);
cout<<password<<endl;
return 0;
}
Here is a sample output
sh-4.3$ main
Enter the password
thisisanex!!1234567
xgikikboct!!1234567
You need to use the character '1' not the integer value 1.
So use case '1': instead of case 1: and so on for the other numbers.
As nos said, the character is different from the integer value.
However, looking at your code, it would make a lot more sense to do this:
#include <iostream>
#include <string>
using namespace std;
int main(){
string charset = "abcdefghijklmnopqrstuvwxyz1234567890";
string scrambledcharset = "r5b6ng1fcl8htau9i74kxy0vjw3psemqz2do"; //Whatever order you want
string uIn;
string output;
cout << "Enter your string: ";
cin >> uIn;
cin.ignore();
for(int i = 0; i < uIn.length(); i++){
for(int j = 0; j < charset.length(); j++){
if(uIn[i] == charset[j]){
output += scrambledcharset[j];
}
}
}
cout << "\nScrambled: " << output;
return 0;
}
I have made a program in c++ for encoding and decoding morse code.
My program is working and the decoded message is fine but without spaces is there any way to add space.
#include <iostream>
#include <string>
using namespace std;
string translateMorseCode(string sentence);
string decoceMorseCode (string sentence);
int main()
{
string sentence;
cout<<"Enter word or sentence: ";
getline(cin,sentence);
cout<<"\nMorse Code is:\n";
//convert input message into morse
cout<<translateMorseCode(sentence)<<endl;
//copying morse code into decode string for decoding
string decode = translateMorseCode(sentence);
cout<<"\nDecoding morse code is text"<<endl;
//converting back ito text string
cout<<decoceMorseCode (decode);
return 0;
}
string decoceMorseCode (string sentence)
{
string delimiter = " ";
string decode ="";
int pos = 0;
string token;
while ((pos = sentence.find(delimiter)) != string::npos) {
token = sentence.substr(0, pos);
if(token==".-")
{
decode.append("a");
}
else if(token=="-...")
{
decode.append("b");
}
else if(token=="-.-.")
{
decode.append("c");
}
else if(token=="-..")
{
decode.append("d");
}
else if(token==".")
{
decode.append("e");
}
else if(token=="..-.")
{
decode.append("f");
}
else if(token=="--.")
{
decode.append("g");
}
else if(token=="....")
{
decode.append("h");
}
else if(token=="..")
{
decode.append("i");
}
else if(token==".---")
{
decode.append("j");
}
else if(token=="-.-")
{
decode.append("k");
}
else if(token==".-..")
{
decode.append("l");
}
else if(token=="--")
{
decode.append("m");
}
else if(token=="-.")
{
decode.append("n");
}
else if(token=="---")
{
decode.append("o");
}
else if(token==".--.")
{
decode.append("p");
}
else if(token=="--.-")
{
decode.append("q");
}
else if(token==".-.")
{
decode.append("r");
}
else if(token=="...")
{
decode.append("s");
}
else if(token=="-")
{
decode.append("t");
}
else if(token=="..-")
{
decode.append("u");
}
else if(token=="...-")
{
decode.append("v");
}
else if(token==".--")
{
decode.append("w");
}
else if(token=="-..-")
{
decode.append("x");
}
else if(token=="-.--")
{
decode.append("y");
}
else if(token=="--..")
{
decode.append("z");
}
else if(token=="-----")
{
decode.append("0");
}
else if(token==".----")
{
decode.append("1");
}
else if(token=="..---")
{
decode.append("2");
}
else if(token=="...--")
{
decode.append("3");
}
else if(token=="....-")
{
decode.append("4");
}
else if(token==".....")
{
decode.append("5");
}
else if(token=="-....")
{
decode.append("6");
}
else if(token=="--...")
{
decode.append("7");
}
else if(token=="---..")
{
decode.append("8");
}
else if(token=="----.")
{
decode.append("9");
}
sentence.erase(0,pos + delimiter.length());
}
return decode ; // returnung decoded text
}
//function convert input message into morse return Morse Code as String
string translateMorseCode(string sentence)
{
string MorseCode="";
for(int i=0;i<sentence.length();i++){
switch (sentence[i]){
case 'a':
case 'A':
MorseCode.append(".- ");
break;
case 'b':
case 'B':
MorseCode.append("-... ");
break;
case 'c':
case 'C':
MorseCode.append("-.-. ");
break;
case 'd':
case 'D':
MorseCode.append("-.. ");
break;
case 'e':
case 'E':
MorseCode.append(". ");
break;
case 'f':
case 'F':
MorseCode.append("..-. ");
break;
case 'g':
case 'G':
MorseCode.append("--. ");
break;
case 'h':
case 'H':
MorseCode.append(".... ");
break;
case 'i':
case 'I':
MorseCode.append(".. ");
break;
case 'j':
case 'J':
MorseCode.append(".--- ");
break;
case 'k':
case 'K':
MorseCode.append("-.- ");
break;
case 'l':
case 'L':
MorseCode.append(".-.. ");
break;
case 'm':
case 'M':
MorseCode.append("-- ");
break;
case 'n':
case 'N':
MorseCode.append("-. ");
break;
case 'o':
case 'O':
MorseCode.append("--- ");
break;
case 'p':
case 'P':
MorseCode.append(".--. ");
break;
case 'q':
case 'Q':
MorseCode.append("--.- ");
break;
case 'r':
case 'R':
MorseCode.append(".-. ");
break;
case 's':
case 'S':
MorseCode.append("... ");
break;
case 't':
case 'T':
MorseCode.append("- ");
break;
case 'u':
case 'U':
MorseCode.append("..- ");
break;
case 'v':
case 'V':
MorseCode.append("...- ");
break;
case 'w':
case 'W':
MorseCode.append(".-- ");
break;
case 'x':
case 'X':
MorseCode.append(".-- ");
break;
case 'y':
case 'Y':
MorseCode.append("-.-- ");
break;
case 'z':
case 'Z':
MorseCode.append("--.. ");
break;
case ' ':
MorseCode.append(" ");
break;
case '1':
MorseCode.append(".---- ");
break;
case '2':
MorseCode.append("..--- ");
break;
case '3':
MorseCode.append("...-- ");
break;
case '4':
MorseCode.append("....- ");
break;
case '5':
MorseCode.append("..... ");
break;
case '6':
MorseCode.append("-.... ");
break;
case '7':
MorseCode.append("--... ");
break;
case '8':
MorseCode.append("---.. ");
break;
case '9':
MorseCode.append("----. ");
break;
case '0':
MorseCode.append("----- ");
break;
}
}
return MorseCode;// return Morse Code
}
The problem is that you use a double space in your morse output to encode a word boundary, but your decoder skips all spaces. It therefore skips double spaces too, which is why it doesn't know where to put a space in the decoded output.
According to wiki:
Each character (letter or numeral) is represented by a unique sequence of dots
and dashes. The duration of a dash is three times the duration of a dot. Each
dot or dash is followed by a short silence, equal to the dot duration. The
letters of a word are separated by a space equal to three dots (one dash), and
the words are separated by a space equal to seven dots. The dot duration is the
basic unit of time measurement in code transmission.[1] To increase the speed of
the communication, the characters are encoded so the length of each character in
Morse is approximately inversely proportional to its frequency of occurrence in
English. Thus, the most common letter in English, the letter "E," has the
shortest code, a single dot.
So in my opinion:
Two "\s" should be between words.
One "\s" between single char.
#Neska gave you the basic answer but did not explain where the space gets interpreted.
When you get the tag decoded your logic should be able to detect the silence between the dots and dashes. Otherwise, it would not be able to determine where an individual character ends and the next character begins. That logic should detect the longer (seven dots long) interval between the end of a word and the beginning of the next work. Put in a special character (not a . or a dash) to be interpreted as a space in your morse code interpreter.
If you are interpreting text input and searching for the next delimiter (such as a space) in order to translate the dots and dashes, output the delimiters as well. That will put in the appropriate number of spaces
For example
.-^-^^-...^-.--
translates to
at^by
where the ^ character shows where the space should be.
Your code translates this as atby
I have found a solution by myself and it was quite simple - I just added
else if (" ")
{
decode.append(" ");
}
before the
sentence.erase(0,pos + delimiter.length());
I am quiet new with C++ and I need to read an input from a MSVC++ text-field and write it to a file. I need to write \n as a new line to the file and not as \n.
After some researching I found that escape characters only work at compile-time. Is it possible for me to use it on run-time. I am only using C++ for this task.
I might write this a bit differently if I were doing it in C++ today (I wrote this in C around 20 years ago), but it might at least provide a little inspiration:
/*
** Public Domain by Jerry Coffin.
**
** Interprets a string in a manner similar to that the compiler
** does string literals in a program. All escape sequences are
** longer than their translated equivalant, so the string is
** translated in place and either remains the same length or
** becomes shorter.
*/
#include <string.h>
#include <stdio.h>
#include "snip_str.h"
char *translate(char *string)
{
char *here=string;
size_t len=strlen(string);
int num;
int numlen;
while (NULL!=(here=strchr(here,'\\')))
{
numlen=1;
switch (here[1])
{
case '\\':
break;
case 'r':
*here = '\r';
break;
case 'n':
*here = '\n';
break;
case 't':
*here = '\t';
break;
case 'v':
*here = '\v';
break;
case 'a':
*here = '\a';
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
numlen = sscanf(here,"%o",&num);
*here = (char)num;
break;
case 'x':
numlen = sscanf(here,"%x",&num);
*here = (char) num;
break;
}
num = here - string + numlen;
here++;
memmove(here,here+numlen,len-num );
}
return string;
}
#include <iostream>
using namespace std;
int main(){
char i;
cin >>i;
switch (i){
case ('e'||'i'||'o'||'u'||'a'):
cout<<"Vowel";
break;
case ('+'||'-'||'/'||'*'||'%'):
cout<<"Op";
break;
}
return 0;
}
if not than how can we use comparison or logical operators in switch ?
& why cant we declare and initialize variable in single case without using scope ?
Without a break statement the previous cases "fall through" so this achieves the || you were looking for:
#include <iostream>
using namespace std;
int main(){
char i;
cin >>i;
switch (i){
case 'e':
case 'i':
case 'o':
case 'u':
case 'a':
cout<<"Vowel";
break;
case '+':
case '-':
case '/':
case '*':
case '%':
cout<<"Op";
break;
}
return 0;
}
The answer to the other part of your question is discussed in depth already on stackoverflow.
You can use fallthrough to map multiple case values to the same action.
The diagnostic message explains it -- it would be possible to jump over the initialization. But isn't it just a warning?
No, you can't; in switches you can only implicitly use the == operator and only on integral and enumeration types (ยง6.4.2). You should rewrite that switch as
switch (i){
case 'e':
case 'i':
case 'o':
case 'u':
case 'a':
cout<<"Vowel";
break;
case '+':
case '-':
case '/':
case '*':
case '%':
cout<<"Op";
break;
}
which exploits the fall-through feature of the switch statement.
if not than how can we use comparison or logical operators in switch ?
Simply, you can't. If you want to do anything different than equality comparison with integral/enumeration types you have to write several if/else statements.
& why cant we declare and initialize variable in single case without using scope ?
It's not a problem of declaration, but of initialization; see the link in #awoodland's answer.
Format it like this:
switch (i)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
cout << "Vowel";
break;
}
Alternative, more terse solution:
#include <cstring>
// ...
if (strchr("eioua", i)) cout << "vowel";
if (strchr("+-/*%", i)) cout << "operator";
Note that strchr considers the terminating zero part of the string, so i should not be 0.
We could, except it doesn't mean what is intended (and yields the same value in both cases): you'd be performing a logical or on a bunch of non-zero integer values and the result is true in both cases.