Program Structure

Hello, Im a novice to programming. I just wanted to know if anyone could direct me to a site with fairly simple c++ programs that I would be able to get into and see the structure and how the commands work to get a preview before I dive into it.
Hm... even fairly simple C++ program can appear quite involved for a beginner, because you'd not only have to understand the syntax of C++, but also the standard library and third party libraries involved, as well as the idioms used.

That being said, here's a simple program that asks the user to input a range (for example from: 0 to: 10) and then outputs the fibonacci numbers in that range (for example, the 0th fibonacci number to the 10th fibonacci number).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <vector>
 
std::vector<int> getFibs(int from, int to);
 
int main(int argc, char** argv)
{
    int from,to;
    std::cout << "Which fibonacci numbers do you want?" << std::endl
              << "from: ";
    std::cin >> from;
    std::cout << "to: ";
    std::cin >> to;
    std::vector<int> fibs = getFibs(from,to);
    std::cout << "[";
    for(std::vector<int>::iterator it=fibs.begin(); it!=fibs.end(); ++it)
    {
        std::cout << *it << " ";
    }
    std::cout << "]" << std::endl;
    return 0;
}
 
std::vector<int> getFibs(int from, int to)
{
     int a = 0;
     int b = 1;
     for(int i=0; i<from;++i) {
         b += a;
         a = b - a;
     }
     std::vector<int> result;
     for(int i=from;i<to;++i) {
         b += a;
         a = b - a;
         result.push_back(a);
     }
     return result;
}



Here you can see it in action - scroll down to the compile results, click on input and then enter the 2 numbers there (for example
0 10

and then click one ideone it)
http://ideone.com/OntbV#ul_inouterr

Note that this is a very simple example - real programs often span dozens (if not thousands) of files, also this example didn't include any error checking whatsoever. It also solves only a single problem, while most programs will achieve more complex tasks that need to be broken down into parts that are solved individually, coordinating all of this is also an important skill one only learns from practice.

That doesn't mean you necessarily have to understand everything this program does immediately though. If you're a complete novice some things might be still a bit unfamiliar to you.
Last edited on
I wouldn't call that simple for a novice programmer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream> //cout, cin, endl
#include <cmath> //pow

int main()
{
    int pownum; //declare a variable, so we can hold data, in this case an int, or integer, which is any number from -2.4 billion to +2.4 billion

    while (true) //never end
    {
        std::cout << "Enter a number to be the power of 2" << std::endl; //output text
        std::cin >> pownum; //get text

        std::cin.clear(); // these two lines are to stop infinite loops caused by unexpected input, like a letter
        std::cin.ignore();

        std::cout << std::endl; //new line
        std::cout << "2^" << pownum << " = " << pow(2, pownum) << std::endl; // display result
        std::cout << std::endl; //new line
    }

    return 0;
}


that is the sort of code you'd be expected to write on your first or second day, nothing complicated, just input/output and math.
Last edited on
He wanted to see structure, so I thought having a function besides main should be in there. main shouldn't do much, if any, work.

//declare a variable, so we can hold data, in this case an int, or integer, which is any number from -2.4 billion to +2.4 billion

This is wrong btw. First of all, it's roughly +-2.1 billion (from -2^31 to +2^31-1) for 32 bit integers - second, int's aren't guaranteed to be 32 bits (well, they are most of the time on current system, but the standard only guarantees than an int is large enough to hold a short int, which is large enough to hold a char and at least 16 bits, char being at least 8 bits).


// these two lines are to stop infinite loops caused by unexpected input, like a letter

One call to cin.ignore() ignores exactly 1 character. I somehow doubt that is the intent here. Note that if you include <cmath>, you should use std::pow rather than pow. (pow is the version declared in math.h, while std::pow has overloads for various types).
Last edited on
Topic archived. No new replies allowed.