Oct 9, 2010 at 3:42pm UTC
I think all the problem is on the menu and on the base identification.
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
// Program to convert a decimal number in binary, octal or hexadecimal one.
#include <iostream>
#include <stdlib.h>
#include <conio.h>
#define MAX 300
using namespace std;
int main () {
int decimal, quoc, opcao, base, ind = 0;
int resto[MAX];
cout << "Programa para conversao de numeros decimais.\n\n"
<< "(1)Decimal para binario.\n"
<< "(2)Decimal para octal.\n"
<< "(3)Decimal para hexadecimal.\n\n"
<< "Opcao (1, 2 ou 3): " ;
cin >> opcao;
while (opcao != 1 && opcao != 2 && opcao != 3)
switch (opcao) {
case 1:
base = 2;
break ;
case 2:
base = 8;
break ;
case 3:
base = 16;
break ;
default :
cout << "Opcao invalida! Tente novamente.\n\n"
<< "Opcao (1, 2 ou 3): " ;
cin >> opcao;
break ;
}
system ("cls" );
cout << "Entre com o numero decimal: " ;
cin >> decimal;
quoc = decimal;
while (quoc != 0) {
resto[ind] = quoc % base;
ind++;
quoc /= base;
}
cout << "\n" << decimal << " equivale a " ;
while (ind > 0) {
if (opcao == 3) {
if (resto[ind - 1] < 10)
cout << resto[ind - 1];
switch (resto[ind - 1]) {
case 10:
cout << "A" ;
break ;
case 11:
cout << "B" ;
break ;
case 12:
cout << "C" ;
break ;
case 13:
cout << "D" ;
break ;
case 14:
cout << "E" ;
break ;
case 15:
cout << "F" ;
break ;
}
}
else
cout << resto[ind - 1];
ind--;
}
if (opcao == 1)
cout << " em Binario." ;
else if (opcao == 2)
cout << " em Octal." ;
else
cout << " em Hexadecimal." ;
getch();
return 0;
}
Last edited on Oct 9, 2010 at 8:56pm UTC
Oct 9, 2010 at 8:02pm UTC
In line 22 you're saying that it should execute this loop only if the number isn't 1,2 or 3. Then in the switch statement you say that if the number is 1,2 or 3 it should do ...
So basically what you'd want to do is: if (opcao==1|| opcao==2 ||opcao==3)
that should work ;)