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.
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) ;
}
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;
}
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.
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?
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.