Homework Help

This is a discussion on Homework Help within the C Programming forums, part of the General Programming Boards category; Hey Guys , i need some help with my code. i followed the design structure diagram, and it doesnt want ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    4

    Homework Help

    Hey Guys , i need some help with my code.

    i followed the design structure diagram, and it doesnt want to compile.

    Here is the code:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    
    double enterBase(double);
    double CalcOut(double);
    
    int main(void)
    {
    	int inBase,outBase,result[100],decimal,error,i;
    	char inStr[100];
    
    
    
    	printf("NUMBER BASE CONVERSION\n");
    
    	printf("Enter the input number base (2-10): ");
    	enterBase(inBase);
    
    	printf("Enter the output number base (2-10): ");
    	enterBase(outBase);
    
    	printf("Enter the number: ");
    	fflush(stdin);
    	gets(inStr);
    
    	decimal = 0;
    	error = 0;
    
    	for(i = 0 ; 0  > strlen(inStr); i = i -1)
    	{
    		if(inStr[i] < '0' || inStr[i] >= '0' + inBase)
    		{
    			error = 1;
    		}
    
    		else
    		{
    
    			decimal = decimal*inBase + inStr[i] - '0';
    		}
    
    	}
    
    	if(error = 1)
    	{
    		printf("INVALID NUMBER");
    
    	}
    
    	else
    	{
    		printf("%c  in %d is ",inStr,inBase);
    
    		
    		if(decimal = 0)
    		{
    			printf("zero in any base");
    		}
    
    		else
    		{
    			double calcOut(double decimal, double outBase);
    			
    		}
    
    
    
    }
    
    double enterBase(double base)
    {
    
    	do
    	{
    		printf(" ");
    		fflush(stdin);
    		scanf("%lg", &base);
    
    		if(base < 2 || base > 10)
    		{
    			printf("Error: Invalid base\n");
    
    			printf("Re-enter the base (2-10): ");
    			fflush(stdin);
    			scanf("%lg", &base);
    		}
    
    
    	}while(base < 2 || base > 10);
    
    	return base;
    }
    
    
    double calcOut(double number, double base)
    {
    	int result;
    	int index[100];
    
    	index[100] = 0;
    
    	while(number > 0)
    	{
    		result[index] = number%base;
    		index = index +1;
    
    		number = number/base;
    
    	}
    
    	while(index = 0)
    	{
    		index = index -1;
    		printf("%d",result[index]);
    	}
    
    	printf(" in base %lg",base);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by joutei View Post
    and it doesnt want to compile.
    Why? It should be telling you why it doesn't want to compile, and on what line it thinks the problem is.

    Also:
    Code:
    	while(index = 0)
    I doubt that does what you think it does.

    = is for assigning a value.
    == is for comparing a value.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4
    Hey could you run it in visual studio. youll see couple errors.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Hey could you just paste them in here since you are the one begging for help and not me? You'll see a couple of more posts from people who actually are willing to help - but you won't see any from me.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4
    Error 2 error C2143: syntax error : missing ';' before 'type' c:\users\user\downloads\numberbaseconversion.c 80
    Error 3 error C2065: 'base' : undeclared identifier c:\users\user\downloads\numberbaseconversion.c 87
    Error 4 error C2065: 'base' : undeclared identifier c:\users\user\downloads\numberbaseconversion.c 89
    Error 5 error C2065: 'base' : undeclared identifier c:\users\user\downloads\numberbaseconversion.c 89
    Error 6 error C2065: 'base' : undeclared identifier c:\users\user\downloads\numberbaseconversion.c 95
    Error 7 error C2059: syntax error : 'while' c:\users\user\downloads\numberbaseconversion.c 99
    Error 8 error C2059: syntax error : 'return' c:\users\user\downloads\numberbaseconversion.c 101
    Error 9 error C2059: syntax error : '}' c:\users\user\downloads\numberbaseconversion.c 102
    10 IntelliSense: expected a ';' c:\users\user\downloads\numberbaseconversion.c 81

  6. #6
    Registered User edw211's Avatar
    Join Date
    Jun 2011
    Location
    Wilkes-Barre, PA
    Posts
    22
    I'm a newbie, but I do notice a few things here:

    -you capitalized CalcOut() in your prototype; in your definition, you refer to it as calcOut().

    -don't use gets(). You should use the safe alternative that prevents buffer overflow, fgets(). Look it up.
    -scratch that, actually... why are you using gets() to read in a number? Just use scanf("%d", &varName);

    -you don't need a whole function full of stuff just to read in an integer, dude. You're over-complicating it. Look:

    Code:
    int inBase,outBase,decimal,error,i;
    int result[100];
    char inStr[100];
    
    printf("NUMBER BASE CONVERSION\n\N");
    
    printf("Enter the input number base (2-10): ");
    scanf("%d", &inBase);
    getchar();
    
    printf("\n\nEnter the output number base (2-10): ");
    scanf("%d", &outBase);
    getchar();
    
    printf("\n\nEnter the number: ");
    fgets(inStr, 100, stdin);
    Now I'm assuming you need to split the number into the whole and fractional components. However, I have no idea what the heck you're doing here:

    Code:
    for(i = 0 ; 0  > strlen(inStr); i = i -1)
    	{
    		if(inStr[i] < '0' || inStr[i] >= '0' + inBase)
    		{
    			error = 1;
    		}
    
    		else
    		{
    			decimal = decimal*inBase + inStr[i] - '0';
    		}
    
    	}
    But I do know your for loop test will always be the same because you are comparing 0, which never changes, to strlen(inStr) which also doesn't change.

    -your main() doesn't return 0
    Last edited by edw211; 06-09-2011 at 12:49 AM.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4
    Thanks man, you've been really helpful.

    The reason for creating separate functions is because that is what my lecturer has instructed me to do for homework, creating those two separate functions then calling it when it is needed.

  8. #8
    Registered User edw211's Avatar
    Join Date
    Jun 2011
    Location
    Wilkes-Barre, PA
    Posts
    22
    Seems rather convoluted, but whatever gets you a passing mark, I guess, haha.

    And no problem, good luck.

  9. #9
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,641
    Code:
    fflush(stdin);
    gets(inStr);
    Good god, do not do either of these things!!!!

    Cprogramming.com FAQ > Why gets() is bad / Buffer Overflows
    Cprogramming.com FAQ > Why fflush(stdin) is wrong

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C homework help
    By camel-man in forum C Programming
    Replies: 6
    Last Post: 01-25-2011, 08:19 AM
  2. Homework... help!
    By xMEGANx in forum C++ Programming
    Replies: 11
    Last Post: 10-15-2007, 05:36 AM
  3. Help with C++ Homework
    By TheTaoOfBill in forum C++ Programming
    Replies: 3
    Last Post: 10-08-2007, 07:17 PM
  4. C++ Homework Help!!!!
    By bar5037 in forum C++ Programming
    Replies: 12
    Last Post: 10-01-2007, 01:21 PM
  5. homework help
    By computerjunkie5 in forum C++ Programming
    Replies: 13
    Last Post: 10-27-2003, 10:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21