This question already has answers here:
Convert single char to int
(3 answers)
Closed 2 years ago.
Struggling to separate a string into a stack containing 1's and 0's. Currently I am trying to iterate through the string and parse them into integers to add to a stack.
I am entering 1010, in which the result is 1010 , 010, 10, 0 instead of the desired stack being 1, 0, 1, 0
I have used atoi and stoi along with indexing and the .at method to where I still have the same issue.
#include <iostream>
#include <string>
#include <stack>
using namespace std;
bool isParsableInt(string input) {
string nums = "1234567890";
string test = input;
int attempt;
try {
if (test == "") { return false; }
if (test[0] == '-') {
test = test.substr(1, test.length() - 1);
}
for (int i = 0; i < test.length(); i++) {
if (nums.find(test[i]) == string::npos) { return false; }
attempt = atoi(&test[i]); // String to integer
}
return true;
}
catch (...) { // Catches any error thrown
return false;
}
}
stack<int> createBinaryStack() {
string input;
stack<int> result = stack<int>();
while (true){
cout << "Enter a binary number : ";
cin >> input;
if (!isParsableInt(input)) {
cout << "Invalid Input found - Must be 1's & 0's" << endl;
continue;
}
if (count(input, '0') + count(input, '1') != input.length()) {
cout << "Invalid Number found - Must be 1's & 0's" << endl;
continue;
}
for (int i = 0; i < input.length(); i++) {
cout << stoi(&input.at(i)) << "\t"; // Issue on atoi and stoi functions do not seem to work
result.push(stoi(&input.at(i)));
}
cout << endl;
return result;
}
}
int binaryStackToDecimal(stack<int> stk){
int count = stk.size();
int total = 0;
for (int i = 0; i < count; i++) {
if (stk.top() != 1 && stk.top() != 0) {
return -1;
}
total += stk.top() * pow(2, i);
stk.pop();
}
return total;
}
int main(){
stack<int> stk = createBinaryStack();
while (!stk.empty()) {
cout << stk.top();
stk.pop();
}
cout << endl;
cout << binaryStackToDecimal(stk);
}
stoi(&input.at(i))
should be
input.at(i) - '0'
or, since you are only dealing with zero and one the even simpler
input.at(i) == '1'
also works (as do many other variations).
Your mistake was taking functions that are intended to convert a sequence of digits to a number (stoi and atoi) when all you wanted to do is convert a single digit.
Related
I am trying to determine if a string is a palindrome by reversing the string and then comparing the letters of both strings. The code is provided underneath. So far whatever I put I always get "is a palindrome " as an output. I am aware of the short cut method for doing this easily and efficiently but trying to understand the long way as well. I am using C++ 11
#include <iostream>
#include <string>
using namespace std;
string reversed = " ";
void reverse_sentence(string s)
{
for (int i = 0; i <= s.length(); i++)
{
reversed = s[i] + reversed;
}
cout << reversed;
}
void is_pal(string reversed, string s)
{
int flag = 0;
// if(s == string(s.rbegin(), s.rend())){
// cout << "is a palindrome"<<endl;
// }else{
// cout <<"failed"<<endl;
// }
for (int i = 0; i <= s.length(); i++)
{
for (int j = 0; j <= reversed.length(); j++)
{
if (s[i] != reversed[j - 1])
{
flag = 1;
}
}
}
if (flag == 1)
{
cout << "is palindrome" << endl;
}
else
{
cout << "not palindrome" << endl;
}
}
int main()
{
string s = "hello";
reverse_sentence(s);
is_pal(s, reversed);
return 0;
}
First issue...
your for loops go out of bounds... length() gives you number of characters in the string. Those characters have index 0 through length() - 1. Because you are using <= s.length() however, then the last time through you get erroneous data, if it doesn't give you out of bounds error. For loops using length() should be from i = 0 to i < s.length()
Biggest issue...
void is_pal(string reversed, string s)
{
int flag = 0;
for (int i = 0; i <= s.length(); i++)
{
for (int j = 0; j <= reversed.length(); j++)
{
if (s[i] != reversed[j - 1])
{
flag = 1;
}
}
}
if (flag == 1)
{
cout << "is palindrome" << endl;
}
else
{
cout << "not palindrome" << endl;
}
}
Your j loop is inside of your i loop.. which means that s[i] never changes as you compare it to every single value in reversed... unless every single character in reversed is the same character... then one of them is guaranteed to be not equal to s[i] which means that flag will be set to 1... which means your function reports it to be a palindrome...
Not only that.. but you already went through the process of doing the reverse... why are you trying to compare against reverse.. in reverse order? That would be the same as comparing against the original string in normal order...
Assuming your reverse_sentence function works correctly... then s.length() is the same as reversed.length(). So to see if s is the same forwards as it is backwards then the letter at each index of s should be exactly the same as the letter at the same index in reversed. Also what you are looking for is when they don't match... so you should initialize your flag to assume at the beginning that they are going to be matching and only set it to false when you discover that they don't match...
void is_pal(string reversed, string s)
{
bool flag = true;
// run loop through length of s or until we know s doesn't match reversed
for (int i = 0; i < s.length() && flag; i++)
{
if (s[i] != reversed[i])
{
flag = false;
}
}
if (flag)
{
cout << "is palindrome" << endl;
}
else
{
cout << "not palindrome" << endl;
}
}
This question already has answers here:
How can I print a list of elements separated by commas?
(34 answers)
Closed 6 years ago.
This is when going through a list of integers in order seperated by commas and I only print one instance of an integer even where there are more than one seperated by commas. (CodeEval challenge https://www.codeeval.com/open_challenges/29/)
My problem is I am trying to do this in linear time without any external storage. And I can't have a comma at the end (e.g. 1,3,4,6,). The solutions I found online all use some list to store the integers and then they do a print.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string str = "1,2,2,3,3,3,4,4";
char c;
int num = -1;
for (int i = 0; i < str.length(); ++i) {
if (str[i] == ',') continue;
else {
c = str[i];
if ((c - '0') != num) {
num = c - '0';
cout << num << ",";
}
}
}
cout << endl;
return 0;
}
One of the solution is to use boolean flag:
bool first = true;
for( ... ) {
if( first ) first = false;
else std::cout << ',';
std::cout << data;
}
if (i == str.length() - 1)
{
cout << num;
}
else
{
count << num << ",";
}
Or you could print a backspace at the end of the string processing:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string str = "1,2,2,3,3,3,4,4";
char c;
int num = -1;
for (int i = 0; i < str.length(); ++i) {
if (str[i] == ',') continue;
else {
c = str[i];
if ((c - '0') != num) {
num = c - '0';
cout << num << ",";
}
}
}
cout << '\b';
cout << endl;
return 0;
}
This question already has answers here:
Convert a String In C++ To Upper Case
(31 answers)
Closed 7 years ago.
I am writing a program that can count the number of times a word is found in a external file. The words that are to be searched for are also in an external file but I am able to retrieve those just fine. I realised that it will only update the value of count if the word exactly matches. So for example if I was searching for the word "School" and the word "school" was in the textfile I don't think the value of count would be changed. I also think that count wouldn't be changed if the word to be search for was "SCHOOL" and the word in the textfile was "school". So how do I edit my if statements so that for example the word "school" would match "SCHOOL" AND "School"?
This is my main function:
#include <iostream>
#include "ReadWords.h"
#include "Writer.h"
#include <cctype>
#include <string>
using namespace std;
int main() {
int x = 9;
int count = 0;
int count0;
int count1;
int count2;
int count3;
int count4;
int count5;
int count6;
int count7;
int count8;
int count9;
int scount;
const int size = 10;
string word_search[size];
string word;
cout << "Please enter a filename: " << flush;
char filename[30];
cin >> filename;
ReadWords reader(filename);
while (reader.isNextWord()){
count = count + 1;
reader.getNextWord();
}
cout << "There are: " << count << " words in the play" << endl;
cout << "Please enter the name of the file with the search words: " << flush;
char filename1[30];
cin >> filename1;
ReadWords reader1(filename1);
scount = 0;
while (reader1.isNextWord()) {
word_search[scount] = reader1.getNextWord();
++scount;
}
cout << "" << endl;
while (reader.isNextWord()) {
This is where I attempted to convert the input to upper case to see if the word matches the uppercase version of itself but this didn't work. Here I also need to check if the word matches itself if the first letter is capital?
if (reader.getNextWord() == word_search[0] || toupper(reader.getNextWord()) == word_search[0]) {
count0 = count0 + 1;
}
if (reader.getNextWord() == word_search[1]) {
count1 = count1 + 1;
}
if (reader.getNextWord() == word_search[2]) {
count2 = count2 + 1;
}
if (reader.getNextWord() == word_search[3]) {
count3 = count3 + 1;
}
if (reader.getNextWord() == word_search[4]) {
count4 = count4 + 1;
}
if (reader.getNextWord() == word_search[5]) {
count5 = count5 + 1;
}
if (reader.getNextWord() == word_search[6]) {
count6 = count6 + 1;
}
if (reader.getNextWord() == word_search[7]) {
count7 = count7 + 1;
}
if (reader.getNextWord() == word_search[8]) {
count8 = count8 + 1;
}
if (reader.getNextWord() == word_search[9]) {
count9 = count9 + 1;
}
}
cout << "Please enter the name of the file to write to: " << flush;
char filename2[30];
cin >> filename2;
Writer reader2(filename2);
cout << "File has been written too.." << endl;
reader2.writeInt(count);
reader2.writeString("Hello my name is Joshua Ogunnote");
return 0;
}
This is a separate file where some of my functions are declared:
#include "ReadWords.h"
#include <cstring>
#include <iostream>
using namespace std;
void ReadWords::close(){
wordfile.close();
}
ReadWords::ReadWords(const char *filename) {
wordfile.open(filename);
if (!wordfile) {
cout << "could not open " << filename << endl;
exit(1);
}
}
string ReadWords::getNextWord() {
string n;
if(isNextWord()){
wordfile >> n;
int len = n.length();
for(int i = 0; i < len ; i++) {
if (ispunct(n[i]))
{
n.erase(i--, 1);
len = n.length();
}
}
cout << n << endl;
return n;
}
}
bool ReadWords::isNextWord() {
if (wordfile.eof()) {
return false;
}
return true;
}
If you're just using English, a simple tolower() transform will do.
std::string tolower( std::string s )
{
for (char& c : s) c = std::tolower( c );
return s;
}
Now you can compare them:
if (tolower( "Hello" ) == tolower( "HELLO" ))
If you are working with Unicode, you should perform a conversion called case folding on the text, and compare the resulting string data.
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
const int MAX_NUMS = 200; // Constant for the maximum number of words.
const int MAX_GUESSES = 8;
const string LETTERS = "abcdefghijklmnopqrstuvwxyz";
char inputLetter();
int findChar(char letter, string&word);
string getGuessedWord(string&secretWord, string&lettersGuessed);
string getLettersGuessed(char letter, string&lettersGuessed, int n);
void display(string&lettersGuessed, string&wordGuessed, int num, int pos);
bool isDone(string wordGuessed);
int main()
{
string oneWord; // holds one word from input file
string secretWord; // holds secret word to be guessed
string words[MAX_NUMS]; // holds list of words from input file
int randomValue; // holds index of secret word
int count = 0; // holds number of words in the file
// Declare an ifstream object named myFile and open an input file
ifstream myFile;
myFile.open("P4Words.txt");
// Exit program if cannot open file for input
if (!myFile)
{
cout << "Error: Unable to open file for input" << endl;
return 0;
}
// Input words from a file into words array
// Add your code here ...
myFile >> oneWord;
while (!myFile.eof())
{
words[count] = oneWord;
count++;
myFile >> oneWord;
}
myFile.close();
cout << count << " words loaded." << endl;
srand(static_cast<unsigned int>(time(0)));
// Select a secret word
// Add your code here ...
secretWord = words[rand() % (count + 1) ];
// Possible useful variables the loop
string lettersGuessed = ""; // holds letters guessed so far
string wordGuessed; // holds current word guessed like �_ pp_ e�
int incorrectGuesses = 0; // holds number of incorrect guesses so far
char letter; // holds a guessed letter
bool done = false; // have not guessed the word yet
int num = 8; int pos;
cout << "Welcome to the game, Hangman V1 by Your Name!" << endl;
cout << "I am thinking of a word that is " << secretWord.length()
<< " letters long." << endl;
// Set up a loop to input guesses and process
// Add your code here ...
do
{
letter = inputLetter();
pos = findChar(letter, secretWord);
wordGuessed = letter;
lettersGuessed = getLettersGuessed(letter, lettersGuessed, 8 - num);
wordGuessed = getGuessedWord(secretWord, lettersGuessed);
display(lettersGuessed, wordGuessed, num, pos);
done = isDone(wordGuessed);
num--;
} while ((num > 1) && (done == false));
// Check for won or lost
// Add your code here ...
if (done == false)
{
cout << "sorry you lose..." << endl;
}
if (done == true)
{
cout << "congratulations! " << endl;
}
system("pause"); // stop program from closing, Windows OS only
return 0;
}
// Add function definitions here ...
char inputLetter()
{
int i;
char letter;
do
{
cout << "please guess a letter: " << endl;
cin >> letter;
for (i = 0; i < 25; i++)
{
if (letter == LETTERS[i])
{
return letter;
}
}
if (letter != LETTERS[i])
{
cout << "Oops! That is an invalid character." << endl;
}
} while (letter != LETTERS[i]);
}
int findChar(char letter, string &word)
{
int i = 0; int pos = 0; bool found = false;
do
{
if (word[pos] == letter)
{
return pos;
found = true;
}
pos++;
} while (pos<word.length() - 1);
if (found == false)
{
return -1;
}
}
string getGuessedWord(string&secretWord, string&letterGuessed)
{
string temp;
temp = secretWord;
for (size_t k = 0; k <= temp.length() - 1; k++)
{
temp[k] = '_';
}
for (size_t i = 0; i <= temp.length() - 1; i++)
{
for (size_t j = 0; j <= temp.length() - 1; j++)
{
if (letterGuessed[i] == secretWord[j])
{
temp[j] = letterGuessed[i];
}
}
}
return temp;
}
string getLettersGuessed(char letter, string&lettersGuessed, int n)
{
string temp;
temp = letter;
lettersGuessed.insert(n, temp);
return lettersGuessed;
}
void display(string&lettersGuessed, string&wordGuessed, int num, int pos)
{
if (pos != -1)
{
cout << "You have " << num << " guesses left." << endl;
cout << "Letters guessed so far: " << lettersGuessed << endl;
cout << "Good guess!: " << wordGuessed << endl;
cout << "-----------------------------------------------------" << endl;
}
if (pos == -1)
{
cout << "You have " << num << " guesses left." << endl;
cout << "Letters guessed so far: " << lettersGuessed << endl;
cout << "Oops! that letter is not my word: " << wordGuessed << endl;
cout << "-----------------------------------------------------" << endl;
}
}
bool isDone(string wordGuessed)
{
bool done = false; int k = 0;
for (size_t i = 0; i <= wordGuessed.length() - 1; i++)
{
if (wordGuessed[i] == '_')
{
k++;
}
}
if (k == 0)
{
done = true;
}
return done;
}
it says subscript is out of range I need help to fix it
let me know how to fix it please its a project that is due very soon
that's all I got so far
Change:
for (size_t i = 0; i <= temp.length() - 1; i++)
to:
for (size_t i = 0; i <= letterGuessed.length() - 1; i++)
I'm making a program that converts a string that the user enters such as "APPLE" into a binary number through the corresponding ASCII numbers that represent each character of the string "APPLE." For example A = 65 in ascii etc.. I've created a function that converts the string into a binary but it doesn't seem to be working. It displays "The equivalent binary number is: 0031F240for A" in an infinite loop and gives me "0031F240for" instead of being in the binary version of 65. I know this function works for converting a decimal number into binary because I've tried it, but I think my implementation of the bin[] array is messing things up. Any help would be appreciated.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <fstream>
using namespace std;
class RandomString
{
private:
string input;
string bin[100];
public:
RandomString() : bin(), input("")
{
}
void getData()
{
cout << "Enter the word to be encoded into a binary file.";
cin >> input;
}
void numToBin()
{
int i = 0;
int len = input.length();
int num = int(input[i]);
for(int i = 0; i < len; i++)
{
while(num != 0)
{
if (num % 2 == 0)
bin[i].insert(0, "0");
else
bin[i].insert(0, "1");
num = num / 2;
cout << "The equivalent binary number is: " << bin << "for " << input[i] << endl;
}
}
}
void display()
{
}
};
I haven't test if the result is correct but this code convert a string to binary. Probably you have to modify it to fit with ASCII codes.
void DecimalToBinary(char a,std::vector<char>& v)
{
if(a==0)
v.push_back(0);
if(a==1)
v.push_back(1);
else
{
v.push_back(a%2);
DecimalToBinary(a/2,v);
}
}
int main()
{
std::vector<char> v;
std::string line;
getline(std::cin,line);
std::istringstream input(line);
char c;
while(input >> c)
{
DecimalToBinary(c,v);
}
std::copy(v.begin(),v.end(),std::ostream_iterator<int>(std::cout,""));
}
First Your while loop never stops because you don't change the value of i inside the while loop, so int(input[i]) has always the same value, you have to use break somewhere or i++, but I don't know if the result is correct,I think recursion is better than while in this situation, but anyway try the following:
void numToBin()
{
int i = 0;
int len = input.length();
int num = int(input[i]);
for(int i = 0; i < len; i++)
{
while(int(input[i]) != 0)
{
if (num % 2 == 0)
{
bin[i].insert(0, "0");
break;
}
else
{
bin[i].insert(0, "1");
num = num / 2;
}
cout << "The equivalent binary number is: " << bin << "for " << input[i] << endl;
}
}
}
Second, doing std::cout << bin you print a memory address, not the contents of the bin.
while(int(input[i]) != 0)
{
if (num % 2 == 0)
bin[i].insert(0, "0");
else
{
bin[i].insert(0, "1");
}
num = num / 2;// this line should be in both case.
cout << "The equivalent binary number is: " << bin << "for " << input[i] << endl;
}
I've changed num = num / 2 for both cases. Please check it.
You may want to change the 'bin' in
cout << "The equivalent binary number is: " << bin
to 'bin[i]'.
Because 'bin' is a string array, also the pointer/address to the string array, so 'cout << bin' will always output the address.