Extra Characters Inserted Into Array - c++

I'm a C++ beginner and wrote a program to check if two phrases are anagrams. The characters are being read one at a time and stored in an array. I have everything working, except in some cases, extra characters are being inserted into the array.
For example, if I enter the phrases aabb and abba, this is the output of the program:
Enter two lines that might be anagrams:
--> aabb
--> abba
String A is aabb
String B is abbai?
The two strings are NOT anagrams.
They should be anagrams, but for some reason, i? is added into the array, causing the phrases to not be anagrams. I'm probably overlooking a simple mistake in the code, but would really appreciate any feedback.
Here is the code:
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
int check_anagram(char [], char []);
int main()
{
char ch, a[60], b[60];
int flag, i;
cout << "Enter two lines that might be anagrams:" << endl;
cout << "--> ";
cin.get(ch);
ch = tolower(ch);
i = 0;
while (ch != '\n')
{
if (ch > '#') {
a[i] = ch;
i++;
}
cin.get(ch);
ch = tolower(ch);
}
cout << "--> ";
cin.get(ch);
ch = tolower(ch);
i = 0;
while (ch != '\n')
{
if (ch > '#') {
b[i] = ch;
i++;
}
cin.get(ch);
ch = tolower(ch);
}
flag = check_anagram(a, b);
cout << "String A is " << a << endl;
cout << "String B is " << b << endl;
cout << "The two strings ";
if (flag == 1)
cout << "ARE";
else
cout << "are NOT";
cout << " anagrams." << endl << endl;
return 0;
}
int check_anagram(char a[], char b[])
{
int first[26] = {0}, second[26] = {0}, c = 0;
while (a[c] != '\0')
{
first[a[c]-'a']++;
c++;
}
c = 0;
while (b[c] != '\0')
{
second[b[c]-'a']++;
c++;
}
for (c = 0; c < 26; c++)
{
if (first[c] != second[c])
return 0;
}
return 1;
}
Thanks in advance!

You just need to terminate the two character arrays with '\0' because the logic in check_anagram treats both arrays as NULL-terminated.
..
while (ch != '\n')
{
if (ch > '#') {
a[i] = ch;
i++;
}
cin.get(ch);
ch = tolower(ch);
}
a[i] = '\0'; // <<<<<<<< Add this line
cout << "--> ";
cin.get(ch);
ch = tolower(ch);
i = 0;
while (ch != '\n')
{
if (ch > '#') {
b[i] = ch;
i++;
}
cin.get(ch);
ch = tolower(ch);
}
b[i] = '\0'; // <<<<<<<< Add this line
..
Here is the result:
Enter two lines that might be anagrams:
--> aabb
--> abba
String A is aabb
String B is abba
The two strings ARE anagrams.

Related

How can I make backspace not count as a character?

So, I have to encrypt my console application with a password, i did something that's working but there is a problem, backsapce doesn't erase the character entered, it is also counted as a character, how can I make it to do its job, to erase the character?
This is the code:
void main()
{
char password[20], my_password[20] = "password";
int i;
char ch;
system("cls");
cout << "PASSWORD: ";
i = 0;
do
{
ch = _getch();
password[i] = ch;
if (ch != 27 && ch != 13 && ch != 9)
cout<<"*";
else
break;
i++;
} while (i < 19);
password[i] = '\0';
if (strcmp(password, my_password) != 0)
{
cout << "\n\nIncorrect password !!!";
cin.get();
return;
}
cout << "\n\nPassword is correct !";
cout <<"\n\nThe program is executed !";
cin.get();
}
void main()
{
char password[20], my_password[20] = "password";
int i;
char ch;
system("cls");
cout << "PASSWORD: ";
i = 0;
do
{
ch = _getch();
if (ch == 8)
{
i--;
cout << "\b \b";
continue;
}
password[i] = ch;
if (ch != 27 && ch != 13 && ch != 9)
cout << "*";
else
break;
i++;
} while (i < 19);
password[i] = '\0';
if (strcmp(password, my_password) != 0)
{
cout << "\n\nIncorrect password !!!";
cin.get();
return;
}
cout << "\n\nPassword is correct !";
cout << "\n\nThe program is executed !";
cin.get();
}
Not the cleanest code but it works. Decrement the counter to over write the previous character and output two backspace characters separated by a space.
"how can I make it to do its job, to erase the character?"
Use a curses library. Like ncurses.
You could check if the character received is a backspace, if it is decrement i which will effectively remove the last character.
i = 0;
do
{
ch = _getch(); // get the character
if(ch == DEL || ch == BS) // check for backspace
{
i--;
cout << BS;
}
else if(ch >= ' ' && ch <= '~') // check if its valid ASCII
{
password[i] = ch;
cout << "*";
i++;
}
else if (ch == 27 || ch == 13 || ch == 9) // check if entry is complete
{
break;
}
} while (i < 19);
password[i] = '\0';
somewhere else
#define BS '\b'
#define DEL 127

