Thread: first c++ program(what am I doing wrong?)

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    41

    Exclamation first c++ program(what am I doing wrong?)

    This is my first c++ program . I've written the majority of it and I'm getting weird results. Would someone look at it and help me figure out what I'm do wrong? How would I continue the program until the user is ready to quit? Use a while statement? My program is attached.

    Problem Statement:
    Write a program (in a single source code file) that will do the following (in order):
    Ask the user to input three unique digits (0 through 9), and read in the input. (You may assume that the input is correct - no error checking needed).
    Using these digits, create and output the six unique numbers that can be made using all combinations of the three digits.
    Output the sum of the six numbers
    Ask the user if they want to run the program again. The user will input a character. 'Y' or 'y' will be considered a "yes" response. Anything else should be considered a "no". If the response is yes, repeat the processing. If no, end the program.

    Sample output
    Welcome!
    Enter three unique digits (0 through 9)
    Enter digit 1: 8
    Enter digit 2: 0
    Enter digit 3: 3
    The six new numbers:
    803
    830
    83
    38
    380
    308
    Sum of the six new numbers: 2442

    Would you like to run it again? (y/n): Y

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    not to do your homework for you, but the second and third numbers are actually 083 and 038. That's your first hint. Secondly outside of cout << means "shift left", think of it as like multiplication by a power of two
    int a=4,b=2;
    a<<b is a * 2^b
    How to actually do this? well the first number you need has num1 in the hundreds place, num2 in the tens place and num3 in the one's place. hint hint

    You don't need nearly as many variables as you seem to think you do, after you have printed the first number and added it to the total you don't need it anymore.

    You also need a loop I suggest do { } while(something)

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

    challenge

    as a challenge to more advanced memebers, write this program using only two variables and no more than three operations to calculate the total. Fun!

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    41

    i still don't understand

    Grib,

    Thanks for your reply but I don't understand your explainations. Could you explain it to me once more please?

    Thanks.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    given three numbers int a,b,c; what you seem to have to do is create a new number "starting" with a, followed by b followed by c. the easyest way to do this would be to just print out a b and then c, but your assignment does not allow this. We need make one number out of the three we have. To do this we use plain old math

    num = a*100 + b*10 + c;

    assuming each is less than 10 and greater than or equal to zero
    cout << num << endl;
    prints out abc, that is if a is 5 then num is 500 + something. if a is zero then num is just b*10+c and less than one hundred.

    as to the challenge to others, let me clarify: calculate the total using only a,b,c and three operations(forget fewest variables). jlmac just add num to a total after you print it each time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM