-1
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

void main() {
    int a, b;
    while (cin >> a) {
        switch (a) {
        case 2: {
            cin >> b;
            std::string s = std::to_string(b);
            int dec = std::stoi(s, nullptr, 2);
            cout << dec << endl;break;
        }
        case 8:
            cin >> b;
            cout << oct <<b<< endl; break;
        case 16:
            std::cin >> std::hex >> b;
            std::cout << b << std::endl;

        }
    }
}

that's my code.Every one of them works, but the hex one.When i use it once, then it's not working.

For example if i have an input: 
2 1111
16  F
8 1
it should have an output:
15
15
1

the first number is hex/bin/oct and the second is the number you give.

About the while(cin>>a), it's that way because it should be kinda of endless cycle, there will be a lot of inputs and yeah.I guess it doesn't work because of that statement, but i don't know how to fix it.

Pafo
  • 29
  • 4

1 Answers1

0

Your code works fine: http://ideone.com/tBkJ2x If you're getting an error it's on values other than your example inputs. If you provide a value that's presenting you a problem I'd be happy to look at it with you.

Incidentally, why not just use stoi for everything? For example:

string s;

while(cin >> a) {
    cin >> s;
    cout << stoi(s, nullptr, a) << endl;
}

Live Example

EDIT:

hex and oct are sticky: https://stackoverflow.com/a/1533222/2642059

Thus if you set this you must also unset it.

You'll need to change: cout << oct <<b<< endl to cout << oct << b << dec << endl And cin >> hex >> b to cin >> hex >> b >> dec In order to get your code to work. (Or preferably switch to my code above.)

When you were inputting to a after reading in a hex number the 16 was translated as a hex number, so it was interpreted as: 22 (in decimal.)

Community
  • 1
  • 1
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • @Pafo I've edited to explain the problem. I didn't see it until you typed that comment. Since it was helpful to me it might be helpful to others as well, rolling it into your question would probably make a better question. – Jonathan Mee Oct 31 '16 at 11:35
  • Hey man, thank you.I added dec on this code -> std::cin >> std::hex >> b>>dec; and it works now.How did exactly dec fixed the program? edit: actually when i input a lot of things it doesnt work right. edit 2: i added cout << oct << b << dec << endl and it works :D – Pafo Oct 31 '16 at 11:40
  • @Pafo You'll need to show me a specific example of inputting a lot of things that breaks functionality. (Again I would strongly suggest going to my code instead of the switch statement.) – Jonathan Mee Oct 31 '16 at 11:44
  • well now it works well, but i dont know how to do it with your code? – Pafo Oct 31 '16 at 11:48
  • @Pafo Click on the "Live Example" button, it has my code linked. Also if this solved your problem don't forget to accept. – Jonathan Mee Oct 31 '16 at 11:53
  • wow, i saw your code before i didnt think it was the whole of it.I didnt know there was something like that in c++.I write mainly of java and there was a function that you just enter the number and the base you want to convert it. i guess that's the same.Really thank you. – Pafo Oct 31 '16 at 12:01