changing my switch statement

i have this code involving binary and hex, which i have written with the help of a few people. however my friend gave me this switch statement(which i have bolded below), and i was curious to know if there was a simpler way to write it, or make it shorter. it should be noted, i'm willing to learn new techniques to acomplich this.


#include <iostream>
#include <cstdlib>
#include <time.h>
#include <sys/time.h>
#include <errno.h> // not really needed, but keep it here, just in case.
#include <math.h> // keep it here, just in case.

using namespace std;

int num;
char* end;

string newbase(int base, int number)
{
char converter[] = "0123456789ABCDEF";
string temp = "";

for (number; number != 0;)
{
int digit = number % base;
number /= base;


temp.push_back(converter[digit]);

}
return temp;
}


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

if (argc == 1) // no parameters on command line, other than the name of the executing file.
{
cout << "3333435,s3333435@student.rmit.edu.au,Matthew_Merigan" << endl ;
system("pause");
return(0) ;
}

//--- START YOUR CODE HERE.


//Parameter Check
if (argc == 2 || argc > 3)
{
cout << "P" << endl;
system("pause");
return 0;
}

if (argc == 3)
{

//comma
string comma(argv[1]);

string::size_type i = comma.find(',', 0);
while (i != string::npos)
{
comma.erase(i,1);
i = comma.find(',', i); // this is so that you don't have to search the entire string again
}


//input
double num = strtod(comma.c_str(),&end);
if(*end != '\0')
{
cout << "X" << endl;
system("pause");
return 0;
}

//range 1
if(num > 1000000 || num < -1000000)
{
cout << "R" << endl;
system("pause");
return 0;
}
//decimal

double dec = (int)num;
if(dec != num)
{
cout << "X" << endl;
system("pause");
return 0;
}

//argv2 for switch
if (argv[2][0] != 'b' || strlen(argv[2]) > 3)
{
cout << "V" << endl;
system("pause");
return 0;
}

if (strlen(argv[2])== 3 && argv[2][1] != '1')
{
cout << "V" << endl;
system("pause");
return 0;
}

//range
if (num > 65535 || num <= 0)
{
cout << "R" << endl;
system("pause");
return 0;
}

int convert = (int)num;

//switch
switch(argv[2][1])
{
case '2': cout << newbase(2, convert) << endl;
break;
case '3': cout << newbase(3, convert) << endl;
break;
case '4': cout << newbase(4, convert) << endl;
break;
case '5': cout << newbase(5, convert) << endl;
break;
case '6': cout << newbase(6, convert) << endl;
break;
case '7': cout << newbase(7, convert) << endl;
break;
case '8': cout << newbase(8, convert) << endl;
break;
case '9': cout << newbase(9, convert) << endl;
break;

case '1':

switch(argv[2][2])
{
case '0': cout << newbase(10, convert) << endl;
break;
case '1': cout << newbase(11, convert) << endl;
break;
case '2': cout << newbase(12, convert) << endl;
break;
case '3': cout << newbase(13, convert) << endl;
break;
case '4': cout << newbase(14, convert) << endl;
break;
case '5': cout << newbase(15, convert) << endl;
break;
case '6': cout << newbase(16, convert) << endl;
break;

default: cout << "V-3" << endl; break;
}

break;

default: cout << "V-4" << endl; break;
}
system("pause");
return 0;

}
system("pause");
return (0);
}
There is a super short version to convert a number to a different base. Without delivering the code, I can tell you that:

a. The base-N logarithm of the number + 1 is the number of digits the number has when converted to base N.
b. The simplest algorithm is a recursive one.

That should get you going, I hope.
I’m sorry you’l have to forgive me, it’s my first semester doing computer coding, and I don’t quite understand where you are coming from. do you mean make something like an if statement, with the switch inside of it? something similar to this?

if(switch 13;i++)
{
//etc
}
Regarding your switch():

How about:

1
2
3
4
5
6
7
8
9
int base = atoi(argv[2]);
if(base > 1 && base < 17)
{
    cout << newbase(base, convert);
}
else
{
    // Error
}


And please use code tags.
Caligulaminus, how would that help, i mean to my understanding that code, would check to make sure base is between the right range, but it doesn't output the binary or hex, like the switch does. i also replaced it with the switch, but got no output.

it doesn't output

What do you think my cout << newbase(base, convert); does?
k, but i stil found that it didn't output the binary or hex output. this is what i did

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
[output]

#include <iostream>
#include <cstdlib>
#include <time.h>
#include <sys/time.h>
#include <errno.h> // not really needed, but keep it here, just in case.
#include <math.h> // keep it here, just in case.

using namespace std;

int     num;
char*       end;

string newbase(int base, int number)
{
    char converter[] = "0123456789ABCDEF";
    string temp = "";
   
    for (number; number != 0;)
    {   
           int digit = number % base;
           number /= base;
       

temp.push_back(converter[digit]);

    }
return temp;    
}


int main(int argc, char *argv[])
{
   
    if (argc == 1)  // no parameters on command line, other than the name of the executing file.
    {
           cout << "3333435,s3333435@student.rmit.edu.au,Matthew_Merigan" << endl ;
           system("pause");
    return(0) ;
    }  

//--- START YOUR CODE HERE.
   
   
//Parameter Check
    if (argc == 2 || argc > 3)
    {
           cout << "P" << endl;
           system("pause");
    return 0;    
    }

    if (argc == 3)
    {
   
//comma
string comma(argv[1]);

string::size_type i = comma.find(',', 0);
while (i != string::npos)
{
comma.erase(i,1);
i = comma.find(',', i); // this is so that you don't have to search the entire string again
}

       
//input   
       double num = strtod(comma.c_str(),&end);
       if(*end != '\0')
       {
               cout << "X" << endl;
               system("pause");
       return 0;
       }
        
//range 1
       if(num > 1000000 || num < -1000000)
       {
               cout << "R" << endl;
               system("pause");
       return 0;
       }
//decimal
       
       double dec = (int)num;
       if(dec != num)
       {
               cout << "X" << endl;
               system("pause");
       return 0;    
       }
       
//argv2 for switch
       if (argv[2][0] != 'b' || strlen(argv[2]) > 3)
       {
               cout << "V" << endl;
               system("pause");
       return 0;
       }

       if (strlen(argv[2])== 3 && argv[2][1] != '1')
       {
               cout << "V" << endl;
               system("pause");
       return 0;
       }

//range
       if (num > 65535 || num <= 0)
       {
               cout << "R" << endl;
               system("pause");
       return 0;
       }
       
       int convert = (int)num;
       
int base = atoi(argv[2]);
if(base > 1 && base < 17)
{
    cout << newbase(base, convert)<<endl;
}
else
{
    cout<<"M"<<endl;
}

    }
system("pause");
return (0);
}

[/output]
Last edited on
Well, I don't know how you are calling your Program.

If you have a 'b' prefixed to your base it should probably be
int base = atoi(argv[2]+1); // +1 to skip the 'b'
And you need to
#include <string>
Topic archived. No new replies allowed.