How to get multiple input with char and multiple output with int? C++

I'm quite new to C++, so I apologize if I am not sounding technical. I am having a little trouble getting a multiple input and output with my code, I'm thinking I should have a loop to get the data but i'm not sure how i'd go about that in my code, I've thought about using getline() but that doesn't seem to want to work with Char.
I've tried getline but I'm not sure how to implement it with the Char input, I believe I may need a separate loop as well but again not too sure. I'm thinking it could be done to EoF as well.
Here's my code:
int main()
{
char inpval;
int outval = 0;
cout << "Enter a Roman Number to convert: " << endl;
while (cin.get(inpval))
{
inpval = toupper(inpval);
if (inpval == 'M')
outval = outval + 1000;
else if (inpval == 'D') {
inpval = cin.peek();
inpval = toupper(inpval);
if (inpval == 'M') {
outval = outval - 500;
continue;
} else {
outval = outval + 500;
continue;
}
}
//etc
cout << "The Equivalent Arabic value is:" << endl;
cout << outval << "\n";
return 0;
}
My expected output is:
(All on newline)
Input:
I
II
IV
V
VI
Output:
1
2
4
5
6
Actual output is:
Input:
I
Output:
1
P.S: The program converts Roman Numeral chars to their respected number.
Any help is appreciated!
You can take input multiple items from cin, using below syntax.
cin >> a;
cin >> b;
cin >> c;
Another way is also there
cin >> a >> b >> c;
This technique is called "operator chaining" which is similar to the above.
Do you have any problem doing it like this?
cout << "Enter a Roman Numeral" << endl;
string inpval;
cin >> inpval;
while (inpval != "exit")
{
int outval = 0;
if (inpval == "I")
outval = 1;
else if (inpval == "II")
outval = 2;
else if (inpval == "III")
outval = 3;
else if (inpval == "IV")
outval = 4;
// ect
cout << "The Equivalent Arabic value is: " << outval << endl << endl;
cout << "Enter next numeral: (type exit to exit) " << endl;
cin >> inpval;
}
Method 1: Use getchar(), Calculate/Convert Roman to integer until you encounter a space ' ',when you get a space ' ' output the integer and do the same next roman number until you get another space ' ' or newline '\n' and stop the program once you encounter newline '\n'.
Method 2:Use type std::string and take input with getline. Then iterate through the string and calculate until you find space ' ' output the number, do the same till you find next space ' ' or end when string ends.
If you know # of Roman numbers you want to convert you can put it in a loop.
Hope this helps.
Example(Method 2)
#include <bits/stdc++.h>
int value(char r)
{
if (r == 'I')
return 1;
if (r == 'V')
return 5;
if (r == 'X')
return 10;
if (r == 'L')
return 50;
if (r == 'C')
return 100;
if (r == 'D')
return 500;
if (r == 'M')
return 1000;
return -1;
}
int main()
{
int out=0;
std::string s;
std::string::iterator i; //string iterator
//for more info go to https://www.geeksforgeeks.org/stdstring-class-in-c/
getline(std::cin,s);
for (i = s.begin(); i!= s.end() ; ++i)
{
if(*i != ' ')//Encounter a space, output result and
{ //go to next roman numeral
int s1 = value(*i);
if (*(i+1) != ' ' || *(i+1) != '\0')
{
// Getting value of i+1 nth Element
int s2 = value(*(i+1));
// Comparing both values
if (s1 >= s2)
{
// Value of current symbol is greater
// or equal to the next symbol
out = out + s1;
}
else
{
out = out + s2 - s1;
i++; // Value of current symbol is
// less than the next symbol
}
}
else
{
out = out + s1;
i++;
}
}
else
{
std::cout<<out<<" ";
out = 0;
}
}
std::cout<<out<<" ";
std::cout<<std::endl;
return 0;
}
Input:
I II MM MCMIV
Output:
1 2 2000 1904

Pig latin conversion using Cstrings

The program takes in a word given by the user and translates that to pig latin. I've gotten everything to work almost perfectly, but have run into two bugs. The first of which is when translating words that begin with consonants say "count", the output is "ounttcay" instead of "ountcay". The second bug is that when for three letter words like "egg" or "not" the output is "egg_\377ay" or "ottn\377ay". Is there a simple way to remove that duplicate character and get rid of those numbers?
Note - Unfortunately it has to be done using a Cstring
#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
int convertToPigLatin(char arr[50]);
bool isVowel(char ch);
int main() {
char userInput[50];
char answer = ' ';
do {
cout << "Enter a word to convert it to pig latin" << endl;
cin.getline(userInput, 50); //get user input
cout << "Your entered word is " << userInput << endl;
convertToPigLatin(userInput); //translate user's input into piglatin
cout << "Would you like to convert another word?" << endl;
cin >> answer;
cin.ignore(); //clear past user input
cin.clear();
} while (answer == 'Y' || answer == 'y');
return 0;
}
bool isVowel (char ch) {
switch (tolower(ch)) { //if the first character of the given input is a vowel
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
return true;
default:
return false;
}
}
int convertToPigLatin(char arr[50]) {
char newArr[50];
// string conjunctions[6] = {"and","but","for","nor","yet","the"}; //list of conjunctions not to be converted
size_t arrLength = strlen(arr); //holds length of input
for (int i = 0; i < arrLength; i++) { //make sure all characters in input are lower case for easier processing
newArr[i] = tolower(arr[i]);
}
char lastChar = newArr[0]; //save the first character in case it needs to be appended
if (atoi(arr) || arr[0] == '\0') { //if the input contains a number or begins with a null character print an error
cout << "Cannot translate inputs that contain numbers" << endl;
return -1;
} else if (arrLength <= 2) { // if the input is 2 or less characters
cout << newArr << endl; //print the input as is
cout << "Boring! Try somthing more than 2 characters long" << endl;
return 0;
} else if ((strstr(newArr, "and") && arrLength == 3) || (arrLength == 3 && strstr(newArr, "but")) || (arrLength == 3 && strstr(newArr, "for")) || (arrLength == 3 && strstr(newArr, "nor")) || (arrLength == 3 && strstr(newArr, "yet")) || (arrLength == 3 && strstr(newArr, "the"))) { //if the input is more than 2 characters long
cout << newArr << endl; //print the input as is
cout << "No conjucntions try again!" << endl;
return 0;
} else { //if the given input is three characters and is not a conjunction, being translation
if (isVowel(arr[0])) { //check if input's first character is a vowel
cout << "Your word in piglatin is "<< strcat(newArr, "ay") << endl; //print that string with 'ay' at the end (i.e. egg'ay')
return 0;
} else { //else if the given input starts with a consonant
for (int r = 1; r < arrLength; r++) {
newArr[r-1] = newArr[r];
newArr[arrLength] = lastChar;
}
cout << "Your word in piglatin is " << strcat(newArr, "ay") << endl;
return 0;
}
}
return 0;
}
You're not terminating newArr, and the last index of the input string is arrLength - 1.
int convertToPigLatin(char arr[50]) {
// Make sure newArr is properly terminated.
char newArr[50] = {0};
// [...]
} else { //else if the given input starts with a consonant
for (int r = 1; r < arrLength; r++) {
newArr[r-1] = newArr[r];
}
// Do this outside the loop.
newArr[arrLength-1] = lastChar;
// No need for strcat here.
cout << "Your word in piglatin is " << newArr << "ay" << endl;
}
}
return 0;
}
You need to add the '\0' at the end of newArr because strlen does not count it so you are not copying it. strcat replaces '\0' witn 'ay\0' but you have no '\0'.
for (int r = 1; r < arrLength; r++) {
newArr[r-1] = newArr[r];
newArr[arrLength] = lastChar;
}
newArr[arrLength+1] = '\0';
cout << "Your word in piglatin is " << strcat(newArr, "ay") << endl;

English to Morse code program

#include <iostream>
#include <iomanip>
#include <conio.h>
#include <stdlib.h>
#include <string>
using namespace std;
int main()
{
string text[39] = {"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",
"1","2","3","4","5","6","7","8","9","0","Stop",",","?"};
string code[39] = {".-","-...","-.-.","-..",".","..-","--.","....","..",".---","-.-",".-..","--",
"-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..",
".----","..---","...--","....-",".....","-....","--....","---..","----.","-----",".-.-.-","--..--","..--.."};
string English, Morse, output_string;
int option, string_size = 0, location;
char again = 'y', letter;
while(again == 'y')
{
system("cls");
cout << "1 - Encode(Text to Morse)\n";
cout << "2 - Decode(Morse Code to Text)\n";
cout << "3 - Display the Morse Code\n";
cout << "4 - Quit\n";
cout << "Enter 1,2,3 or 4:";
cin >> option;
cin.ignore(256,'\n');
system("cls");
switch(option)
{
case 1:
cout << "\nEnter a string with multiple words to encode:";
getline(cin, English);
system("cls");
cout << "\nThe target string to be translated is:" << "\n";
cout << English << "\n";
string_size = English.length();
for(int n = 0; n <= string_size-1; n++)
{
letter = (char)English.at(n);
if(letter != ' ')
{
for(int t = 0; t <=39; t++)
{
if(letter == text[t])
{
cout << code[t] << " ";
break;
}
}
}
else if(letter == ' ')
{
cout << "\n";
}
}
getch();
break;
}
}
}
I didn't finish it yet, but I don't know why I can't run if(letter == text[t]), it says it's an error. how can I fix it? And I have no idea to write the code that Morse to English. how can I know the position of the array that the user entered?
Error message:
error: no match for 'operator==' (operand types are 'char' and 'std::string {aka std::basic_string}')|
You are trying to compare between strings and char.
You need to write the array like that (if you want to use just characters):
char text[39] = {'A','B','C','D','E','F','G','H','I','J','K','L','M'};
and not:
string text[39] = {"A","B","C","D","E","F","G","H","I","J","K","L","M"};
for (int t = 0; t <= 39; t++)
You have 39 items starting at zero index, therefore your loop should go up to (but not including) 39
for (int t = 0; t < 39; t++)
{
...
}
You can declare a temporary string to copy each letter to string. You would also need to make sure text is upper case:
letter = (char)English.at(n);
if (letter != ' ')
{
for (int t = 0; t < 39; t++)
{
std::string temp;
temp = toupper(letter);
if (temp == text[t])
{
cout << code[t] << " ";
break;
}
}
}
If you want the array to be string - then use strcmp() function.
if(strcmp(text[t],letter)==0)
{
cout << code[t] << " ";
break;
}
Have a good luck!

