Thread: Quick help for a college student :/

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    1

    Quick help for a college student :/

    I am having trouble with line 13 ( int numberOfDigits =....) im getting a "type double unexpected" message. I'm pretty sure its something small, but im new to c++. any assistance would help

    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main(){
      //Read in
      cout << "Enter an interger strictly between 0 and 1000: ";
      int number;
      cin >> number;
    
      //Calculations
    
      int numberOfDigits = double ceil(log10(number + 0.1));
    
      int lastDigit = number % 10;
      number = number / 10;
    
      int secondLastDigit = number % 10;
      number = number / 10;
    
      int thirdLastDigit = number % 10;
      number = number / 10;
    
      int sumOfDigits = lastDigit + secondLastDigit + thirdLastDigit;
    
      int averageOfDigits = (lastDigit + secondLastDigit + thirdLastDigit) / 3;
    
      //Read out
    
      cout << "The sum of the digits is " << sumOfDigits << endl;
      cout << "The average of the digits is " << averageOfDigits << endl;
      cout << "The number of digits is " << numberOfDigits << endl;
    
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    The function signature
    Code:
    double ceil( double num )
    just tells you that the function takes a double as its argument, and that the value that it returns is a double. You don't use the keyword "double" in your code when you call the function.

    In your program, since you are assigning the return value to an int, you should explicitly convert the double to an int, either with a C-style cast as in:
    Code:
    int numberOfDigits = (int)ceil(log10(number + 0.1));
    or use C++ static-cast as described here: Type Casting

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    The behavior of this program will not be correct if only a one or two digit number is given.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Updating in a sequential file?
    By Ronnyv1 in forum C Programming
    Replies: 1
    Last Post: 03-24-2009, 04:41 PM
  2. Database assignment is Killing me!
    By Boltrig in forum C Programming
    Replies: 2
    Last Post: 11-29-2007, 03:56 AM
  3. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM
  4. LinkList Sorting in C
    By simly01 in forum C Programming
    Replies: 3
    Last Post: 11-25-2002, 01:21 PM
  5. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM