I have been trying to make this code work for some time now. I have had an issue geting a return from toBinary. I have all of the needed includes so do not worry about them. I am some what new to C++ and even to programing in general so some of this is above my understanding.

using namespace std;

int toBinary (string myString)
{
    int outValue = 0;
    //string myString = inData;
    for (std::size_t i = 0; i < myString.size(); ++i)
    {
        //cout << bitset<8>(myString.c_str()[i]) << endl;
        bitset<8>(myString.c_str()[i]) >> outValue;

        return outValue;
    }
}

int main ()
{
    cout << toBinary("432423") << endl;
}

Recommended Answers

All 5 Replies

What do you expect to be returned from toBinary? What is being returned? Do you get warnings when you compile? What are they?

As an aside, when I see:

I have all of the needed includes so do not worry about them.

followed by:

I am some what new ... so some of this is above my understanding.

I automatically think the problem is somewhere other than what is listed in the example. Maybe not applicable here, but in general, it is.

The point to toBinary is much like toString in say Lua. In short a string goes in and you get out interger binary which you can write to a file.
"As an aside, when I see:

I have all of the needed includes so do not worry about them.

followed by:

I am some what new ... so some of this is above my understanding.

I automatically think the problem is somewhere other than what is listed in the example. Maybe not applicable here, but in general, it is."
Thanks for this advice the full code is below.
To add to this there are no compiler errors however when data is passed into toBinary I get 0 without exception.

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <cstddef>
#include <vector>
#include <windows.h>
#include <mmsystem.h>
#include <map>
#include <stdio.h>
#include <bitset>
using namespace std;

int toBinary (string myString)
{
    int outValue = 0;
    //string myString = inData;
    for (std::size_t i = 0; i < myString.size(); ++i)
    {
        //cout << bitset<8>(myString.c_str()[i]) << endl;
        bitset<8>(myString.c_str()[i]) >> outValue;

        return outValue;
    }
}

int main ()
{
    cout << toBinary("432423") << endl;
        system("color 9");
}

I should at least put the return on line 24 outside of your for loop.

I went ahead and removed it from the loop. If no one has a solution then I will just keep trying to fix this myself.

Hope you did not remove the return completely.
You say toBinary is much like a ToString in Lua, then why does toBinary returns an int?
Reading suggestion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.