Thread: Novice needs help

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    5

    Novice needs help

    The following is my the program I am suppose to do:

    A program that uses a sentinel controlled loop to inputs
    5 digits of a 5 digit number (no spaces will appear between
    the digits) and then prints the sum of the digits. For example,
    the input value 12345 should have a sum of 15 (1+2+3+4+5).

    The sentinel value is the newline character which should be
    the last character retrieved from the input stream. It's value
    is '\n'.

    My problems are as follows: I do not know how to convert from ASCII to a numerical value, and I am not exactly sure how to use a sentinel loop.

    The following is what I have so far.
    Any help explaining my problems would be appreciated.



    ________________________________________________
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;


    int main()
    {
    int digit;
    int sum=0;


    cout << "Enter a five digit number:";
    digit = cin.get();
    cout << digit<<"+";
    digit = cin.get();
    cout << digit<<"+";
    digit = cin.get();
    cout << digit<<"+";
    digit = cin.get();
    cout << digit<<"+";
    digit = cin.get();
    cout << digit;

    sum = sum+digit;


    cout << "The sum is: " << sum << endl;

    return 0;
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Your compiler will convert literal chars (like '\n') to the numerical equivalent. To see the actual numerical values, you can google for the ASCII chart.


    Code:
    int main()
    {
     int sentinal = '\n'; 
     int digit = 1;
     int sum=0;
    
    
    cout << "Enter the numbers to add:";
    
        while( (digit = cin.get() ) != sentinal)
     {
           if(digit != ' ')
        {
           sum = sum+digit;
        }
      
    }     
    
     cout << "The sum is: " << sum << endl;
    
     cin.get();  // pause screen
    
     return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    5

    Still doesn't work

    I tried what you have suggested, yet my results are still the same. The addition is still not converting, so the result (if I type 12345) is 255 not 15.

    Also, I want to make sure I grasp the concept of the sentinel loop. If I understand correctly, I am assigning \n as the value so as long as \n is not input it will keep taking digits.

    Thank you

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Sorry about that. The problem is, when the user enters '1' for instance, this has the corresponding ASCII value of 49. Hence:

    49+50+51+52+53 = 255

    So you must subtract 48 from each input.

    And yes, you get the idea of the sentinal it seems. Note that the string.h library of functions rely on the value of zero as a sentinal.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 06-17-2005, 10:00 PM
  2. Novice trying to learn C++
    By dead in forum C++ Programming
    Replies: 10
    Last Post: 12-01-2003, 09:25 PM
  3. Rtfm
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 03-13-2003, 05:19 PM
  4. help for a C novice
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 05-02-2002, 09:49 PM
  5. Please help a novice
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 01-08-2002, 10:53 PM