Help with a very basic C++ program

This is a discussion on Help with a very basic C++ program within the C++ Programming forums, part of the General Programming Boards category; okay so enclosed is the program i am working on for class, basically what it does is it will take ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    8

    Help with a very basic C++ program

    okay so enclosed is the program i am working on for class, basically what it does is it will take the provided integers and get the avg of them as well as the difference, smallest and largest number; but my problem is that i need it to be where if the user enters a "0" then the program will go to the end and basically still show the avg of the numbers up till 0, the smallest, largest and difference as well, so if someone could please help/ teach me to do this it would be mostly appreciated. by the way i hope i posted this properly i am just getting used to writing codes.

    --------------------------------------------------------------
    Code:
    //Charles Shane Miller Jr.
    
    
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
              int one; // integer uno
              int two; // integer dos
              int three; // integer tres
              int four; // integer cuatro
              int five; // integer cinco
              int avg; // average
              int smallest;
              int largest;
              int diff;
              int zero;
              
              cout << "So what is lucky number one?" << endl;
              cin >> one;
              cout << "okay and what shall the terrible second integer be?" << endl;
              cin >> two;
              cout << "So what is unlucky number 3?" << endl;
              cin >> three;
              cout << "okay and que es numero cuatro por favor?" << endl;
              cin >> four;
              cout << "and last but not least, what is lucky number five? " << endl;
              cin >> five;
              avg = one * two * three * four * five / 5 ; //formula to take integers and get the average
              cout << "" << endl;
              cout <<"The average of the numbers is " << avg << endl; // outputs the answer in a user friendly way
    cout << "" << endl;
    if ( one < two ) // start of algorithm to find smallest number
          smallest = one;
       else 
          smallest = two;
    if ( smallest < three )
          smallest = smallest;
    else
          smallest = three;
    if ( smallest < four )
         smallest = smallest;
         else 
         smallest = four;
    if ( smallest < five )
       smallest = smallest;
       else
       smallest = five;
       cout << "the smallest is  " << smallest << endl; // end of algorithm to find smallest number
       cout << "" << endl;
       if ( one > two ) // start of algorithm to find largest number
          largest = one;
       else 
          largest = two;
    if ( largest > three )
          largest = largest;
    else
          largest = three;
    if ( largest > four )
         largest = largest;
         else 
         largest = four;
    if ( largest > five )
       largest = largest;
       else
       largest = five;
    if ( 0 )
     break;
     else 
     { continue; }
       cout << "the largest is  " << largest << endl; 
       cout << " " << endl;
       diff = largest - smallest;
       cout << "The difference of the laregst number and the smallest number is " << diff << endl;
              system("PAUSE");
              while (zero != 0);
              return 0;
              }
    --------------------------------------

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    if ( smallest < three )
          smallest = smallest;
    So, don't do that, ok? Just reverse the order/direction of the compare, and do the smallest = three if the test shows three is greater...

    Code:
    if ( 0 )
     break;
     else 
     { continue; }
    Doesn't do ANYTHING useful。

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    i meant to take the break out because it doesnt work with the bloodshed dev-c++ program, like it does all the functions i want it to i just want it to have the ability to if the user enters a 0 that it will stop the program and go to the end showing the largest/smallest numbers of the 5 that the user entered already, hope i am explaining it right but i want it to if the user enters a 0 go to the end of the program... :-/

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    You might want to learn about arrays and loops first. The program that you want to write will be much more easily implemented using an array and loops.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    any websites to teach this easier? i am sorry to be a hassle just this professor really doesnt teach she talks about it for a second and that is about it other than that i am rather lost :-/ sigh.

  6. #6
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68
    You need to understand:
    Code:
    if ( 0 )
    translates to:
    Code:
    if ( FALSE ) 
    {
        // this will 'never' be run
    }
    regardless of break or no break, you will always jump to else

    Start here with the tutorial on if statements to understand the above, and continue through the end of the array section. (Some of that may or may not apply to what you have covered in class so far, but in either case the tutorial will help you in the long run.)
    Last edited by QuestionKing; 02-11-2009 at 10:06 AM.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    okay, thanks for the help i will try to figure it out, again thanks!

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    Code:
              while (zero != 0);
    now cant i use that to end the program and if i do would it be like this:

    Code:
    //Charles Shane Miller Jr.
    
    
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
              int one; // integer uno
              int two; // integer dos
              int three; // integer tres
              int four; // integer cuatro
              int five; // integer cinco
              int avg; // average
              int smallest;
              int largest;
              int diff;
              int zero;
              
              cout << "So what is lucky number one?" << endl;
              cin >> one;
              cout << "okay and what shall the terrible second integer be?" << endl;
              cin >> two;
              cout << "So what is unlucky number 3?" << endl;
              cin >> three;
              cout << "okay and que es numero cuatro por favor?" << endl;
              cin >> four;
              cout << "and last but not least, what is lucky number five? " << endl;
              cin >> five;
              avg = one * two * three * four * five / 5 ; //formula to take integers and get the average
              cout << "" << endl;
              cout <<"The average of the numbers is " << avg << endl; // outputs the answer in a user friendly way
    cout << "" << endl;
    if ( one < two ) // start of algorithm to find smallest number
          smallest = one;
       else 
          smallest = two;
    if ( smallest < three )
          smallest = smallest;
    else
          smallest = three;
    if ( smallest < four )
         smallest = smallest;
         else 
         smallest = four;
    if ( smallest < five )
       smallest = smallest;
       else
       smallest = five;
       cout << "the smallest is  " << smallest << endl; // end of algorithm to find smallest number
       cout << "" << endl;
       if ( one > two ) // start of algorithm to find largest number
          largest = one;
       else 
          largest = two;
    if ( largest > three )
          largest = largest;
    else
          largest = three;
    if ( largest > four )
         largest = largest;
         else 
         largest = four;
    if ( largest > five )
       largest = largest;
       else
       largest = five;
       cout << "the largest is  " << largest << endl; 
       cout << " " << endl;
       diff = largest - smallest;
       cout << "The difference of the laregst number and the smallest number is " << diff << endl;
              system("PAUSE");
              while (zero != 0);
              return 0;
              }
    thanks again

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    well now i just need to know i got it working, sorry to bug you all, but how do i set it so when 0 is typed in the program automatically exits?

  10. #10
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68
    Ok. It looks like you have a good understanding of how to create conditionals.
    (maybe I should point out while(zero !=0); still may not exactly do what you want it to do)
    ... now you should get into the structures, arrays, and switch sections, and repost your code after those sections. You should be well under way by the end of those sections.
    (your program should be much shorter, include some for loops, and have fewer variables)
    Feel free to ask questions if you need to.
    Last edited by QuestionKing; 02-11-2009 at 01:59 PM.

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    yeah reading into them problem with my conditionals lol is if u use a way to end the program prematurely then it messes up the largest number value for some odd reason :-/ bleh back to the drawing board i suppose.

  12. #12
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68
    Stick to it and look over them tutorials, compare to see how and when the tutorials match parts of your program you are writing. Compare to the sample code, and what it can do. You are making progress, good job so far.

  13. #13
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68
    You might also consider editing your posts and removing what appears to be a RL name from your code, but that is up to you. This is a public forum, anyone could be reading...

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    Code:
    #include <iostream.h>
    
    int main ()
    {
        int i = 0;
        int j,totals,x,z;
        double avg;
        int k = 0;
        int largest = 0;
        int smallest = j;
        do
        {
            cout << "Enter a number (enter zero to exit): ";
            cin  >> j;
            i = i + j;
            k = k + 1;
            if (j > largest) {largest = j; }
            if (j < smallest) {smallest = j;}
        }
        while (j != 0);
        totals = k - 1;
        avg = double(i) / (double(k) - 1);
        cout << "\n\n\nThe number of numbers is " << totals << endl;
        cout << "The average is " << avg << endl;
        cout << "the largest number is " << largest << endl;
        cout << "The Smallest Number is " << smallest << endl;
        system("PAUSE");
        return 0;
    }
    i was alternating between the two codes but now i cant get the smallest code to work properly lol this is interesting may have to resort to the book i swear why does my professor work here if she doesnt go over anything lol...
    hehe oops.
    Last edited by Method4ever; 02-11-2009 at 02:41 PM. Reason: nub

  15. #15
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68
    Please edit your post above and move your comment out of the code so wordwrap will adjust the length.

    Start with an outline of your program's requirements. What it must do for an end result. Then populate a list of what will be required to accomplish the task. What variables, how many, etc.
    Then think of ways some of them relate to one another, see if you can come up with a system that will be easy to understand as well as cutting down on variable use. There is more than one way to do this, but you are not yet using some very nice features you could be. I will start you out with a hint, I see first off you want the user to input 5 integers. There should be an easy way to get and store those with just a few lines of code. (I am about the same level of learning as you, so I am keeping a close eye on this thread incase someone else offers some input)

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. Basic encryption program???
    By Finchie_88 in forum C++ Programming
    Replies: 14
    Last Post: 09-10-2004, 09:01 AM
  3. IDEA: A basic drawing program
    By ygfperson in forum Contests Board
    Replies: 0
    Last Post: 08-12-2002, 11:15 PM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. Help me with this basic c program
    By [ToXiC]-[LeaK] in forum C Programming
    Replies: 1
    Last Post: 01-28-2002, 10:44 PM

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