Right now I have two completed projects, and the first one works perfectly, but is there a way to justify why a string with value 0 converted to an array becomes 48? First project is called "Binary to Decimal" and I have to create a documentation on it, but i have no clue how to answer the question of 0 becoming 48. The project is basically create a program that converts binary base two to a positive integer value.
#include <string>
#include <iostream>
usingnamespace std;
int main(){
string binary;
cout << "Welcome to the \"Binary --> Decimal\" program." << endl;
cout << "This program runs on a binary base two system." << endl;
cout << "Please only use a maximum of 12 digits and only enter \"0\" and \"1\" only." << endl;
cout << "Enter in \"00\" to shut down the program permanently." << endl;
while(true){
int total = 0, counter, code[12];
float adder = 0.5;
cout << "Please enter your binary number: ";
cin >> binary;
for(counter = 0; counter < 12; counter++){
code[counter] = binary[counter];
}
for(counter = binary.size(); counter > -1; counter--){
if(code[counter] == 49){
total += adder;
}
adder *= 2;
}
if((code[0] == 48) && (code[1] == 48)){
cout << "Thank you for using this program." << endl;
break;
}
else{
cout << "The decimal representation of the binary number is: ";
cout << total << "." << endl << endl;
}
}
return 0;
}
Thank you Chervil for solving my problem. It was ASCII code all along since number 48 was just the symbol for zero. I still have another big issue with my second project though. This program seems to crash when I enter in something like "I'm not an idiot". The problem is somehow related to basic string::erase, but I don't know how to fix it.
Sorry, I tried to debug your code, but I couldn't figure out a way to keep the original coding. This is the functional version I came up with, it has a nested while loop that turns every letter in the string to lower-case then checks for the full word "idiot", it will now detect "Idiot" "idiot's" "IdIoT", etc.
I actually edited that post right after making it, but I stand by what I said in it, the final if() in the code is testing for the values of find2 through find5 without assigning them a value, so they should be initialized to 0.
That doesn't solve the main problem, but assigning the value 0 in the beginning will at least get that final if() to run.
But as I said early there is no any need in this set of variables and there is no any need to erase the string. It is enough to advance previously found position and start searching from this position.
Mushymon, this is the closest I could get to your original code, Like Vlad says, you don't need the string.erase() method. I think that it somehow ends up trying to access a member of the array that is beyond the length of the array, but I didn't find exactly where or how.
@ Vlad, try this without the unsigned vars being initialized with a value and try typing in a short word, 5 letters or less.
@newbieg
@ Vlad, try this without the unsigned vars being initialized with a value and try typing in a short word, 5 letters or less.
I am sorry but I have no any desire to spend much time in this thread. It is the second thread that is devoted to this question. The task is being done very simply by means of standard algorithm std::all_of and I wrote the corresponding code for myself when the first thread was discussed.:)
But even without the algorithm it is simply being done if to use another form of class string member function find (or find_first_of) with the second parameter that specifies starting position:)
Thank you so much Newbieg, I learned a lot about the find function in strings after looking at your improved code. I did not know that you could just make string.find_first_of() continue where something else left off, so that is why I was using to prevent it from repeating itself. Thank you for your consideration too Vlad.
using System;
namespace Idiot
{
/// <summary>
/// Program Idiot. Copyright 2013, Vladimir Grigoriev (Vlad from Moscow)
/// ====================================================================
/// Determines whether a given word can be present in a sentence
/// </summary>
class Program
{
staticvoid Run()
{
while (true)
{
Console.Write("Enter a sentence (<enter> to quit): ");
// "i don't like to eat"
string s = Console.ReadLine();
if (s.Trim().Length == 0) break;
const string idiot = "idiot";
int pos = 0;
foreach (char c in idiot)
{
char[] letter = { c, char.ToUpper(c) };
pos = s.IndexOfAny( letter, pos);
if (pos == -1) break;
++pos;
}
Console.WriteLine("There is {0}word \"{1}\" in the sentence\n",
pos == -1 ? "no " : "", idiot);
}
}
staticvoid Main()
{
Program.Run();
}
}
}