CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2013
    Posts
    1

    Allowing user to input decimal.

    This code is near complete, the only task that is left is allowing the user to input a decimal and then two integers, or automatically using .00 decimal.

    The automatic part: /*This is not correct.
    printf(".%.2d\n", number);
    */

    But that does no good for me. Question: Do I have to create a some sort of while loop again, to allow the user to input a decimal followed by integer?
    Code:
    #include <iostream>
    #include <cstdlib>
     
    using namespace std;
     
    int main()
    {
        int j , i = 0, k = 0;
    	int number;
    	static_cast<double>(number);
    	double sum = 0.0;
    	double Average = 0.0;
        
    	cout << "Enter Positive integer number: ";
        while(cin >> number)
        {
            cout << endl;
            if( number < 0)//test if the number is negative
            {
    			cout << "Ending program since user has input a negative number" <<endl;
                break;
            }
            int temp = number;
            int p = 1;
    		sum = (sum+temp);	
    		Average = sum/2;
            
    		while(temp > 0) //counting number of digits
            {
                temp /= 10;
                p *= 10;
                i++;
            }
    
    			cout << "Total Sum: " << sum << endl;
    			cout << "Average: "<< Average << endl;
    
            j = i % 3;
            p /= 10;
            while( i > 0 )//display integer number with 1000 seperator
            {
    			//entering gives me error if digits exceed 9
                cout << char ((number/p) +'0');
                number %= p;
                p /= 10;
                i--;
                k++;
                j--;
                if ((k % 3 == 0 && i > 0)||(j == 0 && i > 2) )
                {
                    cout <<",";
                    k = 0;
                }
            }
    		/*This is not correct.
    		printf(".%.2d\n", number); 
    		*/
            cout << endl << endl;
            cout << "This program exits if you input negative number and/or input non-integer\n";
            cout << "Enter another integer number: ";
        }
    
        return 0;
    }

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Allowing user to input decimal.

    This line does nothing

    Code:
    static_cast<double>(number);
    as the result of the cast is not used.

    Code:
    Average = sum/2;
    The average of a set of numbers is their sum divided by the number of numbers. So in this case you need to keep a count of the number of numbers entered and use that for the division to find the correct average.

    For 32 bit signed integers, the maximum value is 2147483647 (see limits.h)
    //entering gives me error if digits exceed 9
    because when the number of digits exceeds 9,
    Code:
    p *= 10;
    causes p to overflow its max value.

    Code:
    /*This is not correct.
    		printf(".%.2d\n", number); 
    		*/
    try
    Code:
    printf("\n%.2lf\n", (double)number);
    which casts number to a double so it can be displayed as a double with decimals.
    Note that at this point in your code, number does not have the correct value to display.

    allowing the user to input a decimal and then two integers,
    If the user needs to be able to input a number containing a decimalpoint, then this is a double/float number and can be input directly as such.

    When confronted with a program which does not work as expected, you need to use the debugger to trace through the program to see where its behaviour deviates from that which is expected. being able to use successfully the debugger is as important as being able to write code and is a skill which needs to be mastered.

    Also, just using single letter variable names is not very helpful when trying to understand the code. Use meaninful variable names and don't re-use a variable for more than one purpose.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured