Thread: I need help with this Turbo C program. Please.

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    1

    Question I need help with this Turbo C program. Please.

    I can't seem to work this out. It's really difficult. I've been trying for hours and I always get errors. Please help me.

    I have no idea how to write a code that will let me input how many variables I want.


    Write a program that will enable you to enter a certain amount of values

    Eg. 4

    And then add the corresponding integers.
    Ex.

    Choice: 4

    12 34 56 78

    The sum of the integers is: 180.

  2. #2
    Registered User
    Join Date
    Sep 2014
    Location
    SE Washington State
    Posts
    65
    declare your variables (4) plus one for the answer.
    input the values as integers (%d) four times.
    add the integers and assign the value to the fifth variable.
    print out the answer and pause the program so you have a chance to read the answer.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Welcome to the forum. You don't need separate variables for each input on this task. In fact, you only really need two.

    I've been trying for hours and I always get errors.

    ...

    I have no idea how to write a code that will let me input how many variables I want.
    Plan your program first, preferably on paper. Only then start writing code. If you do not know how to solve a problem yourself, you will not be able to program a computer to solve it.

  4. #4
    Registered User
    Join Date
    Sep 2014
    Location
    SE Washington State
    Posts
    65
    Matticus,
    You are, of course, correct, but it might be easier for a beginner to understand using a variable for each number and another for the answer.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by PaulS View Post
    Matticus,
    You are, of course, correct, but it might be easier for a beginner to understand using a variable for each number and another for the answer.
    But the problem is that the user has to enter how many values they will be entering, which means this number will not be known at compile time.

    That means your suggestion would require either dynamic memory allocation (which would certainly be above the level of the assignment) or else an array (which would make the resulting code more complicated than it needs to be).

    I stand by my hint as the simplest solution.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird Turbo C based, Graphics program error
    By sidx64 in forum C Programming
    Replies: 8
    Last Post: 11-18-2012, 05:12 PM
  2. Replies: 3
    Last Post: 11-13-2012, 12:17 AM
  3. (c program)ARRROW key don't in borland turbo c++ 4.5
    By massiha in forum C Programming
    Replies: 8
    Last Post: 12-19-2010, 12:04 PM
  4. Need help with a turbo c program....urgent plz help
    By hassan_shohag in forum C Programming
    Replies: 10
    Last Post: 06-27-2010, 01:14 PM

Tags for this Thread