How to avoid using Backspace character with push_back?

I'm making a software as an ATM, so when the user try to enter the password the user only sees *******, but when trying to delete it doesn't delete a character. It just adds a new one. Here's my code:
string password2 = "";
cout << "PASSWORD: ";
ch = _getch();
while(ch != 13) //character 13 is enter
{
password2.push_back(ch);
cout << '*';
ch = _getch();
}
And also, I try to use pop_back(); but it doesn't work either. Can anybody help me please?
Just did a little magic and created this:
I must admit, I don't like the style... But it "works" !
#include<iostream>
#include<string>
#include <conio.h>
bool verify_pass(const std::string& pass)
{
std::string input = "";
char ch = '0';
while(true)
{
ch = getch();
if(ch == '\b')
{
if(input.size() > 0)
{
input.erase(input.begin() + input.size() - 1); // erase last char
std::cout << "\b \b";
}
}
else if(ch != '\r\n' && ch != '\n' && ch != '\r')
{
input += ch;
std::cout << '*';
}
else
break;
}
return input == pass;
}
int main()
{
std::string insecurePass = "1234";
std::cout << "Enter Password: ";
if(verify_pass(insecurePass))
std::cout << "\nCorrect!\n";
else
std::cout << "\nFalse!\n";
}
By the way, you can use the vector like I used the string, but use push_back instead of += and pop_back() should work too instead of my method with erase.