hey welcome to the forum, I think the main problem you are having is due to the lack of understanding of program design.
Usually when you buy a book on programming they do not discuss how to actually design programs, they teach you syntax of a specific programming language.
In program design, basically you have 3 basic control structures which will allow you to create any kind of program you want, they are:
1. sequential
2. selection
3. iteration
the sequential control structure is usually the default, when you create a program it executes each statement by sequence.
the selection control structure is used when you want to do different things depending on different values, for example
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
|
#include <iostream>
#include <string>
using namespace std;
int main()
{
string password = ""; // create a string to hold our password
cout << "password: ";
cin >> password;
// selection control structure
// the program does something different depending on the conditions
if(password == "mypass")
{
cout << "access granted\n"; // it only executes this part if the condition was true
}
else
{
cout << "access denied\n"; // it will execute this part if the condition was false
}
return 0;
}
|
Anyway, the selection control structure allows your program to do different things or execute different statements depending on when certain values are achieved. if statements and switch statements would fall under this category.
Next is the iteration control structure, you use iteration control structures when you need to do something repeatedly, usually you can use these when there is a simple predefined pattern you want to achieve which you need to do repeatedly.
for example if you want to print out something 5 times:
1 2 3 4 5
|
// iteration control structure
for(int i = 0; i < 5; i++)
{
cout << "hello world\n"; // print out hello world 5 times
}
|
or if you want to print out even numbers from 0 to 100
1 2 3 4 5
|
// iteration control structure
for(int i = 0; i < 100; i += 2)
{
cout << i << "\n";
}
|
you can also use it to keep your program running until someone tries to exit.
1 2 3 4 5 6 7 8
|
bool exit = false;
// iteration control structure
while(!exit)
{
// do whatever you want your program to do here
// if the user is trying to exit, set the exit value as true
}
|
Anyways, for, while, do-while and all kind of looping mechanisms would fall under the iteration category.
Anyways these are just some examples, so those are the basic elements all programs have. Then all you have to do is to learn how to combine those to create what you want to do. You can combine something to keep iterating then select what it's going to do like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
// iteration control structure
while(condition)
{
// selection control structure
if(some condition is true)
{
// do something
}
else if ( some other condition is true)
{
// do something else
}
else
{
// none of the conditions are true
// do something
}
}
|
or you can select something, then what it does is iterates to create the result.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
// selection control structure
if(condition)
{
// iteration control structure
while( some condition is true)
{
// do something
}
}
else if(another condition)
{
// iteration control structure
while( some condition is true)
{
// do something
}
}
else
{
// do something
}
|
Once you know how to combine those 3, then you can make any kind of program. All the rest of the syntax is just for improving the design of your program. Like classes, templates, functions, etc.
So if you really want to learn how to program I think you need to focus on those 3 things first, and learn how to combine them in different ways. Once you master those, then you can move on to improving your programs by using templates, classes, functions, and etc. I hope that helps point you in the right direction you were looking for. =)