Please Help i just dont understand c++!

If someone could help me please im looking for some sort of miracle i just dont get c++ especially while and for loops, functions and how to declare an array argument, as well as arrays
ive tried to do many for loop problems and try to make dummy programs to help me understand but i just dont get it.
ive have a test in 2 days...i dont know if there is hope now but im determined to learn this.
If anyone could give me some resources that i can learn from as much as possible in 2 days please do i know this language isnt something you cant learn in 2 days but i have to try to learn these concepts.

this a sample problem from one of my profs sample test please help if u can:
Write a C++ program to prompt the user to enter the following selection. If the user enters 1 (by pressing "1") then your program has to print all even numbers from 0 to 100 (inclusive). If the user enters 2 (by pressing "2") then your program should prompt the user to enter his/her last name, first name (initial only) and student I.D. number and print them. If the user enters any character, you should terminate the program.



heres my program:


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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
using namespace std;
int even(void);


int main()

{
	char last[15];
	char first[2];
	const int max = 5;
	int  ID[max];

	int num;
	


	
		cout << "please enter 1 or 2" << endl;
		cout << "       1 for even numbers" << endl;
		cout << "       2 for print name and ID" << endl;
		cin >> num;
		switch (num)
		{
		
		case 1:
			even();
			break;


	
	cout << "last name" << last[15];
	cout << "first initial" << first[2];
	cout << "ID" << ID[max];
		return 0;
 }


int even()
{  
	int i;
	for (i = 0; i != '\0'; i += 2)
	{
		 i;
	}

	return i;

}





please help if u can im desperate at this point....
i just dont get c++ especially while and for loops, functions and how to declare an array argument, as well as arrays


Here's a quick review. Let's start with loops:

Loops (like the name suggests) are control structures, which loop through and execute a section of code until or while a condition is true.

Here's the syntax for a while loop:

1
2
3
while(<condition>) {
	//code
}


Like the name suggests, while loops executes the code within the brackets while the supplied condition is true.
An example of using a while loop:

1
2
3
4
int counter = 0;
	while(counter != 5) {
	counter++;//increment
}


The while loop will increment the counter variable until the counter reaches five.

Here's the syntax for a for loop:

1
2
3
for(<init>; <condition>; <increment>) {
	//code
}


This one is a bit more confusing for a beginner, but it's very useful.
They're designed with counter variables in mind.

<init>
expects a declaration and initialization of a counter variable.
<condition>
is the condition - loop while this condition is true
<increment>
increments the counter variable

an example of a for loop:

1
2
3
for(int counter = 0; counter != 5; ++counter) {

}


This code does the same as the while loop above. There are some differences, however. Notice that the body of the for loop is empty. It's empty in this example, because it doesn't need to do anything other than increment a counter variable, which it does internally.
Another difference is that the counter variable of the for loop ceases to exist once the for loop terminates, whereas the counter variable for the while loop continues to exist past the termination of the while loop. This is because the for loop counter variable's life is bound by the for loops body scope. This means that the counter cannot be used after the for loop is over, and any attempt to use it will result in a compilation error.

However, you do not need to declare a for loop counter variable on the same line it is initialized. The following example uses a for loop counter variable, which continues to exist past the for loop's termination:

1
2
3
4
int counter;
for(counter = 0; counter != 5; ++counter) {

}


Now arrays:

Because arrays and pointers are related in some ways, they can be daunting for beginners. Right now I'll neglect to share the pointer-side of arrays for simplicity.

If a variable is like a crayon, an array is like a crayon box.
A crayon box can hold a certain amount of crayons, it depends on how big the box is.

An array can hold a certain amount of variables, it depends on how big the array is.

A box of crayons has a slot for each crayon to go into. Similarly, an array has a slot for each variable that it holds. Each variable that is contained within an array is called an "element" - which makes sense, because it's an element of the array.
An array needs to know how many elements it should hold when it is declared. The size of the array cannot be changed later on. An array places the elements next to one another - contiguously in memory.

Here's a simple visual representation of how an array holds it's elements in memory:

	0		1		2		3
[	51	][	123	][	16	][	1337	]


Notice the upper numbers 0 - 3. They represent the index location of each element, which is how we access an element - through an index.

When we are declaring an array, the syntax is:

type name[number of elements];


Example of an array with 4 integer elements:

int array[4];

Note, that an array also can only hold variables of one type. You can't have an array hold, for instance, a char and an int. What type of variable an array can hold is also permanent, and cannot be changed later on.

Now we have an array with four integers. However, none of the integers have a value. We can either choose to initialize the array with values for each of it's elements like so :

int array[4] = {1, 2, 3, 4};

Or, we can access each individual element at a time, and give it a value:

1
2
3
4
5
6
int array[4];

array[0] = 1;
array[1] = 2;
array[2] = 3;
array[3] = 4;


Notice, that the first element of an array is located at index position zero.
Therefore, it's a mistake to assume that array[1] is the first element, and that array[4] is the last element. It's counter intuitive, but it's just something you'll have to learn. If you attempt to access an element which doesn't exist, like so:

array[4] = 5;

You'll probably get a run-time out-of-bounds error. That's a big no-no.

Now, to combine loops and arrays - here's an example of how you might initialize an array with certain computed values:

1
2
3
4
5
6
7
8
9
int number_of_elements = 4;

int array[number_of_elements];

int counter = 0;

while(counter < number_of_elements) {
	array[counter] = counter * 2;
}


We could have expanded the while loop, and instead could have written:

1
2
3
4
5
6
7
8
int number_of_elements = 4;

int array[number_of_elements];

array[0] = 0 * 2;
array[1] = 1 * 2;
array[2] = 2 * 2;
array[3] = 3 * 2;



Now functions:

Functions are a way of splitting up your code, and making it more reusable.
This is a good thing, because you don't need to re-type the same thing over and over again. Let's say that you want to print a very long sentence twice.

1
2
3
4
5
6
int main() {
	std::cout << "This is a very long sentence which goes on and on." << std::endl;
	std::cout << "This is a very long sentence which goes on and on." << std::endl;

	return 0;
}


You could have written the above. Or, you could have used a function which prints the sentence.

1
2
3
4
5
6
7
8
9
void printSentence() {
	std::cout << "This is a very long sentence which goes on and on." << std::endl;
}

int main() {
	printSentence();
	printSentence();
	return 0;
}


In this example, the amount of time you save typing isn't really apparent, but for larger and more complex projects it's impossible to get anything done without functions.

Like a lot of things, functions have their quirks and their learning curve.

When you want to create a new function, you typically declare and define it all at once. You can chose to declare and function, but not define it - but that's a whole different story.

This explanation turned out very long, and it took me a while to write it. Now it's late and I'm tired, so for now I'll just redirect you towards the tutorials on this website:

http://www.cplusplus.com/doc/tutorial/functions/

There's a lot of stuff I'm omitting. I'll probably edit this later for completeness.
Last edited on
Hi @Bass581,
are you allowed to
use strings?
#include <string>

I have a
starting point for
you, i tried to write
helpful comments in
the code, regards!


--
./SampleTest 
Enter an option
[1]Even numbers
[2]Enter info
1

Even numbers from 0 to 100 (inclusive)
0 2 4 6 8 10
12 14 16 18 20 22 24 26 28 30 32
34 36 38 40 42 44 46 48 50 52 54
56 58 60 62 64 66 68 70 72 74 76
78 80 82 84 86 88 90 92 94 96 98
100 

---
./SampleTest 
Enter an option
[1]Even numbers
[2]Enter info
whateverioefubww
Invalid option.. Bye!

---
./SampleTest 
Enter an option
[1]Even numbers
[2]Enter info
2

Under construction!
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//SampleTest.cpp
//##

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <string>
using std::string;
using std::getline;


//function prototypes
bool isEven(int number); 


int main(){

	int option;

	cout<<"Enter an option\n[1]Even numbers\n[2]Enter info"<<endl;
	cin>>option;

	//"Catch" invalid input
	if(option<1||option>2||cin.fail()){
		if(cin.fail()){
			cin.clear();
			cin.ignore(100,'\n');
		}//end if
		cout<<"Invalid option.. Bye!"<<endl;
	}else{ //Option 1 or 2
		if(option==1){ //Print even numbers
			cout<<"\nEven numbers from 0 to 100 (inclusive)"<<endl;
			for(int number=0;number<=100;number++){
				if(isEven(number)){
					cout<<number<<((number+1)%11==0?'\n':' ');
				}//end if
			}//end for
		cout<<endl;
		}else{ //Prompt user to enter info
			cout<<"\nUnder construction!"<<endl;
		}//end if-else
		
	}//end if-else



return 0; //indicates success
}//end of main

bool isEven(int number){
	if(number%2==0)return true;


return false;
}//end function isEven 
thank you all for your help!
and eyenrique i know how to use strings and in this problem it would make life so much simpler but i think my prof wants me to use arrays to print out the characters.....
Last edited on
closed account (zybCM4Gy)
Sounds like you should have covered pointers already.

But think about

char array_of_characters[6] = ['h', 'e','l','l','o','\0]
char *array_of_characters = "hello".

Take a look at the tutorial on character sequences.

but using an array to print out characters is relatively simple (if pointless bjarne doesn't like arrays at all for this purpose and thinks that they are slow, error prone and expensive).

1
2
3
4
char array[6] = ['h','e','l','l','o']

for i = 0; i < 6 /*array size*/; i++
    cout << array[i];
Yeah we kind of stopped on two dimensional arrays....
I highly recommend searching for thenewboston on youtube and watching his C++ videos. He explains concepts in a very clear way.
Topic archived. No new replies allowed.