C++ string converting

i am asked to write two c++ programs , the first asks the user to enter a character string that consists of numbers ranging from 0-255 separated by a single dash. the program should parse each integer and convert it to 8 bit binary in the form of character string. the second program is asking the same thing but converting to hexadecimal instead.
Hello jxo,

Welcome to the forum.

As keskiverto once said:
Please note that this is not a homework site. We won't do your homework for you. The purpose of homework is that you learn by doing. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.

We didn't see your attempts to solve this problem yourself and so we cannot correct mistakes you didn't made and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again.

Or say that you do not understand the instructions or how to figure them out.

I would start with the main function and set it up to get the user's input. Until you have something to work with there is not much point of the rest of the program yet.

It is best to work in small steps. Compile and run what you have until it is working the way you want. it is easier to add what you need than try to work with the whole program unless you start with a piece of paper and plan the program first.

As I will do start with the basics of just getting a string to work with.

Hope that helps,

Andy
Hello jxo,

As I started working on the program I found that using a string stream could be useful. If you know about string streams. If not I will have to take a different approach.

Andy
void main()
{

char str[100];
char *temp;
cout << " Enter a String that contains numbers from 0 to 255: ";
cin >> str;
temp = strtok(str, "-");
while (temp != NULL) {
cout << " The equivalent Binary Numbers are: " << endl;
cout << str << "=>"<< setw(8) << setfill('0')<< bitset<8>(temp);
temp = strtok(NULL, "-");
}
system("pause");
}



thats what i've reached so far , and no i don't really know about string streams.
thank you
void main()
That's just plain wrong. In C++, main returns an int; int main()

You're writing C code, and thinking in C.

You should be using C++ strings; not an array of char.

Hello jxo,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Repeater is correct there is parts of the program the are all wrong. I would say most is in the while loop.

You should read up on "bitset" because you are not using it correctly. http://www.cplusplus.com/reference/bitset/bitset/bitset/

I also fount that the "setw" and "setfill" are not needed if "bitset" is used correctly. Well "setw" may be OK.

This is what I did to fix what you have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <iomanip>
#include <limits>
#include <bitset>
#include <cctype>  // <--- std::stoi().

#pragma warning(disable : 4996)  // <--- I need this for old C code like strtok().

int main()
{
	char str[100]{ "10-20-30-254-255" };  // <--- Used for testing. Remove the quoted string for normal use Leave the {}s.
	char *temp{};

	//  Uncomment these lines for normal run.
	//std::cout << " Enter a String that contains numbers from 0 to 255 (10-20-30): ";
	//std::cin >> str;

	temp = strtok(str, "-");

	while (temp != NULL)
	{
		std::cout << "\n The equivalent Binary Numbers are: " << std::endl;
		std::cout << "   " << temp << " => " << std::bitset<8>(std::stoi(temp)) << std::endl;  // <--- Changed "str" to "temp". Removed what was not needed.
		temp = strtok(NULL, "-");
	}

	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue";
	std::cin.get();

        return 0;
}

At the beginning of main you should ALWAYS initialize your variables when defined.

On line 23 notice the changes to the "cout", some for appearance and some because you were using it wrong. "bitset" takes a number not a string.

The last lines of main before the return that you do not have are a replacement for "system("pause");" that it is best not to use. First it will limit your program to windows computers. Second it could leave the program vulnerable to hackers.

Hope that helps,

Andy
Following Andy's format, strtok comes in from <cstring>, and I guess the stoi comes with it. Printing hex to stream comes nicely built-in to the <iomanip> set of goodies -- just output std::hex to the stream before the number.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <cstring>
#include <iostream>
#include <iomanip>
#include <limits>
#include <bitset>

int main()
{
    char str[100]{ "10-20-30-254-255" };
    int n;
    char* temp = strtok(str, "-");
    std::cout << "The equivalent Binary and Hex values are: " << std::endl;
    while (temp)
    {
        n = std::stoi(temp);
        std::cout << "   " << temp << " => " << std::bitset<8>(n) << 
                     " => " << std::hex << n << std::endl;
        temp = strtok(NULL, "-");
    }

    return 0;
}


If you were to instead use streams to parse the input data, it's much easier on the stream to separate on a whitespace delimiter rather than a dash (e.g. "10 20 30 254 255"). After a bit of reading, I see it's possible to override the default delimiter with a bit of work, as per this answer https://stackoverflow.com/a/7304184/1831037 , which would allow for the nice-looking while( cin >> n ) { ... } . I suppose alternately you could try something like reading the initial integer, and then in a loop reading a pair of delimiter & integer, but it wouldn't be as pretty.
I have one suggested change to Andy's and icy's programs. Use a for loop instead of a while loop. For loops aren't just for counters. They can be used for all sorts of loops. I like them because they separate the loop code from the body code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cstring>
#include <iostream>
#include <iomanip>
#include <limits>
#include <bitset>

int main()
{
    char str[100]{ "10-20-30-254-255" };
    int n;
    std::cout << "The equivalent Binary and Hex values are: " << std::endl;
    for (char* temp = strtok(str, "-"); temp; temp = strtok(nullptr, "-"))
    {
        n = std::stoi(temp);
        std::cout << "   " << temp << " => " << std::bitset<8>(n) << 
                     " => " << std::hex << n << std::endl;
    }

    return 0;
}

Topic archived. No new replies allowed.