Thread: joining two pieces of code

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    3

    joining two pieces of code

    hi there I'm new to programing so sorry about the questions as I no there basic steps. but I just cant figure it out. I have wrote two pieces of codes now I wish to join them up, at the moment I have one code that will multiply the number I enter by 9 and give me the answer. and I have another code that will add up all the numbers I enter apart from the last one. what I wish to do is join the two codes. so when I enter a number it will multiply it by 9, then adds up all the numbers in the answer apart from the last one and gives me the answer. can anyone show me how to do this as iv spent hours and hours trying it but cant get it to work. hear are the two codes I have
    Code:
    #include <stdio.h>
    #include <iostream>
    using namespace std;
    void calc(int);
    int main()
    {
    int num;
    printf("enter 3 diget number above 0:\n");
    scanf("%i", &num);
    calc (num);
        return 0;
    }
    void calc (int n)
    {
     n=n*9;
     printf("the enterd number multiplyed by 9 id %i\n",n);
     return;
    }
    
    void calc (long)
    {
    return;
    }
    and the second one
    Code:
    #include <stdio.h>
    #include <conio.h>
    int main()
    {
      int num;
      int sum = 0;
      printf(" Enter a number above 0 : ");
      scanf("%d", &num);
      num /= 10;
      while (num > 0)
      {
        sum += num % 10;
        num /= 10;
      }
      printf("Sum = %d", sum);
      getch();
      return 0;
    }
    if anyone can help me it would be great, thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,415
    Join how?

    Do you want a main() where you print two choices like
    - multiply by 9
    - sum digits

    and you call the appropriate function?


    Well the first thing to do is to make both sets of code a function.
    Eg, your 2nd program could be
    Code:
    #include <stdio.h>
    #include <conio.h>
    void sumDigits() {
      int num;
      int sum = 0;
      printf(" Enter a number above 0 : ");
      scanf("%d", &num);
      num /= 10;
      while (num > 0)
      {
        sum += num % 10;
        num /= 10;
      }
      printf("Sum = %d", sum);
    }
    
    int main()
    {
      sumDigits();
      getch();
      return 0;
    }
    Now, copying the function sumDigits() to another program source becomes that much easier.

    FYI, the first program is C++ and the second program is archaic C.
    Be clear on what it is you're trying to learn.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sending JPGs in pieces?
    By chris24300 in forum C Programming
    Replies: 4
    Last Post: 01-15-2010, 08:04 AM
  2. Reading Multiple Pieces of Data From Text File
    By bengreenwood in forum C++ Programming
    Replies: 3
    Last Post: 09-02-2009, 07:49 AM
  3. Joining these code snippets
    By spadez in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 04:07 PM
  4. Are these two pieces of code equivalent?
    By leogoldseed in forum C Programming
    Replies: 7
    Last Post: 09-29-2008, 11:55 PM
  5. keeping track of number of pieces left
    By axon in forum C++ Programming
    Replies: 2
    Last Post: 02-14-2003, 05:55 PM

Tags for this Thread