Just started a Ansi C course at uni, wanted to practice a bit so I can go into the subject knowing what I am doing.
I'm trying to code a base conversion that inputs a baseten number and a base to convert it to and outputs the converted number.
I've ran into a few errors however and googling hasn't really helped me, I'll be fucking around for hours before I get results.
Can anyone see immediate fixes for the errors I have commented in below (I'm using visual studio, also compiling using gcc on the uni network)
Also if you see any bad coding practices in what I have wrote let me know
[code]
#include <stdlib.h>
#include <stdio.h>
//#include <string.h>
float parseChar(char * toConvert);
float checkForEs(const char * toConvertArray);
int main(int argc, char *argv[])
{
int stage = 0;
if (argc != 3) {
printf("Incorrect amount of arguments\n");
printf("Usage: basecontrol <number to convert> <base to convert to>");
printf("The input must be a base10 number for this to work correctly.");
return EXIT_FAILURE;
};
float numInBaseTen = parseChar(argv[1]);
//actual conversion will go here: convert(numInBaseTen);
return EXIT_SUCCESS;
}
float parseChar(char *toConvert) {
char toConvertArray[] = toConvert; // Why is this erroring? "Initialization with '{...}' expected for aggregrate object"
int i = 0;
float num;
//will probably add a validty check here, to make sure the user isn't inputting stupid shit like 123ajdicks0
// checking for "e's"
num = checkForEs(toConvertArray); // Basically looks for "e"s, in case the user wants to put in a large number like 1.56e20
return num;
};
float checkForEs(const char* toConvertArray) {
int i = 0;
float j;
int k;
char temp1[20];
char temp2[20];
int strlenc = sizeof
for (i < strlenc; i++) { // For loop is erroring: "expected an expression?"
if (toConvertArray[i] == "e" || toConvertArray[i] == "E") {
for (int temp = 0; temp < (i - 1); temp++) {
if (temp == 20) {
return -1;
}
temp1[temp] = toConvertArray[temp];
}
j = atof(temp1);
for (int temp = i + 1; temp < strlenc; temp++) {
if (temp == 20) {
return -1;
}
temp2[temp] = toConvertArray[temp];
}
k = atoi(temp2);
for (int z = 0; z < k; z++) {
j = j * 10;
}
break;
}
else {
j = atof(toConvertArray);
};
};
return j; // Error: expected a declaration, wot
}[/code]
[code]float parseChar(char *toConvert) {
char toConvertArray[] = toConvert;
[/code]
You can already use "toConvert" variable like an array since it's a pointer reference. No need to try to place it in another variable.
First, what do you mean by ANSI C? I see some ANSI here, but I also see c99 here as well.
in CheckForEs, you have:
[code]
int i = 0;
//...
for(i < strlenc; i++){
//..
}
[/code]
a for loop requires 3 parameters. You don't actually have to put anything there, but you do have to use semicolons
[code]
int i = 0;
//...
for(; i < strlenc; i++){ //empty first argument.
}
[/code]
also, I'm unsure what you're trying to do here:
[code]
int strlenc = sizeof;
[/code]
sizeof is an operator. If you want the size of something, you have to do:
[code]
int strlenc = sizeof(object); //where `object` is your string.
[/code]
however, you don't need to do that, since the C standard library has `strlen` to do exactly that!
try, instead:
[code]
int length = strlen(toConvertArray);
[/code]
but make sure to put
[code]
#include <string.h>
[/code]
at the top.
also, when checking for `e`s:
[code]
if (toConvertArray[i] == "e" || toConvertArray[i] == "E") {
[/code]
the double quotes denote a string, not a char. if you want to check for a char, you use single quotes, or:
[code]if (toConvertArray[i] == 'e' || toConvertArray[i] == 'E') {[/code]
If you did want to check for a string, by any chance, you would use the "strcmp" function (string compare).
[code]
if(strcmp("string1", "string2") == 0){
//...
}
[/code]
strcmp returns 0 if the strings are equal; it returns some other number based on how un-equal they are.
as for your parseChar function:
[code]
char toConvertArray[] = toConvert;
[/code]
is unnecessary. if you want to access `toConvert` like an array, you can simply do:
[code]
toConvert[0];
toConvert[1]; //etc.
[/code]
an array is a lot like a pointer in that regard, but they're not the same thing.
also, there are better ways to convert bases:
[url]http://www.had2know.com/academics/convert-base-n-remainder-method-calculator.html[/url]
at the end of the file, the error "expected a ddeclaration" is because you put
[code]for(...){
}; //this semi-colon ought not be here!
[/code]
thanks very much, I have gotten what I have so far to work (it will calculate your input if it has an "e" in it)
Now just have to implement base calculation by remainders and validity checks on the input. Thanks!
Sorry, you need to Log In to post a reply to this thread.