Thread: Getting the same answer, when the program restarts

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    1

    Getting the same answer, when the program restarts

    Well i've made a program that asks the decimal number, and puts it in a numberStelsel.
    BUt my problem is that it doenst work properly.
    for example
    Decimal number = 16
    Numberstelsel= 8
    I get: 20

    This is correct
    After i push de Y, to let the program start again.
    I insert
    Decimal number = 15
    Numberstelsel= 5
    I get: 20

    This is the wrong number + its the same answer of the one before this.
    Can someone help me with this one?

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    /* definition of functions: */
    int Input(void);
    void DecimalToBinary(double *);
    void Output(double);
    
    /* variables declare*/
    int nDecimal = -1, nStelsel;
    double dRem , dPow_10 = 1, dBinary;
    char chAgain = 'y';
    
    int main()
    {
    while(chAgain=='y')
    	{
    		nDecimal = Input();
       	printf("\nVoer het getalstel in:\n");
    		scanf("%d",&nStelsel);
    		while(nStelsel < 2 || nStelsel >= 10)
    		{
    			printf("Voer een getalstelsel tussen de 1 en de 10:");
    			scanf("%d", &nStelsel);
    		}
    		DecimalToBinary(&dBinary);
    		Output(dBinary);
    		printf("Do you want to run this program again? [y/n]: ");
       	chAgain = getch();
    	}
    
    
    	/* exit program */
    	return 0;
    }
    
    int Input(void)
    /****************************************************|
    |Function: Hier word het decimale getal gekozen.     |
    |Argument: geen                                      |
    |return:   nDecimal                                  |
    \****************************************************/
    {
    
    	while((nDecimal < 0) || (nDecimal > 1023))
    	{
          printf("Dit programma rekent een decimaal getal om naar een getal \nin de door uw ingevoerde getalstelsel\n");
    		printf("Your decimal number (0 .. 1023): ");
    		scanf("%d",&nDecimal);
    	}
    return nDecimal;
    }
    
    void Output(double dBinary)
    /*******************************************|
    |Function: Print de waarde uit              |
    |Argument: dBinary                           |
    |Return: geen                                 |
    \*******************************************/
    
    {
    	printf("The value is: %10.0lf\n",dBinary);
    }
    
    void DecimalToBinary(double *pBinary)
    /*********************************************\
    |Function: Rekent het om in het getalstelsel  |
    |Argument: nDecimal                            |
    |Return:   dBinary                              |
    \*********************************************   */       
    {
    	while(nDecimal != 0)
    	{
    		dRem = nDecimal % nStelsel;
    		nDecimal = nDecimal / nStelsel;
    		*pBinary = dBinary + (dRem * dPow_10);
    		dPow_10 = dPow_10 * 10;
    	}
    }

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Well, once your Input function retrieves a valid input it will keep returning that one. Look closer on this function.
    Oh, and this is a C# forum, not C++.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Mutiliplcation Program that uses a (do, while, and for loop)?
    By Debbie Bremer in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2008, 06:04 PM
  3. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  4. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 09:48 AM
  5. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM