basic examples of C++

Discussion in 'C++' started by zachry00, Nov 21, 2011.

  1. zachry00

    zachry00 New Member

    Joined:
    Jul 20, 2011
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    0
    hi to all of you can you pls give me some basic programs here at C++ i am new to this programing language. thank you anything you post will be helpful

    chow...:pleased::pleased:
     
  2. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    Not sure what you mean by programs... do you mean source? How about inputting a few integers into a vector and then output what's been stored.

    Code:
    #include <iostream>
    #include <vector>
    
    int main() {
    
       std::vector<int> data;
       std::vector<int>::iterator it;
       int num;
    
       do {
           std::cout << "Enter a number (0 to quit): ";
           std::cin >> num;
    
           if(num != 0) {
              data.push_back(num);
           }
           
       } while(num != 0);
    
       for(it = data.begin(); it != data.end(); ++it) {
          std::cout << "numbers: " << *it << std::endl;
       }
    
       return 0;
    }
    I haven't done a lot of coding recently, so I may be a bit rusty. There's no error checking of anykind and you might be better off using stringstreams instead of cin to read the numbers, but hopefully, it will be helpful.

    Good luck.
     
  3. bhabe

    bhabe New Member

    Joined:
    Feb 1, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hey sir., can you help me bout how to determine the largest or smallest number if i'll input 10 numbers? :0
     
  4. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    initialize variables for largest and smallest
    
    loop(while inputted numbers are valid) {
       if(largest_variable less than current_value)
          largest_variable equals current_value;
       /* similar approach for smallest value */
    }
    HTH
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice