Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C Problem - for, while and do-while problem

Name: Anonymous 2012-03-13 13:20

Hi guys, so basically I have a little problem. I wrote a systematic program designed to convert base 5 numbers to base 10, (but it'll function with mostly any bases)

here's the code:
#include "stdafx.h"

#include <conio.h>


#include <stdlib.h>




int convert(int a,int base);


int main()
{
 int a,base,ans;
 
 printf("\nEnter your preferred number: ");
 scanf("%d", &a);

 printf("\nEnter the base of that number: ");
 scanf("%d", &base);

 ans=convert(a,base);
    
 printf("The converted base 10 is: %d", ans);
 getch();
 return 0;
}  


int convert(int a,int base)
{
int sum=a%10;
for(int i=base;(a/=10)!=0;i*=base)
sum+=a*i;
return sum;
}

//it compiles and works.

the issue with this is that im also supposed to switch the for conditional, and make it work with while, and do-while, and acquire the same results... and ive literally tried everything.

int convert(int a,int base)
{
int sum=a%10;
int i = base;
        
while((a/=10)!=0){
i*=base;
sum+=a*i;
}
return sum;
}


int convert(int a,int base)
{
int sum=a%10;
int i = base;
do{
i*=base;
sum+=a*i;   
}while((a/=10)!=0);

return sum;
}

yes so this is my dilemma. so if anyone is willing to help, it will be greatly appreciated.

Name: Anonymous 2012-03-13 13:23

Firstly, use code tags.
Secondly, you're using the wrong tool for the job, don't ask  scanf for a number and instead get a string.
Thirdly, do your homework alone.

#include "stdafx.h"
#include <conio.h>
Considered harmful.

Name: Anonymous 2012-03-13 13:27

well im actually using the borland compiler to run this so the stdafx.h is unnecessary and this point, and no this is not my homework. im majoring in math, and im just interested in programming. if i cant ask for help, then whats the point of this board?

Name: Anonymous 2012-03-13 13:31

>>3
im actually using the borland compiler
Use GCC.

if i cant ask for help, then whats the point of this board?
Spamming and expert-level programming.

Name: Anonymous 2012-03-13 14:03

so you cant help me?

Name: Anonymous 2012-03-13 14:04

btw, i find this problem would even be challenging for most expert programmers, considering i havent solved it yet.

Name: Anonymous 2012-03-13 14:43

Use the Standard Library, OP.
char buf[16]; /* large enough to hold 2**32 in base 5, sign, and NUL */
long n;
scanf("%15s", buf);
n = strtol(buf, 5);
printf("%s in base 5 is %d in base 10\n", buf, n);

Name: Anonymous 2012-03-13 14:45

>>3
This purpose of this board is to act as an automated DUBZ generation and verification mechanism.

Name: Anonymous 2012-03-13 16:08

Use strtol and fgets, scanf is shit.

Name: Anonymous 2012-03-13 16:15

fgets more like fagets

AMIRITE LOLOLOLolOLOlollllllOOOOOOOOOOOOOOOOOOOOLAOLOLAojasojdfoaijsdiosjafbscxzhjcrqttwqssassdsadasdsdasdsDSaDSDSSDDSDSDSDSDSDSDS omFFGGGGGGGGGGGGGGGGGGGGFDBIUBD SIAIAIAIAIIAIAIIAIAIAIAIIAIAIAIAIAIAIAI THE NUMBER TRANSFORMATION MFDSJKKKKKKKKKKKK

Name: Anonymous 2012-03-13 16:35

Just don't put shit like (a/=10) in your conditionals; you wouldn't be having this problem if you coded cleanly. (Also, I recommend reading a string and converting that to base 10 instead, so it'll support bases over 10.)

Don't change these.
Name: Email:
Entire Thread Thread List