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).
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.
#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.
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).