Nothing shows in the console window.

Hi Guys, I'm quite new to C++ and I'm trying to write a program that takes the number from the command line and converts it to a hexadecimal. There are no errors when i compile it but when i run it, the product of the conversion dosent show. In the command line i have "22222 b10"

I don't get why nothing appears in the console window.

Any help would be greatly appreciated!

My code is below.

----------------------------

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <errno.h>
#include <math.h>
using namespace std;

int main(int argc, char* argv[])
{

int base10 = atoi(argv[1]), temp;


if (strcmp(argv[2], "b2") == 0){
while (base10 != 0){
cout << base10 % 2;
base10 = base10 / 2;
}
}

else if (strcmp(argv[2], "b3") == 0){
while (base10 != 0){
cout << base10 % 3;
base10 = base10 / 3;
}
}

else if (strcmp(argv[2], "b4") == 0){
while (base10 != 0){
cout << base10 % 4;
base10 = base10 / 4;
}
}

else if (strcmp(argv[2], "b5") == 0){
while (base10 != 0){
cout << base10 % 5;
base10 = base10 / 5;
}
}

else if (strcmp(argv[2], "b6") == 0){
while (base10 != 0){
cout << base10 % 6;
base10 = base10 / 6;
}
}

else if (strcmp(argv[2], "b7 ") == 0){
while (base10 != 0){
cout << base10 % 7;
base10 = base10 / 7;
}
}

else if (strcmp(argv[2], "b8") == 0){
while (base10 != 0){
cout << base10 % 8;
base10 = base10 / 8;
}
}

else if (strcmp(argv[2], "b9") == 0){
while (base10 != 0){
cout << base10 % 9;
base10 = base10 / 9;
}
}

else if (strcmp(argv[2], "b10") == 0){
while (base10 != 0){
cout << base10 % 10;
base10 = base10 / 10;
}
}

else if (strcmp(argv[2], "b11") == 0){
while (base10 != 0){
temp = base10 % 11;
if (temp == 10) {
cout << "A";
}
else {
cout << temp;
}
base10 = base10 / 11;
}
}

else if (strcmp(argv[2], "b12") == 0){
while (base10 != 0){
temp = base10 % 12;
if (temp == 10){
cout << "A";
}
else if (temp == 11) {
cout << "B";
}
else {
cout << temp;
}
base10 = base10 / 12;
}
}

else if (strcmp(argv[2], "b13") == 0){
while (base10 != 0){
temp = base10 % 13;
if (temp == 10){
cout << "A";
}
else if (temp == 11){
cout << "B";
}
else if (temp == 12){
cout << "C";
}
else {
cout << temp;
}
base10 = base10 / 13;
}
}

else if (strcmp(argv[2], "b14") == 0){
while (base10 != 0){
temp = base10 % 14;
if (temp == 10){
cout << "A";
}
else if (temp == 11){
cout << "B";
}
else if (temp == 13){
cout << "C";
}
else if (temp == 13){
cout << "D";
}
else {
cout << temp;
}
base10 = base10 / 14;
}
}

else if (strcmp(argv[2], "b15") == 0){
while (base10 != 0){
temp = base10 % 15;
if (temp == 10){
cout << "A";
}
else if (temp == 11){
cout << "B";
}
else if (temp == 12){
cout << "C";
}
else if (temp == 13){
cout << "D";
}
else if (temp == 14){
cout << "E";
}
else {
cout << temp;
}
base10 / 15;
}
}

else if (strcmp(argv[2], "b16") == 0){
while (base10 != 0){
temp = base10 % 16;
if (temp == 10){
cout << "A";
}
else if (temp == 11){
cout << "B";
}
else if (temp == 12){
cout << "C";
}
else if (temp == 13){
cout << "D";
}
else if (temp == 14){
cout << "E";
}
else if (temp == 15){
cout << "F";
}
else {
cout << temp;
}
base10 = base10 / 16;
}
}







system("pause");
return (0);
}[/code]
Disregard the "[/code]" at the bottom.
Hi George..
Please first write your code in proper format
Will do, if i knew what that meant. Please explain haha
Edit your first post, and place a [code] at the top of your code.
You probably need to copy-paste your code again, since spaces and tabbing aren't preserved.
Cant you still read it how it is now though?
I'll still redo my question in proper format now though
This int base10 = atoi(argv[1]), temp; make no sense. What is temp? Just remove it.
There should be an "edit" button in the bottom-right.
Anyways you have a lot of repeating code there.

You can get the base number as follows:
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
#include <iostream>
#include <cstdlib>
int main(int argc, char** argv)
{
    if((argc != 3) || (argv[2][0] != 'b'))
    {
        std::cout << "Invalid arguments." << std::endl;
        return 1;
    }
    int number = std::atoi(argv[1]);
    int base = std::atoi(argv[2]+1);
    // base will be the number after the 'b'
    if((base <= 1) || (base > 16))
    {
        std::cout << "Invalid number base" << std::endl;
        return 1;
    }
    else if(number < 0)
    {
        std::cout << "Number is negative" << std::endl;
        return 1;
    }
    else if((base == 10) || (number < base && base < 10) || (number < 10 && base > 10))
        std::cout << number << std::endl;
    else
        std::cout << "Please compute base " << base << " for " << number << std::endl;
    return 0;
}
Last edited on
Topic archived. No new replies allowed.