I made a times table program, could i have some pointers?

This is a discussion on I made a times table program, could i have some pointers? within the C++ Programming forums, part of the General Programming Boards category; Here is the program: _________________________________ #include<iostream.h> #include<stdlib.h> int enter() { cout<<"Enter a number: "; return 0; } int main() { ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    2

    I made a times table program, could i have some pointers?

    Here is the program:

    _________________________________
    #include<iostream.h>
    #include<stdlib.h>

    int enter()
    {
    cout<<"Enter a number: ";
    return 0;
    }
    int main()

    {
    int yana;
    do

    {
    int iX, iY, iG, iA, iB, iC, iH, iZ, iJ, iK, iN, iM, iL;
    system("cls");
    yana=0;
    enter();
    cin>> iX;
    enter();
    cin>> iY;
    enter();
    cin>> iG;
    enter();
    cin>> iB;
    enter();
    cin>> iJ;
    enter();
    cin>> iZ;
    enter();
    cin>> iC;
    enter();
    cin>> iH;
    enter();
    cin>> iK;
    enter();
    cin>> iN;
    enter();
    cin>> iM;
    enter();
    cin>> iL;
    iA=iY+iX+iG+iB+iC+iH+iZ+iJ+iK+iN+iM+iL;
    cout<< "\nThe sum of your numbers are: "<<iA<<endl;

    cout<<"do you want to continue? type 1 to do again\n";
    cin>>yana;
    }while(yana==1);
    cout<<"This program has been brought to you by: Twarrior.";
    cout<<"For some updates go and email me at tysterty@cs.com";
    system("pause");
    return 0;
    }
    _________________________________

    I just need a few pointers, so help a the newguy out!

    P.S.: I attached the c++ program so you can look at it off lne.
    Attached Files Attached Files
    Last edited by Cwarrior; 05-02-2003 at 12:51 PM.

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    its pretty good as is. One thing that you might consider changing is allowing someone to enter as many (or as few) numbers as they want. Also, cout<<"Hit 1 for more info\n"; seems to be useless there since there is no way for the user to input any more data at that point.
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    2

    thx pimp!

    Thx clown pimp!

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Here's a few for you
    int* p = NULL;
    int* mem = new int;
    char* a = new char;
    (*a) = 'a';

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    51

    Arrays

    You could use arrays as well for your program. That would make it so you could loop your program. A few "endl"s to help space out your program would make it look more organized. Now, you could void the enter() function since it doesn't return anything.

    For example:
    Code:
    #include<iostream.h>
    #include<stdlib.h>
    
    void enter()
    {
     cout << "Enter a number: ";
    }
    
    int main()
    {
     int yana;
     int iC = 0; //counter
    
     do
     {
      int iArray[12] = {0}; //declares 12 ints for storing numbers
      int iSum = 0; //stores sum
      yana=0;
    
      system("cls");
    
      while (iC < 12)
      {
       enter(); //call enter function
       cin >> iArray[iC];
       iSum = iSum + iArray[iC]; //adds new number into iSum
       iC++;
      }
    
      cout << "\nThe sum of your numbers are: "<< iA <<endl;
    
      cout << "Do you want to continue?  Type 1 to do again\n";
      cin >> yana;
     } while(yana == 1);
    
     cout << "This program has been brought to you by: Twarrior." << endl;
     cout << "For some updates go and email me at tysterty@cs.com";
     system("pause");
     return 0;
    }
    But by using a loop, we realize that an enter() function isn't even necessary! So we could either inline the function, since its only called once, it would help to increase efficency. Or we could just directly place the function into the code. Also, we could use the += operator instead of repeating iSum again.

    For example:
    Code:
    #include<iostream.h>
    #include<stdlib.h>
    
    int main()
    {
     int yana;
     int iC = 0; //counter
    
     do
     {
      int iArray[12] = {0}; //declares 12 ints for storing numbers
      int iSum = 0; //stores sum
      yana=0;
    
      system("cls");
    
      while (iC < 12)
      {
       cout << "Enter a number: ";
       cin >> iArray[iC];
       iSum += iArray[iC]; //adds new number into iSum
       iC++;
      }
    
      cout << "\nThe sum of your numbers are: "<< iA <<endl;
    
      cout << "Do you want to continue?  Type 1 to do again\n";
      cin >> yana;
     } while(yana == 1);
    
     cout << "This program has been brought to you by: Twarrior." << endl;
     cout << "For some updates go and email me at tysterty@cs.com";
     system("pause");
     return 0;
    }
    It'd also be a good idea, as ClownPimp suggested, to add an option to select the numbers you can choose to add together. Happy programming!
    Last edited by luckygold6; 05-02-2003 at 01:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutiliplcation Program that uses a (do, while, and for loop)?
    By Debbie Bremer in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2008, 06:04 PM
  2. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  4. Replies: 2
    Last Post: 03-13-2003, 08:40 AM
  5. Replies: 5
    Last Post: 11-19-2002, 08:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21